- Joined
- Jul 18, 2008
- Sythe Gold
- 13
- Messages
- 2,442
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" }
Code:
category = "ScriptCategory"
Code:
name = "Script Name"
Code:
version = 1.00
Code:
description = "<html><head></head><body>Script description here.</body></html\n")
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");
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");
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















