Jump to content

A Beginners Guide to Writing OSBot Scripts (where to get started!) by Apaec


Apaec

Recommended Posts

  • 2 weeks later...

Is it not better to make a instance variable to hold the stall object? And only try to acquire that stall object under certain conditions?

 

I assume you mean a global variable? You could do something like that if you wanted to, by doing so you would only need to find the tea stall once:

import org.osbot.rs07.api.model.Entity;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;
import org.osbot.rs07.utility.ConditionalSleep;

@ScriptManifest(author = "You", info = "My first script", name = "Tea thiever", version = 0.1, logo = "")
public final class TeaThiever extends Script {
    
    private Entity teaStall;

    @Override
    public final int onLoop() throws InterruptedException {
        if(teaStall == null) teaStall = getObjects().closest("Tea stall");
        else if(getInventory().isFull()) getInventory().dropAll();
        else if(teaStall.hasAction("Steal-from")) stealFromStall();
        return random(200, 300);
    }

    private void stealFromStall() {
        if(teaStall.interact("Steal-from")) {
            new ConditionalSleep(5000) {
                @Override
                public boolean condition() throws InterruptedException {
                    return myPlayer().isAnimating();
                }
            }.sleep();
        }
    }
} 
Edited by Explv
Link to comment
Share on other sites

But you still make a new object of the steal stall every time you go through the loop, which is a lot. Is not better to do something like this (memory wise)? So you create the object once and use that same object. After all, it's not going to change or move.

private Entity teaStall;
boolean success = false;

public final int onLoop() throws InterruptedException {
    
    if (success == false) {
        teaStall = getObjects().closest("Tea stall");
        success = true;
    }
    if (teastall == null) {
        success false;
    }

}
Edited by Athylus
Link to comment
Share on other sites

 

But you still make a new object of the steal stall every time you go through the loop, which is a lot. Is not better to do something like this (memory wise)? So you create the object once and use that same object. After all, it's not going to change or move.

private Entity teaStall;
boolean success = false;

public final int onLoop() throws InterruptedException {
    
    if (success == false) {
        teaStall = getObjects().closest("Tea stall");
        success = true;
    }
    if (teastall == null) {
        success false;
    }

}

 

:???: The code I commented with only gets the stall object once, and then stores it. The code you have written here is just a longer way of writing:

private Entity teaStall;

public final int onLoop() throws InterruptedException {
    
    if (teastall == null) teaStall = getObjects().closest("Tea stall");
}

Which is exactly what my code does. Also I would not concern yourself with memory usage here.

Link to comment
Share on other sites

  • 2 weeks later...
import org.osbot.rs07.api.model.Entity;

import org.osbot.rs07.script.Script;

import org.osbot.rs07.script.ScriptManifest;

 

import java.awt.*;

 

@ScriptManifest(author = "You", info = "My first script", name = "Power Miner", version = 0, logo = "")

public class main extends Script {

 


    public void onStart() {

        log("gonna mine");

    }

    

    private enum State {

    MINE, DROP, WAIT

    };

 

    private State getState() {

    Entity Rocks = objects.closest("Rocks");

    if (!inventory.isEmpty())

    return State.DROP;

    if (Rocks != null)

    return State.MINE;

    return State.WAIT;

    }

    


    public int onLoop() throws InterruptedException {

    switch (getState()) {

    case MINE:

    Entity Rocks = objects.closest("Rocks");

    if (Rocks != null) {

    Rocks.interact("Mine-from");

    }

    break;

    case DROP:

    inventory.dropAll();

    break;

    case WAIT:

    sleep(random(500, 700));

    break;

    }

        return random(200, 300);

    }

 


    public void onExit() {

        log("mining done");

    }

 


    public void onPaint(Graphics2D g) {

 

    }

 

}

 

only hovers over rocks doesnt actually mine

Edited by hariake
Link to comment
Share on other sites

 

import org.osbot.rs07.api.model.Entity;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;
 
import java.awt.*;
 
@ScriptManifest(author = "You", info = "My first script", name = "Power Miner", version = 0, logo = "")
public class main extends Script {
 
    public void onStart() {
        log("gonna mine");
    }
    
    private enum State {
    MINE, DROP, WAIT
    };
 
    private State getState() {
    Entity Rocks = objects.closest("Rocks");
    if (!inventory.isEmpty())
    return State.DROP;
    if (Rocks != null)
    return State.MINE;
    return State.WAIT;
    }
    
    public int onLoop() throws InterruptedException {
    switch (getState()) {
    case MINE:
    Entity Rocks = objects.closest("Rocks");
    if (Rocks != null) {
    Rocks.interact("Mine-from");
    }
    break;
    case DROP:
    inventory.dropAll();
    break;
    case WAIT:
    sleep(random(500, 700));
    break;
    }
        return random(200, 300);
    }
 
    public void onExit() {
        log("mining done");
    }
 
    public void onPaint(Graphics2D g) {
 
    }
 
}
 
only hovers over rocks doesnt actually mine

 

 

If that happens, you've spelt the interaction wrong. For example, perhaps you spelt 'rocks' wrong, or 'mine-from' incorrectly. And when I say incorrectly I mean based on the ingame object. Hover your mouse over the rock and copy exactly what the option and interaction options are!

Link to comment
Share on other sites

If that happens, you've spelt the interaction wrong. For example, perhaps you spelt 'rocks' wrong, or 'mine-from' incorrectly. And when I say incorrectly I mean based on the ingame object. Hover your mouse over the rock and copy exactly what the option and interaction options are!

 

Ive triple checked all spellings, nothing seems wrong. Are you sure there cant be any other causes, script finds the rocks and cycles throgh them all but never clicking on them.

 

Edit: nvm my bad its actually just "mine" not "mine-from"

Edited by hariake
  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...