• Log in or sign up for an account here: https://www.sythe.org/login

Understanding RSBot scripting. [Java]

Rawr

Addict.

Retired Sectional Moderator
$100 USD Donor
Joined
Jul 18, 2008
Sythe Gold
13
Messages
2,442
Pokémon Trainer
Everyone at some point has used RSBot, and if you're anything like me you're wondering "How the hell do I make my own script?" Well, here I'm going to show you some of the basics to get you started!​

First off -
You're going to have a simple template; like below for example...

Code:
//Imports

@ScriptManifest(authors = { "YourName" }, category = "ScriptCategory", name = "Script Name", version = 1.00, description = "<html><head></head><body>Script description here.</body></html\n")
public class YourScriptName extends Script implements PaintListener {

    public boolean onStart(Map<String, String> args) {
        return true;
    }

    public int loop() {
        return 100;
    }
       
    public void onRepaint(Graphics g) {
        if(isLoggedIn()) {
            //Paint Here.
        }
    }
    
    public void onFinish() {
        log("Your Script Name has finished");
    }
}

Now, this may all look very confusing at first. But I'll go through and explain all of the sections.

Section 1) The Imports.
What an Import basically is, is a line of code that is grabbing information from a package allowing you to collect the methods from that class and be able to implement them in your script.

Note: If there's a * at the end of the import before the semi-colon that means the script is going to import EVERYTHING from that file.

  • import java.awt.*;
  • import java.util.*;
  • import java.util.List;
  • import java.util.logging.Level;
  • import javax.accessibility.*;
  • import javax.swing.*;
  • import org.rsbot.bot.Bot;
  • import org.rsbot.script.*;
  • import org.rsbot.script.wrappers.*;
  • import org.rsbot.accessors.*;
  • import org.rsbot.event.listeners.PaintListener;
  • import org.rsbot.event.listeners.ServerMessageListener;
  • import org.rsbot.event.events.ServerMessageEvent;

These are the basic imports allowing you to start your first script.

Section 2) The name.
The name of your script is very important, because this name be EXACTLY what you save the file as. Example... If I were to save it as Template.java I MUST name the script "Template".

Code:
public class YourScriptName extends Script implements PaintListener {

So, in here I'm starting with public class because it's a file for everyone to see, aka public. Then there is the name! "SCRIPT-NAME-HERE". This is where the name of your script goes, you need to change it to the name of you'd like the script to be, just make sure you save the script with that exact name (with .java after). Then after that your script is just gathering the implements the Paint (The graphics presenting data in your bot). Then there is the { bracket which is starting the script code. For every { bracket you must close it with a }.

Section 3) Details of your script.

RSBot has updated this to @ScriptManifest, I personally like it a lot better than the old stuff.

Code:
@ScriptManifest(authors = { "YourName" }, category = "ScriptCategory", name = "Script Name", version = 1.00, description = "<html><head></head><body>Script description here.</body></html\n")

Here we're providing all of the details about your script, name, author, description, etc. This is probably the easiest part of your entire code. To break it down...
Code:
authors = { "YourName" }
This is collecting the creators name. AKA you.
Code:
category = "ScriptCategory"
This is collecting what category your script will go under, such as "Mining" for example. (Case sensitive)
Code:
name = "Script Name"
This is collecting the name of your script. This is the title you will be displaying for people to see while selecting to run a bot.
Code:
version = 1.00
This will collect what version the script code is, version is a "double" variable, which means it will have a decimal.
Code:
description = "<html><head></head><body>Script description here.</body></html\n")
This is going to Present the data @ScriptManifest has collected in an HTML output. Basic HTML knowledge is required for this part. All of your data must go in between the <body>Right here</body> tags. Provide a brief explanation of what your bot does, and if you have selections, or options put them in here.

Section 4) Starting up.
Code:
    public boolean onStart(Map<String,String> args) {
        return true;
    }

This part of your code is going to get any last minute information needed before the script starts, such as GE Prices, Locations, Skills, etc. Once it's collected all of the information, your script will start.

Section 5) The loop!
The loop is where all of your code that's going to run your bot will be. The loop is basically the brains of your script. You can either do a) Put your IF statements inside the loop, or b) make your methods outside it, and declare it in the loop afterwords. I'll do examples of both below... The example will detect if your characters run is greater than 50, and if it is, it will turn it on.

Note: When doing Comments in your code you can either do "//" for commenting 1 line of code, or "/* */" for multiple lines of code.

Note 2: After every line that doesn't end in {, } or : You must end it with a semi-colon ";".

Code:
    /* Option B
        public int turnRunOn(){
        if(getEnergy > 50){
              setRun(true);
        }
   */

    public int loop() {
        //Option A
        if(getEnergy > 50){
              setRun(true);
        }

        //Continue of option B
        turnRunOn();
        return random(500, 800);
    }

Oh yeah, another thing to mention, whenever you see a "return #;" that just means your script is going to wait that # of milliseconds before going on the the next step. Example... 1000 = 1 second. Another thing to mention, if you try to return a negative int such as -1 it will stop your loop, therefore stopping the script.


