RSBot Java Programming | Lesson 4
75Lesson 4
Using Eclipse [Start!]
I have already exaplained a bit on using Eclipse in Lesson 1, but still, a bit of refreshing info before we start.
The first thing you want to do to start with RSBot Scripting is create a new Java Project with its source folder being RSBot folder.
File --> New --> Java Project.
- Enter the project name.
- "Creat project from existing source". Here, go to your RSBot folder and select it.
- Click finish.
Now, you have your project there. When you expand it, various folders appear. Right-click "Scripts", and go to New --> Class. This new class is your script. Name it, and that's it!
Using semicolons and these: { }
First of all, if someone knows how these ({) are called in english, please let me know (comment or something).
Now, I've had some people asking me where to put semicolons. I said: "At the end of every expression". They were like "What?"... So, an expression is every line of executable code. These are expressions:
public int x = 10;import <something>;atObject(Tree, "Chop");//And many others. Practically everything.
So, what is not an expression? An expression is executable lines. These expressions go into blocks. These blocks are defined by these symbols: { CODE; }.
So, this is not an expression. This would be called a method. But not only methods are not expressions. Logical statements (such as if-statements or switches) are not expressions either. I'll go to these later on.
public int loop() {//CODE}
So, public int loop() { } is a method. The Code inside it is made up from expressions. See this: (In bold, methods and other non-expressions, in underlined, expressions)
public int loop() {x = 10;if(x = 10) {x = 5;}return 100;}
So, everything ending with a semicolon (;) is an expression. Expressions are grouped into blocks, which are enclosed between { and }.
If you have your class, and you write an expression outside the closing bracket, it will give you an error. For example, the underlined would be an error:
public class example {int x = 5;}int y = 10;
It would be an error because it is outside the class. The code enclosed between the classe's brackets is everything that can be called or executed. Something outside will never be used, so it gives an error because, anyways, it is useless there.
Creating a PaintListener (Timer!)
So, before this I have been explaining about Java. Now I'm going to start teaching you about RSBot Programming. As I have said before, I feel this is a great way to start easily, because without having to know much you can see your progress. How well I felt when I managed my first script to do something!
So, if you don't have RSBot, download it. If you don't have the latest JDK(you want JDK <number> Update <number>, in my case, JDK 6 Update 16), download it too. Please take into account that RSBot has been updated recently. Therefore, there are things that won't work (such as setParameter). You will have to use a different way.
Now, lets get to creating a Paint.
A Paint (or PaintListener) is a method created by RSBot's creators. What this method does is continually create graphics in the screen, for the user to see. What we want to create is something to show our user how long the script has been running, the experience gained, levels gained and so on. To keep our user informed, you know.
This is the basic Paint template:
public void onRepaint(Graphics render) {if(isLoggedIn()) || render == null) {//Paint code goes here}}
Now, there are a few things you need. In order to create a timer, you need this, that is used by everyone, so feel free to copy it:
long millis = System.currentTimeMillis() - startTime;long hours = millis / (1000 * 60 * 60);millis -= hours * (1000 * 60 * 60);long minutes = millis / (1000 * 60);millis -= minutes * (1000 * 60);long seconds = millis / 1000;
And you will need this variable at the start of your script:
long startTime = System.currentTimeMillis();
Ok, so now something normally taught by books in pretty advanced levels, but that I find easy. You do:
render.drawString(args);
You will change the "args" to what you want the String to include. What this does is draw a String of text (and numbers) in the screen. A basic String would look like this:
render.drawString("Hello!", 1, 1);So, "Hello!" is what will appear in the screen. The first "1" is the x axis on the screen, and the second one, the y axis. You can see the axes of RSBot by going (inside RSBot) to View --> Mouse Position.
But what we want is to show our user the running time. We do this by printing to the screen the hours, minutes and seconds the script has been running:
render.drawString("Time running: " + hours + ":" + minutes + ":" + seconds, 240, 350);What that prints is "Time running: 0:0:0" at the start, that will go increasing every second that passes. Awesome, huh? So, the String printed is written between quoting marks. You can add things to that String by closing these marks and doing "..."+ <something> +"...". That <something> will also be printed.
In our case, hours, minutes and seconds were longs (remember datatypes?), that go increasing. The expression System.currentTimeMillis() gives you the current time in milliseconds. By using maths, be get seconds, minutes and hours out of that.
Now I'll explain how to create counters!
Creating a PaintListener (Exp Counter!)
So, now you just have to add a counter to your Paint.
You want to show the user the amount of exp gained since the starting of the script. To do so, you need 2 variables: 1 to store the initial exp, and another one to store the difference between that and the current exp (in runtime).
So, add these 2 variables to your script (at the start):
int GainedExp, StartingExp = 0;
You have just created 2 int variables, one with an undefined variable (GainedExp) and another equalling 0 (StartingExp).
Now, you should add this somewhere in your paint:
GainedExp = 0;
if(StartingExp == 0) {
StartingExp = skills.getCurrentSkillExp(STAT_MINING);
GainedExp = skills.getCurrentSkillExp(STAT_MINING) - StartingExp;
}What that does is set GainedExp to 0 every time. The first time the script runs, StartingExp = 0, so it sets StartingExp to the current Skill exp (STAT_MINING). You may get any skill exp that way:
skills.getCurrentSkillExp(STAT_<SKILL>);
What that calls is skills, a class made to store every RuneScape skill, then the method getCurrentSkillExp stores in the variable the current skill exp for a certain skill. This skill was in our case Mining (STAT_MINING), but it could also be woodcutting or thieving, for example (STAT_WOODCUTTING, STAT_THIEVING).
So, it sets StartingExp to that (it will only do it once!), and the GainedExp to the current exp (again) take away the starting exp. So, the first time it will equal 0 (since StartingExp will be = 0), but then, as the current Skill Exp grows, GainedExp will increase. This is just simple maths =).
So, you have got your counter, but it isn't showing up in the screen! You must create another String to show that value:
render.drawString("Gained exp: " + GainedExp, 240, 356);What that does is 'draw' another String that will show 'Gained exp: <the exp gained>'. Remember I have invented the coordinates!!
So, I hope you have learnt something! I know you probably don't yet know how to create your own script, but don't worry! I'll get to that soon, after the ServerMessageListener! At the moment I'm just trying to teach you the basic methods and some very basic Java for you to defend yourself with your own Scripts, before actually teaching you how to do them without you knowing. That way you would only get confused!
Good luck with your programming!
PrintShare it! — Rate it: up down flag this hub
There were, but only in video.
Creating the text hubs takes so long that I have no will to do them...
However, from the video you may see my YouTube channel, and from there, the rest of the lessons.




sheryld30 says:
4 weeks ago
Is there a lesson, 5 & 6?