Jump to content

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


Apaec

Recommended Posts

Ya it seems to be an issue with all scripts, not just local ones. Ive tried all the free woodcutting bots (thats what im trying to do as my practice run) and they still get stuck at start, the onStart function doesnt even run. It seems to be a problem with my client and not something with the code but thank you for your help!

Link to comment
Share on other sites

  • 2 weeks later...

Hey, i have a few questions regarding my first script.

As i've never done any scripting, i simply can't understand the most of it(when to put "if"-s, "break"-s etc).

So, my plan was to create a simple bow fletching script. I got the fletching part done but i've encountered a few errors with the banking option:

 

1. I can't define the bank booth - Entity booth = objects.closest("Bank Booth"); is what i put, but it says that it cant convert from rs2object to entity

2. How can i get it to open the bank, withdraw items and close it? I tried many different ways but they all gave me errors.

 

Any information is appreciated =]

 

 

 

Link to comment
Share on other sites

Hey, i have a few questions regarding my first script.

As i've never done any scripting, i simply can't understand the most of it(when to put "if"-s, "break"-s etc).

So, my plan was to create a simple bow fletching script. I got the fletching part done but i've encountered a few errors with the banking option:

 

1. I can't define the bank booth - Entity booth = objects.closest("Bank Booth"); is what i put, but it says that it cant convert from rs2object to entity

2. How can i get it to open the bank, withdraw items and close it? I tried many different ways but they all gave me errors.

 

Any information is appreciated =]

 

Try:

RS2Object bankBooth = objects.closest("Bank booth"); //defining the booth

