Jump to content

Communication between classes - Why does this not work?


Recommended Posts

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

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
  • 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...