Jump to content

Communication between classes - Why does this not work?


Athylus

Recommended Posts

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
Link to comment
Share on other sites

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