if (!bank.isOpen(){ //if bank isn't already open
if (bankBooth != null && bankBooth .exists() && bankBooth .hasAction("Bank")) { //checking if it exists and if it has the action
bank.interact("Bank"); //clicks bank
sleep(random(400,500)); //little sleep. Conditional would be better but this is fine for now
}
} else {
bank.withdraw("Bowstring", 500);
//add some checks in here, but this will do for now.
bank.close();
}

wrote it in the reply box so sorry if there are any errors. If you're still not clear of anything, please let me know!!! :)

 

apa

Link to comment
Share on other sites

Try:

RS2Object bankBooth = objects.closest("Bank booth"); //defining the booth

if (!bank.isOpen(){ //if bank isn't already open
if (bankBooth != null && bankBooth .exists() && bankBooth .hasAction("Bank")) { //checking if it exists and if it has the action
bank.interact("Bank"); //clicks bank
sleep(random(400,500)); //little sleep. Conditional would be better but this is fine for now
}
} else {
bank.withdraw("Bowstring", 500);
//add some checks in here, but this will do for now.
bank.close();
}

wrote it in the reply box so sorry if there are any errors. If you're still not clear of anything, please let me know!!! smile.png

 

apa

 

Huge thanks mate!! 

But i still manage to find one last error:

 

 private State getState() {     //it underlines "getState" and says: it must return a result of type main.State

Link to comment
Share on other sites

Huge thanks mate!! 

But i still manage to find one last error:

 

 private State getState() {     //it underlines "getState" and says: it must return a result of type main.State

Make sure you're getState() method returns a default state (i.e if none of your if statements are accepted).

 

apa

Link to comment
Share on other sites

  • 2 weeks later...

 

Hi there future scripters! This tutorial I am writing for you all now will go in-depth with everything you need in order to get started writing your very own scripts. After reading through this tutorial you will be capable of writing, compiling and running simple scripts such as a tea thiever, chicken killer and so on. If you are really keen to get started scripting I recommend reading the whole guide through and going back through the parts you did not fully understand.

 

Previous required knowledge:

  • None
  • Knowledge of basic java helps but is not required!

 

What the guide will teach you:

 

  • Downloading your code editor (IDE)
  • Simple code and Basic OSBot API
  • Writing a script (with tester snippets to get you started!)
  • Exporting a script into OSBot
  • Running your script

Please note:

  • Script will not be complex. I will be writing a simple tea thiever.
  • Script will have no paint nor GUI, I'll be writing a follow on guide on implementing these later.

So, let's get started!

 

FIRSTLY, DO NOT BE AFRAID OF THESE { OR THESE () OR THESE } OR ANY OF THOSE! They are simple yet look complicated. All { means is defining the start of a block. } defines the end of it. Simple! 

 

Firstly, you're going to want to download an IDE (a program in which you write and compile code). In this tutorial I'll be using Eclipse, a piece of free software which is perfect for scripting for osbot. Simply put, Eclipse is like a word document in which you write your code and then save the code as a .Jar file, which is interpreted and run by the OSBot client.

 

PART 1 - DOWNLOADING ECLIPSE

 

https://www.eclipse.org/downloads/packages/eclipse-standard-432/keplersr2

 

Head to the above site:

 

H0WjNXh.png

 

Select the correct download button depending on whether your computer is 32/64.

 

Not sure?

Click here: http://windows.microsoft.com/en-us/windows/32-bit-and-64-bit-windows#1TC=windows-vista to check this information

 

Once the download has completed and you have the ZIP file, run it.

 

4SSD6lw.png

 

Open it in WinRar or 7Zip or whichever other programs you will use, and extract it directly to your C root.

 

lkgH5T9.png

 

6FPoDlt.png

 

And that will extract eclipse to your c:\ directory.

 

tn03NRr.png

 

Open eclipse.exe

 

Once you have your IDE in place you should have Eclipse open and this screen should be visible:

 

ETZ3DUA.png

 

Then open your workbench:

 

lunNaas.png

 

Success! You have successfully installed your script developing IDE.

 

ne1cU5L.png

 

Let's move swiftly on to part 2 and start coding!

 

PART 2 - SETTING UP YOUR JAVA PROJECT

 

Create a new java project going by the name of your desired script like so:

 

aSQcp5u.png

 

Give the project the name. 

 

lnv7GoJ.png

 

Click finish and you've successfully created your workspace project!

 

Now you need to create a new class in which to write your code.

 

gLJl5Gl.png

 

Give the class a name (I suggest 'main' for the tutorial, but you could call it 'cabbages' if you wanted, it doesn't matter!)

 

YxztiUz.png

 

Now all we need is to attach osbot to this directory, so we can utilise the API:

 

oBCduqR.png

 

Then simply navigate to your OSBot.jar and select it.

 

Your eclipse should then look like this:

 

R6ySaGv.png

 

Now let's start writing the script!

 

PART 3 - WRITING YOUR SCRIPT

 

To start you will need to paste the following framework from which we will start writing the code:

import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;
 
import java.awt.*;
 
@ScriptManifest(author = "You", info = "My first script", name = "Tea thiever", version = 0, logo = "")
public class main extends Script {
 
    @Override
    public void onStart() {
        log("Let's get started!");
    }
 
    @Override
    public int onLoop() throws InterruptedException {
        return random(200, 300);
    }
 
    @Override
    public void onExit() {
        log("Thanks for running my Tea Thiever!");
    }
 
    @Override
    public void onPaint(Graphics2D g) {
 
    }
 
}

Go ahead and paste that in, and your eclipse should look like so:

 

74E6wns.png

 

This is the basic starting point from which all scripts are made (the 'backbone' of the script). 

 

I will break down each part of this backbone to give you an understanding of what they do and how you use them.

 

shia1Bj.png

 

ONSTART:

 

This look is activated ONLY ONCE when you press the start button on the script while running the client.

You can use this loop for logging things in the console below the screen, initialising timers and variables and much more. Remember this code is always run once and only once when you start the script!

 

Here's an example of me using the onStart to log some information for the user to read:

 

cuFobMt.png

 

PfNMdmW.png

 

ONLOOP

 

This is the heart of the script. This loop repeats itself as the main part of the script. this is where the code and core logic of your script is held. There is a return value on the onLoop which defines the delay between loops. At the moment, the return value is:

return random(200,300);

This means the delay between every loop is a random number of milliseconds between 200 and 300.

 

WKZRfz4.png

 

ONEXIT

 

Similar to onStart, this loop runs only once when you press the red stop button at the top of the bot. You can use this block to log details of the script for example how many teas you thieved, or how long you ran the script for. It often does not server a hugely useful purpose in the logic of a script

 

Finally, 

 

6UAS5kD.png

 

ONPAINT

 

This is where the code for your paint goes. I will not cover this in this tutorial, I'll cover it in the following tutorial which I post. This loop serves no use to the scripts core logic and will not affect how the script runs. You would only use it to display information on the screen such as runtime, teas stolen, teas stolen hourly etc.

 

That's the backbone of the script explained, and I'll now get into the coding.

 

To start with, this script is going to be written using what's called a 'State Machine'. Now you may or may not know what this is, but simply put it means the script will do different things (run different blocks of code) depending on whether a question is answered as true or not.

 

For example, in our tea thiever we want the script to steal tea while the tea stall is available, and drop tea if you have it in your inventory, and if the stall isn't available the script will wait until it is.

 

So that means we want to define three different states: Drop, Steal and Wait.

 

States are held in an Enum (Enumeration) and by Java Convention all subjects within an Enum are CAPITALISED_AND_UNDERSCORED_IF_REQUIRED.

 

PLEASE NOTE:

You do not need to understand the following code, as it is universal to every script you will write in the future. You just need to understand how to use it which trust me is simple enough. Once you get more experienced you can start to read it through and decode what it's doing. If you can already then great! you should find this all very easy!

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 = "Tea thiever", version = 0, logo = "")
public class main extends Script {

	@Override
	public void onStart() {
		log("Welcome to Simple Tea Thiever by Apaec.");
		log("If you experience any issues while running this script please report them to me on the forums.");
		log("Enjoy the script, gain some thieving levels!.");
	}

	private enum State {
		STEAL, DROP, WAIT
	};

	private State getState() {
		if (condition 1 is true)
			return State.DROP;
		if (condition 2 is true)
			return State.STEAL;
		return State.WAIT;
	}

	@Override
	public int onLoop() throws InterruptedException {
		switch (getState()) {
		case STEAL:
			break;
		case DROP:
			break;
		case WAIT:
			break;
		}
		return random(200, 300);
	}

	@Override
	public void onExit() {
		log("Thanks for running my Tea Thiever!");
	}

	@Override
	public void onPaint(Graphics2D g) {

	}

}

The code above will demonstrate to you simply how a state machine works

 

First, you run the code in the onLoop. This is seen by the 'getState()' line. This diverts the scripts runner to the getState method which checks if conditions are true. If a condition is true it stops checking which state you are in and returns that state as true. If the condition is false it continues to check the second condition.

 

PLEASE NOTE:

You always need a default case which will be set as true if all the above conditions are false. In this case the default is 'WAIT', which will only run if conditions 1 and 2 are both false.

 

Now let's look at the getState in context. What we want to to is first define the tea stall. 

 

To define an entity (non-npc in the game such as a cooking range, bank booth, crate, herb patch) you use the following code:

Entity entityname = objects.closest("Entity Name");

FOR EXAMPLE:

Entity stall = objects.closest("Tea Stall");

Okay, so we've defined the tea stall, but now what?

 

Now we're going to want to check if it exists. This is called a 'null check'.

The idea behind a null check is to check if an object exists before interacting with it (you can't interact with an object when it doesn't exist, this will throw an error). A null check will only interact with an object if it is not null (i.e it exists).

 

Here's the implementation:

private State getState() {
		Entity stall = objects.closest("Tea stall");
		if (stall != null)
			return State.STEAL;
		return State.WAIT;
	}

What the above code will do when called:

 

  1. Defines the tea stall
  2. Checks if the tea stall exists
  3. If it does, it returns State.STEAL
  4. If it doesn't, it returns State.WAIT

Other things you can learn from this code:

 

  • '!' means 'not' (inverted)
  • brackets surround statements much like basic maths - 1+2*3 = 7 because it's 1+(2*3).
  • the whole block is surrounded with { } which simply means the start and the end of the block

 

Simple, huh!?

 

Now, we wanted to make the script be able to drop the tea in your inventory. So that's simple too, right?

 

if you're inventory is not empty, then empty it. This will also do the trick of dropping unneccessary random items!

 

Here's the code:

	private State getState() {
		Entity stall = objects.closest("Tea stall");
		if (!inventory.isEmpty())
			return State.DROP;
		if (stall != null)
			return State.STEAL;
		return State.WAIT;
	}

What the above code will do when called:

 

  1. Defines the tea stall
  2. Checks if your inventory ISN'T empty
  3. If it isn't empty, it returns State.DROP
  4. If it is empty, then it continues
  5. Checks if the tea stall exists
  6. If it does, it returns State.STEAL
  7. If it doesn't, it returns State.WAIT

OKAY! so that's all good, we have our states defined correctly, but now what :/ I mean, it returns those states, that's great, but where's the code with what to do go?!

 

Well that's simple, it goes right inside your onLoop.

The following code will show you where it goes:

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 = "Tea thiever", version = 0, logo = "")
public class main extends Script {

	@Override
	public void onStart() {
		log("Welcome to Simple Tea Thiever by Apaec.");
		log("If you experience any issues while running this script please report them to me on the forums.");
		log("Enjoy the script, gain some thieving levels!.");
	}

	private enum State {
		STEAL, DROP, WAIT
	};

	private State getState() {
		Entity stall = objects.closest("Tea stall");
		if (!inventory.isEmpty())
			return State.DROP;
		if (stall != null)
			return State.STEAL;
		return State.WAIT;
	}

	@Override
	public int onLoop() throws InterruptedException {
		switch (getState()) {
		case STEAL:
			Entity stall = objects.closest("Tea stall");
			if (stall != null) {
				stall.interact("Steal-from");
			}
			break;
		case DROP:
			inventory.dropAll();
			break;
		case WAIT:
			sleep(random(500, 700));
			break;
		}
		return random(200, 300);
	}

	@Override
	public void onExit() {
		log("Thanks for running my Tea Thiever!");
	}

	@Override
	public void onPaint(Graphics2D g) {

	}

}

So the following code will actually steal from the stall when the stall exists, drop everything in your inventory if it isn't full, and wait if the stall doesn't exist (when it's unstocked).

 

And that's it - the finished source!

If you have any questions about the source do not hesitate for one second to ask them and i'll do my best to help you out. If you're confused right now, all you need to know how to write is the conditions for the states, and what happens in the states. You do NOT need to know how to write a state machine.

 

PART 4 - COMPILING AND RUNNING THE SCRIPT

 

That's the hardest part over, now it's time to load this script up into osbot and get it running!

 

First, you need to save it as a .jar. To do this, follow the following steps:

 

o5nIBiH.png

 

Export it

 

D5YtmvX.png

 

As a jar

 

6Jh9HmR.png

 

Give it a name and location

 

LOCATION HAS TO BE TO YOUR OSBOT SCRIPTS DIRECTORY!

 

Hit finish!

 

To run the script, open up your osbot, refresh your scripts and it should be there locally!

 

yptIv9Y.png

 

TEA THIEVER SOURCE CODE AND DOWNLOAD (untested!)

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 = "Tea thiever", version = 0, logo = "")
public class main extends Script {

	@Override
	public void onStart() {
		log("Welcome to Simple Tea Thiever by Apaec.");
		log("If you experience any issues while running this script please report them to me on the forums.");
		log("Enjoy the script, gain some thieving levels!.");
	}

	private enum State {
		STEAL, DROP, WAIT
	};

	private State getState() {
		Entity stall = objects.closest("Tea stall");
		if (!inventory.isEmpty())
			return State.DROP;
		if (stall != null)
			return State.STEAL;
		return State.WAIT;
	}

	@Override
	public int onLoop() throws InterruptedException {
		switch (getState()) {
		case STEAL:
			Entity stall = objects.closest("Tea stall");
			if (stall != null) {
				stall.interact("Steal-from");
			}
			break;
		case DROP:
			inventory.dropAll();
			break;
		case WAIT:
			sleep(random(500, 700));
			break;
		}
		return random(200, 300);
	}

	@Override
	public void onExit() {
		log("Thanks for running my Tea Thiever!");
	}

	@Override
	public void onPaint(Graphics2D g) {

	}

} 

Download: http://uppit.com/qh7kvttp0t2i/simpleTeaThief.jar

That's the end of this tutorial and hopefully I inspired a new wave of scripters!

 

If you have any questions do not hesitate for a second to post below, send me a PM or even add my skype (i'd prefer contact via osbot first before contacting my skype though!)

 

That's all folks!

 

Happy scripting,

Apaec.

 

Hey man I downloaded exlipse but it wont start. I have extracted it but it wont load the application.

Link to comment
Share on other sites

Hey man, i use u guide, i want to learn a little more, i want do a script to mining, the Southeast mine of varrock, i want a script what can mine iron and save it on the bank at come back to the mine, can u helpme to script one like this?

 

Sure I can help you, what you need to do is start simple though. Start with just the mining iron part and add banking in later.

 

(make it drop the ores to start). Once you've got that nailed, then you can swap the banking out for dropping.

 

As for the help, the best way you can learn is by trying it and asking me any questions which you happen to have. I'll always be willing to help you!

 

apa

  • Like 1
Link to comment
Share on other sites

Sure I can help you, what you need to do is start simple though. Start with just the mining iron part and add banking in later.

 

(make it drop the ores to start). Once you've got that nailed, then you can swap the banking out for dropping.

 

As for the help, the best way you can learn is by trying it and asking me any questions which you happen to have. I'll always be willing to help you!

 

apa

 

 

thanks i will start with mining, how i can get the ID of iron? i think i start with mine ore, i will make a test and post it for u can see, or i can send in a message?

Link to comment
Share on other sites

thanks i will start with mining, how i can get the ID of iron? i think i start with mine ore, i will make a test and post it for u can see, or i can send in a message?

 

Posting here is fine, I will read it for sure :)

 

You can get the ID of the iron by using the entity hover debug in the debug menu (in the settings of the client)

 

apa

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