Jump to content
View in the app

A better way to browse. Learn more.

OSBot :: 2007 OSRS Botting

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Communication between classes - Why does this not work?

Featured Replies

public class CObject extends MethodProvider {
	public boolean finished = false; 
  
  	public boolean onLoop() {
    	if (...) this.finished = true;
    }
}

public class Main extends Script {
	private CObject obj = new CObject(); 
  	
  	public void onStart() {
    	obj.exchangeContext(getBot());
      	obj.onStart();
    }
  
  	public void onLoop() {
    	switch (state) {
          case 1:
            if (obj.onLoop()) then
              Main.state = 2;
           	break;
          case 2:
            Main.state = 3;
            break;
          case 3:
            log("Code never reaches this point. Why not?");
            break;
          default:
            break;
        }
    }
}

Why does case 1 not work? It does not seem to read the variable correctly.

Edited by Athylus

Will need actual code to diagnose why case 1 is not working. As of right now, there is no way to tell what is causing case 1 to fail since the method onLoop() in the CObject class, if statement, does not show the condition.

Also if your using a switch system, you should look into Enums to separate the different types of states that your bot will be in. It will make it easier to switch between states since you can set the state based on a series of condition checks.

Ex.

Declare an Enum with the different states. I'll just use ATTACK, LOOT, BANK states.

Quote

public enum BotState {
    ATTACK,
    LOOT,
    BANK
}

Than in the main class declare a BotState variable that will hold the current state of the bot.

Quote

public class OSBotScript extends Script {
    
    private BotState botState;

    @Override
    public void onStart() throws InterruptedException {
        super.onStart();
    }

    @Override
    public int onLoop() throws InterruptedException {

        return random(800, 3600);
    }

    @Override
    public void onExit() throws InterruptedException {
        super.onExit();
    }

    @Override
    public void onPaint(Graphics2D g) {
        super.onPaint(g);
    }
}

Than create a method that will change the state of the variable in the main class. And add it to the first line of the onLoop() method.

Quote

public class OSBotScript extends Script {

    private BotState botState;

    @Override
    public void onStart() throws InterruptedException {
        super.onStart();
    }

    @Override
    public int onLoop() throws InterruptedException {
        setBotState();

        return random(800, 3600);
    }

    @Override
    public void onExit() throws InterruptedException {
        super.onExit();
    }

    @Override
    public void onPaint(Graphics2D g) {
        super.onPaint(g);
    }

    private void setBotState() {
        if (!myPlayer().isUnderAttack() && !getInventory().isFull() && getGroundItems().closestThatContains("Bones") == null) {
            botState = BotState.ATTACK;
        } else if (!myPlayer().isUnderAttack() && !getInventory().isFull() && getGroundItems().closestThatContains("Bones") != null) {
            botState = BotState.LOOT;
        } else if (!myPlayer().isUnderAttack() && getInventory().isFull()) {
            botState = BotState.BANK;
        }
    }
}

Lastly, add the switch statement to the onLoop() method.

Quote

public class OSBotScript extends Script {

    private BotState botState;

    @Override
    public void onStart() throws InterruptedException {
        super.onStart();
    }

    @Override
    public int onLoop() throws InterruptedException {
        setBotState();

        if (botState != null) {
            switch (botState) {
                case ATTACK:
                    // Do attack method here.
                    break;
                case LOOT:
                    // Do loot method here.
                    break;
                case BANK:
                    // Do bank method here.
                    break;
                default:
                    log("Unsupported bot state.");
                    break;
            }
        }

        return random(800, 3600);
    }

    @Override
    public void onExit() throws InterruptedException {
        super.onExit();
    }

    @Override
    public void onPaint(Graphics2D g) {
        super.onPaint(g);
    }

    private void setBotState() {
        if (!myPlayer().isUnderAttack() && !getInventory().isFull() && getGroundItems().closestThatContains("Bones") == null) {
            botState = BotState.ATTACK;
        } else if (!myPlayer().isUnderAttack() && !getInventory().isFull() && getGroundItems().closestThatContains("Bones") != null) {
            botState = BotState.LOOT;
        } else if (!myPlayer().isUnderAttack() && getInventory().isFull()) {
            botState = BotState.BANK;
        }
    }
}

 

Edited by BravoTaco

Create an account or sign in to comment

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.