Jump to content
Einstein

Einstein's Scripting Tutorial

Recommended Posts

 

I've recently received a PM from a user asking for scripting advice.

 

His code made me contemplate suicide.

 

 

yu9AUtD.png

 

 

I'm writing this tutorial in an attempt to prevent this from happening again in the future.

 

 

 

 

 

Einstein's Scripting Tutorial

 

This tutorial covers the basics and it's aimed at users with very little to no programming experience.

After reading this tutorial, you will be able to write your own basic TRiBot scripts.

Let's get started!

 

 

 

What exactly is a script?

 

According to Wikipedia, scripts are:

"programs written for a special run-time environment that automate the execution of tasks that could alternatively be executed one-by-one by a human operator".

 

Sounds familiar?

latest?cb=20141201090409   

 

In this case, the special run-time environment is the TRiBot client. Therefore, scripts written for TRiBot will only work within the TRiBot client. Attempting to execute a script from your desktop will have no effect. You need to start a client instance and then feed it a script in order to get things running.

 

A RuneScape script is basically a set of instructions for your bots to execute:

  • go to the bank
  • kill a goblin
  • move the camera
  • chop down a tree

 Adding rules and condition to those instructions will allow us to write useful scripts, because the actions will be performed at the appropriate time:

  • go to bank if the inventory is full
  • kill a goblin if my player is not in combat
  • do nothing (sleep / idle) while the player is busy cutting the tree
  • if the player reached level 40 fishing, switch from trout/salmon to lobsters

 

 

 

 

Let's take a step back

 

New programmers sometimes get overwhelmed by the avalanche of new symbols, keywords, { } [ ] ( ) ; : ... -> ( ͡° ͜ʖ ͡°)    (ok, maybe the last one is not part of the Java syntax)

Don't get distracted by the apparent complexity, and try to understand the logical meaning of the code. Any kind of software, as incomprehensible and cryptic as it may seem at first, it's really just a very simple set of instructions.

 

In order to understand how simple and logical programming really is, let's take a step back and abstract TRiBot/Runescape.

Consider the following script that you probably ran today IRL, without even being aware of it:

 

The comments (green) are ignored by the compiler, I included them just to make matters more clear. ("!" - means "NOT" )

DT3QOVH.png

 

 

Writing a script that will be executed by TRiBot

 

A. The setup:

Steps 2 to 5 will be repeated every time you write a new script. Step 1 will only be repeated if Eclipse tries to hack you.

 

1. Download any Java IDE. This tutorial uses Eclipse.

2. Create new project, the name is irrelevant.

3. Go to Java Built Path (depends on IDE) >  add extrenal JAR > navigate to .tribot\dependancies > add "TRiBot.jar"

4. Create a new package called scripts (lowercase 's').

5. Create a new class, again the name is irrelevant.

PTebJtc.png     If your setup looks like this, then you are ready to move on.

 

B. The Actual coding:

Now that you finished the setup, you are finally ready to start coding.

 

Simply make your class extend Script (uppercase 'S'). 

    aDvfL4w.png     

This will generate two different errors.

1. You need to import the abstract class Script. Just use the IDE's auto import shortcut (Eclipse: Ctrl + Shift + O).

2.You need to implement the inherited method run from the Script class. Click the error warning > "Add unimplemented methods" or write it as shown below.

 

Things should look like this at this point.

S2x81Ps.png

The run() method will be called automatically when you press M7qNtMw.png in the Tribot client.

