Jump to content

Babby's first script


Drokle

Recommended Posts

Hello botters,

 

I made the following script just for practice. It is intended to withdraw bones of any kind from the bank and bury them.


import org.osbot.rs07.api.model.Item;
import org.osbot.rs07.api.model.RS2Object;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;

import java.awt.*;

@ScriptManifest(author = "Drokle", info = "Babby's first script", name = "DrokleBone", version = 0, logo = "")


public class DrokleBone extends Script {

    @Override
    public void onStart(){
        log("This is it, you're running DrokleBone!");
    }

    private enum State {
        BANK, BONE
    }


    @Override
    public int onLoop() throws InterruptedException {

        Item[] pockets = inventory.getItems();

        for(Item thing: pockets)
        {
            if(thing.hasAction("Bury"))
                thing.interact("Bury");

            sleep(random(500,550));
        }

        RS2Object booth = getObjects().closest("Bank booth");

        bank.open();
        Item[] vault = bank.getItems();

        for(Item thing: vault)
        {
            if(thing.nameContains("Bones")||thing.nameContains("bones"))
                thing.interact("Withdraw-All");
            if(inventory.getEmptySlotCount() == 0)
                break;
            sleep(random(500,700));
        }

        return random(400,800);
    }

    @Override
    public void onExit() {
        log("This is has been DrokleBone, hope you had a good one.");
    }

    @Override
    public void onPaint(Graphics2D g) {
        g.drawString("THIS IS IT, YOU'RE RUNNING DROKLEBONE!! GOOD JOB!!!!", 50,50);
    }

}

 

For some reason, when I run it, the VM eventually becomes unresponsive and I can't shut it down. Could anyone help me figure out why? It happens if I use it to bury bones far away from any bank.

 

Best,

Drokle

Link to comment
Share on other sites

First of all, the reason that it is crashing is that the variable "booth" which represents a Bank booth is null if there is no bank booth loaded in the game near you, so when you call bank.open() it will throw a nullpointerexception. Second, you never used your states that you set up in the enum, so on every loop, it buries all bones you have in your inventory and opens the bank to withdraw more. I suggest you follow one of the guides to learn the flow that should be followed. Generally, only one action should be performed per loop of the onloop method. Meaning that each iteration only one interaction should occur in the game that takes place over the course of a tick. In your script, you are doing many.

Hope that helps :)

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

Hey ! 

Good effort! As @TheWind said, the key is to have one and only one 'action' line execute in one onLoop iteration. The reason for this is that you are programming for a live game and as such issues such as latency fluctuations can result in interactions failing. If you're relying on an interaction to succeed for future lines of code, this can cause horrible errors! A classic example is banking. For example calling:

getBank().open();
getBank().withdraw("Example", 500);

Is a big error - ask yourself, what if the first line fails? The the script will try to withdraw stuff from the bank but it's not open - uh oh! Instead, you want to make it only do what it needs to based on the current situation:

if (getBank().isOpen()) {
  if (getBank().contains("Example") && getBank().getAmount("Example") >= 500) {
    getBank().withdraw("Example", 500);
    //Conditional sleep here
  } else {
    log("Not enough Example in bank");
    stop();
  }
} else {
  getBank().open();
}

-Apa

Edited by Apaec
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...