Flag This Hub

RSBot Java Programming | Lesson 3

By


Explanation

Well, to those of you that didn't want to see / couldn't see the video, here's what I say:

Every RSBot script has the following parts:

  • Imports: These state the path of everything you will use in the script. For example, a PaintListener (which is to create text on the screen so that the user knows the running time, exp gained and all that stuff) has to be imported.
  • public class <scriptname> extends Script implements <Paint, ServerMessageListener> { [Script Code] }
  • public boolean onStart(Map<String, String>args){ [onStart Code] }
  • public int loop() { [loop Code] }
  • And others, such as the paintListener.

How to define variables

How to define variables. This is a very important, yet very simple part.

A typical variable definition for RSBot would be something like this:

public <datatype> <varname> = <value>;

You could also use "private", but I'll get to that later. As you may remember if you saw the last hub, datatypes could be many different: int, char, short, and so on. The ones you will mostly use are boolean, String and int. Varname is the name of your variable. Variable names can include letters (both upper and lower case), numbers and these: _. They may not start with a number. There are a few names you can't use such as "int" or "boolean", but if you use Eclipse you'll notice that.

Now you know how to define a variable that can hold 1 value. But what if you have, for example, many ID's of trees? You want to hold in one variable many values. You can do that easily by using arrays.

public <datatype>[] <varname> = {<value1>, <value2>, <etc...>};

So, exactly the same as before, just adding [] after the datatype. Then, after the =, you have to include the values separated by commas and between these { }. Of course, since this is an expression, it will end with a semicolon (;).

Basic Script info

So, you're starting your first script. Well, I always like to start them (by using Eclipse, of course) this way:

public class <scriptname> extends Script implements PaintListener, ServerMessageListener {
 
}

Once you do that, it will tell you that there are unimported methods when you hover over PaintListener and ServerMessageListener, and unimplemented methods when you hover over Script. So, import and implement everything. You should now have something like this:

import com.speljohan.rsbot.event.events.ServerMessageEvent;
 
import com.speljohan.rsbot.event.listeners.ServerMessageListener;
 
import com.speljohan.rsbot.event.events.PaintListener;
 
import com.speljohan.rsbot.script.Script;
 
 
public class <scriptname> extends Script implements PaintListener, ServerMessageListener {
 
public int loop() {
}
 
public void onRepaint(Graphics render) {
}
 
public void serverMessageReceiver(ServerMessageEvent e) {
}
 

And you should add these methods anywhere:

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

You should also read this in order to add the script info.

Some more variable using & definition

Now, some more on defining RSBot Variables.

I have already talked about the different datatypes, and mentioned RSBot's datatypes. Well, I'm going to explain one of the latters.

//RSObject <objectname> = getNearestObjectByID(<objectid>);
//In my example, this was:
RSObject normalTree = getNearestObjectByID(NormalID);
atObject(normalTree, "Chop down");

What that does is define a RSObject variable. This is any RuneScape Object (such as a tree, a rock, or whatever), giving it a name (normalTree), and it does something. What it does is give me the nearest object by its ID. In this case, the ID I gave it was NormalID, my ID for normal trees.

Then, atObject(object, String) does something to an RSObject. What it does is the String, and to what it does it is the object. The String must be in the menu appearing once you right-click whatever object. In my case, when you right-click a tree, "Chop down" appears. When you click that, the character chops down the tree, so that is what I wanted.

Some things you want to add

Well, you want to change this:

 
public void onRepaint(Graphics render) {
}
 
public void serverMessageReceiver(ServerMessageEvent e) {
}

To this:

 
public void onRepaint(Graphics render) {
if(isLoggedIn() || render == null) {
//Paint goes here
}
}
 
public void serverMessageReceiver(ServerMessageEvent e) {
String message = e.getMessage();
}

What "if(isLoggedIn() || render == null) does is only execute the next block of code if the character is logged in or if the render is null. So always. "String message = e.getMessage();" just calls the variable "message", which is a String. This variable will get the latest message written in the RuneScape chatbox.

I know, this may sound like chinese to you right now, but read a script or two from another scripter and try to understand them. That way, you will start noticing how simple it is in fact. You may also try doing a simple AutomaticMiner (same as AutomaticWoodcutter, but simpler).


NOTE: AutomaticWoodcutter as in the video won't work. You need more methods! But a miner like that will (just that it will do nothing of interest!).

I hope you learnt something. Check out the next tutorial pretty soon!

hunterdurn 2 years ago

when hovering over paint listener i get this

Note: This element neither has attached source nor attached Javadoc and hence no Javadoc could be found.

Runescape Cursors 2 years ago

Thanks for posting this. It's really a great help for beginners like me. Keep it up.

Submit a Comment
Members and Guests

Sign in or sign up and post using a hubpages account.



    Like this Hub?
    Please wait working