The instructions contained within the run() method will be executed in sequence, from top to bottom (Blue Arrow). If all the instructions are executed, the script will stop. In order to prevent this from happening, we will write all the instructions inside a infinite loop (@Todd's signature).

When control flow reaches the end of the loop, instead of exiting the method, it will return back to the top, executing the instructions again. (Red Arrow)

VDBMUrj.png

 

That's everything you need to get a working TRiBot script.

In order to make the script perform useful tasks, you will have to write instructions inside the run method. I've included a script example in this tutorial. For a complete list of instructions, refer to the TRiBot API.

 

 

 

Running the script

Your IDE will automatically compile the .java files into .class files.

Navigate to your workspace (the location of your project) and copy the .class files. Paste them in C:\Users\UserName\AppData\Roaming\.tribot\bin\scripts.

If you packaged the script correctly, it will appear in the script list when you press M7qNtMw.png.

  • VIP is required to run local scripts
  • TRiBot is unable to execute .java files. Only add .class files to the scripts folder.
  • By declaring a script manifest (see below), you will be able to define a custom script category, name, and other metadata.

 

 

 

 

 

Actual script example

 

I never imagined that I would actually publish this.

If I remember correctly, this is my first script.

 

I've removed a few things for simplicity's sake and I've commented every line of code. Note: This modified version is not tested.

06zAgEc.png

 

 

 

Why did I named the potato-gathering method abuse?

Spoiler

i have mental issues

A very long time ago, soon after I started playing Runescape, I discovered a Youtube channel called "Angry Noob" or something like that. In one of his adventures, he finds "free cabbage" and says:

nrp6H2W.png

When I was grabbing IDs for the script I suddenly recalled this scene where Angry Noob is "bug abusing". :lol:

 

 

 

 

 

Null Pointer Exception / Array Index Out of Bounds Exception

 

Two of the most common bugs that new scripters encounter are the Null Pointer Exception and the Array Index Out of Bounds Exception.

It's very easy to avoid them if you understand them correctly. They both refer to the same idea:

You cannot perform actions upon something that doesn't exist.

 

 

Null Pointer Exception

usFOhvq.jpg     Consider the following table. Let's try to eat the apple that is on the table and see what happens.

// Declare a variable of type Apple, named 'apple'.
Apple apple; 

// Search the table for the nearest apple:
// Assign the variable 'apple' with the value returned by the method 'getNearestApple()' 
apple = table.getNearestApple(); 

// Attempt to eat the apple stored in the 'apple' variable
apple.eat();

Line 2: Since there is no apple on the table, the method getNearestApple(), will assign the value null (nothing) to the 'apple' variable.

Line 3: Since the variable apple points to nothing, attempting to perform an action upon it (eat the apple) will throw a Null Pointer Exception.

 

How to prevent a null pointer exception on line 3:

if (apple == null)
  print("The apple variable is null, let's leave it alone.");
else
  apple.eat();
  

 

 

 

Array Index Out of Bounds Exception

 

OXPQlq8.png

 

We have an array of 4 apples.

Let's try to eat the 5th apple and see what happens.

apples[4].eat();

Since the array only contains 4 elements (indexes 0 to 3), attempting to perform an action on the 5th element (index 4) will throw an Array Out of Bounds Exception.

How to prevent this from happening:

if (apples.length < 5)
  print("The array's length is less than 5. We are unable to access the 5th element. (index 4)");  
else
  apples[4].eat();

 

 

Tips

  • I highly recommend learning Java before even attempting to script.
  • Read the whole API a few times: https://tribot.org/doc/
  • The only way you become proficient at any kind of programming is by deliberate practice.
  • Solve the problem first, then write the code.
  • Read the open source scripts of other users.
  • Write scripts because because you enjoy doing it. Have fun; experiment with code.

 

 

 

The end

Thank you for reading my basic scripting tutorial and I hope you learned something from it.

If you need any help with your code, or you just want to harass someone, feel free to send me a PM.

 

Edited by Einstein
  • Like 13
  • Thanks 1

Share this post


Link to post
Share on other sites
Just now, godspower33 said:

Nice, looks pretty neat for people wanting to start learning. I wasn't able to give it a full read but looks good.

Thanks God! :blush:

 

Don't worry about a full read, here is the summary of this tutorial:

1 hour ago, Einstein said:

suicide.

1 hour ago, Einstein said:

( ͡° ͜ʖ ͡°)

1 hour ago, Einstein said:

abuse

1 hour ago, Einstein said:

i have mental issues

1 hour ago, Einstein said:

just want to harass someone

 

  • Like 3

Share this post


Link to post
Share on other sites

Thanks Einstein, this is definitely helpful for someone who hasn't dabbled in scripting before. Now to find time to put work into a decent script. Perhaps a great addition to this would be to give a basic example of a GUI and progress display? Assuming you want to pursue further tutorials.

Great work!

  • Like 2

Share this post


Link to post
Share on other sites
7 hours ago, aexious said:

Thanks Einstein, this is definitely helpful for someone who hasn't dabbled in scripting before. Now to find time to put work into a decent script. Perhaps a great addition to this would be to give a basic example of a GUI and progress display? Assuming you want to pursue further tutorials.

Great work!

Thanks, I'm glad you liked it.

I might write another tutorial on paint, GUI and other script features.

  • Like 1

Share this post


Link to post
Share on other sites
On 11/2/2017 at 1:27 AM, aexious said:

Thanks Einstein, this is definitely helpful for someone who hasn't dabbled in scripting before. Now to find time to put work into a decent script. Perhaps a great addition to this would be to give a basic example of a GUI and progress display? Assuming you want to pursue further tutorials.

Great work!

On 11/2/2017 at 8:53 AM, Einstein said:

I might write another tutorial on paint, GUI and other script features.

On 11/10/2017 at 5:13 AM, TristanWPA said:

This would also be great  ▲ ▲  ....  Thanks for the entertaining and easy to understand tutorial, well written! 

 

Done!

 

  • Like 1

Share this post


Link to post
Share on other sites
1 hour ago, ryansmith1998 said:

 

What do you mean by this? 

You can write a simple script fairly easily with no framework and just using a basic while loop, but begin create a massive script using just that and it'll become messy, so people often find a framework they prefer. (State, "Node/Task", creating other loops etc)

  • Like 1

Share this post


Link to post
Share on other sites
On 12/25/2017 at 2:48 AM, ryansmith1998 said:

What do you mean by this? 

As @HeyImJamie explained, having a framework will allow you to write better, cleaner code, that is much easier to debug, modify or add extra functionality to. You and other programmers that read the code will be able to understand it faster.

 

Einstein's Potato Abuser, featured in this tutorial works fine with a main binary decision:

Is the inventory full?

  • Yes. Bank everything.
  • No. Pick potatoes

 

Scripts that are slightly more complex, will require a proper framework, or else the development process will be hindered.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Our picks

    • This update will:

      Fix GE inventory item positioning bug


      Fix broken object hooks
        • Like
      • 21 replies
    • This release will:

      Fix some ClosedChannelException bug


      Fix bug in RSObject#getAllTiles


      Add game tab support for "Kourend Favour"
        • Like
      • 15 replies
    • This release will:

      Fix Settings UI placement bug


      Fix game object location bug


      Fix small layout bug making the client shift up and down


      Fix client crashing bug where loading the client with a small display area will cause the client to crash


      Fix annoying Linux bug relating to painting events and peers


      Fix settings saving bug where settings are saved to disk more often than they should


      Fix RSInterface#isBeingDrawn bug affecting a limited amount of people


      Drop Java 1.7 bytecode version for 1.8


      Important: Since the downloadable RS client uses Java 7, it will no longer be compatible with Looking Glass. To make up for this, we will add support for using other clients such as RuneLite (at a later date).


      This change was necessary to allow us to use Java 8 syntax. It also paves the way for Java 9/10/11 support.
        • Like
      • 40 replies
    • This update will:

      Fix the RSMenuNode bug which also fixes the bug with bank opening


      Fix the incorrect object positions bug


      Fix and re-enable the LG Objects API Accelerator


      Fix the RSObject#getAllTiles bug
        • Like
      • 22 replies
    • Try our development release by checking "Development Release" on the TRiBot Loader. Note that these new features are currently in beta.

      This release features:

      Re-sizable mode support for both LG and the regular client


      Slightly improved login bot


      Removed final access modifiers from API classes


      Added RSServer hook wrapper to get the client's cached list of server/world info


      [NEW] Bug fix for intelligent banking


      [NEW] Improvement to the stability of LG over time


      [NEW] Vastly improved the reliability and speed of Screen#getColorAt on both LG and the regular client


      [NEW] Fix LG login problems


      [NEW] Fixed re-sizable mode container bug


      [NEW] Fixed re-sizable mode mouse bug


      [NEW] Use of public constants in the Banking API


      [NEW] Use of other various constants such as Projection#NULL_PT and Screen#EMPTY_COLOR



      More features to come very soon!

      Please test it and let us know here if there are any new bugs introduced in this release.
        • Thanks
        • Like
      • 12 replies
  • Recently Browsing   0 members

    No registered users viewing this page.

×