Section 6) Some basic code to work with.
Here I'm going to give you a few lines of basic code to help you start out making a bot.
log();
Code:
log("Hello, it's Rawr Im Phat");
log is an output statement that will appear in the description box below your bot. It's useful for declaring what your bot is doing at a certain time. You start out with log( then quotes to let the bot know we're putting in a string variable such as above "Hello, it's Rawr Im Phat." then you're going to close it with a ) and end the line with a semi-colon. ;

atNPC();
Code:
RSNPC npcName = getNearestNPCByID(id#);
atNPC(npcName, "String action");

Ok, here we are declaring a final variable for the bot to read which is the RSNPC npcName = getNearestNPCByID(id#); part. We start off my stating that it's an RSNPC and we give it a name npcName. Then we have it = getNearestNPCByID(id#); to search for the nearest NPC by you. Then with our atNPC statement we're going to use that name we made npcName then tell it what to do where it says String action. This can be "Attack" "Teleport" "Trade", whatever is needed.

atObject();
Code:
RSObject objectName = getNearestObjectByID(id#);
atObject(objectName, "String action");
This is very similar to atNPC but we're just doing an object, such as a bankBooth or ironRocks, but if you want to do a bot for woodcutting I suggest you use atTree instead of atObject. For string actions you could do anything such as "Mine" "Chop" "Take", etc.

Section 7) Finishing up.
Code:
public void onFinish() {
    log("Thanks for using my tutorial!");
    }

Simple part of the script, you're basically just telling the bot what to shutdown before the entire script stops, and you could put a log Message of what your bot did, etc. Example... Lets say you made a Woodcutting bot and you cut a certain number of logs, you could put that information in here of what you did.

Section 8) The Paint.
This is the funnest part of the script because it's what makes your script unique in my opinion. Here is where you put all of your progress information such as, exp gained, skill training, exp till next level, time running, etc.

An example of some of the paint opportunities:
Code:
    //Declare colors
    //You use RGB coloring.
    final Color colorName = new Color(R, G, B); 
    
    //Setting Colors 
    g.setColor(colorName); 
    
    //Filling a region
    g.fill3DRect(x, y, height, width, true);

    //Outputing data on your paint.
    g.drawString("Content here.", x, y);

For more on paint read here: Graphics

Well, that's about it for now, I'll refine it more as I read through it.

©RawrImPhat​
 
<3 n4n0
Discord Verified
Two Factor Authentication User
Community Participant
Spam Forum Participant
Sythe's 10th Anniversary
Sythe Awards 2011 Winner
Good for starters, but everything could be explained much more... But that would lead to a huge guide... But still good to read before so people have a better idea what questions they need answered if they want to learn... But. Good guide. Obviously.
 
Can you do that with other bots using the same template?

Also very nice guide, I'm going to need to start using it once RSBOT comes back up.
 
Very good guide. I'm trying to learn how to script so really helped. 10/10.
 
nice guide, help me a bit

I love your sig ;o
 
Eh, it's alright, but if you don't know this, I doubt you would be looking into making scripts, well ones people will use.
 
This is a fairly good guide to get people interested in rsbot scripting.

A few suggestions, however:
  • Change "file" and "folder" to class and package, as class does not import a file, it imports a different class.
  • SCRIPT-NAME-HERE isn't a very good choice of an example class name, as a class name cannot contain a dash (-)
  • Do a bit more explaining about ScriptManifiest, and on how annotations like it work.
  • Explain that if the loop returns -1, then the script will stop its execution.
  • Perhaps add a link to the Sun Javadoc for java.awt.Graphics (located here) so that a scripter can see all of the things that they can do with the Graphics that were passed as an arg in the onRepaint() method.
  • Add a few basic methods of rsbot so that the new scripters know a few basic methods to work with (i.e. log(), findObject(), atObject(), atTile(), etc.).

Overall, I think that this is a great guide to get new people into rsbot scripting, and, with a bit of improvement, can be an extremely helpful resource to these people.
 
Pokémon Trainer
This is a fairly good guide to get people interested in rsbot scripting.

A few suggestions, however:
  • Change "file" and "folder" to class and package, as class does not import a file, it imports a different class.
  • SCRIPT-NAME-HERE isn't a very good choice of an example class name, as a class name cannot contain a dash (-)
  • Do a bit more explaining about ScriptManifiest, and on how annotations like it work.
  • Explain that if the loop returns -1, then the script will stop its execution.
  • Perhaps add a link to the Sun Javadoc for java.awt.Graphics (located here) so that a scripter can see all of the things that they can do with the Graphics that were passed as an arg in the onRepaint() method.
  • Add a few basic methods of rsbot so that the new scripters know a few basic methods to work with (i.e. log(), findObject(), atObject(), atTile(), etc.).

Overall, I think that this is a great guide to get new people into rsbot scripting, and, with a bit of improvement, can be an extremely helpful resource to these people.

Took your advice and added all of it, thanks!
 
Helped a bit. I think someone should make a video commentary on how to script :p
 
I'm stupid then, I don't get ANY of this. I get the first few things(but at the "Loop"). I'm lost. I don't understand. Help?
 
Doesn't really get in-depth, but it's good.
 
Just so everyone is aware, I used RSBot, for fishing and I was perm. banned. So just watch out if you do use the bots.
 
Runescape Gamblingrunescape gambling
Back
Top