Jump to content

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


Recommended Posts

  • 2 weeks later...
Posted (edited)

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
Posted (edited)

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
Posted

 

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.

  • 2 weeks later...
Posted (edited)

Got me on the path to programming for OSBot, this is nice to have all of these resources in one place to get going (especially for people who know java already, having a basic thing to look at is great !)

 

Thanks for putting the time in.

Edited by Nebulae
  • Like 1
Posted (edited)
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
Posted

 

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!

Posted (edited)

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

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