Jump to content
Einstein

Einstein's Scripting Tutorial - Part 1

Recommended Posts

Einstein's Scripting Tutorial

Part 1 - The Basics

 

This tutorial was written specifically for the users that are interested in learning how to code their own scripts, but lack any kind of technical knowledge. After reading this tutorial, you will understand the basics and have a good starting point in this journey.

 

There are plenty of concepts that you will need to learn and understand, but you shouldn't get discouraged by the complexity, as everything will get easier with time, if you put in enough effort.

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".

 

latest?cb=20141201090409       Sounds familiar?

 

In our case, the special run-time environment is the TRiBot client.

Scripts written for TRiBot will only work within it. You cannot run a script by double clicking the files on your desktop. You need to start a TRiBot instance, offer it a script, and then the client will execute it line by line.

 

A script is basically just a set of instructions:

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

 

 You can add rules and conditions to determine if and when to execute the instructions:

  • if the inventory is full: go to the bank
  • if my player is not in combat: click the next goblin
  • while the player is busy cutting the tree: idle - do nothing
  • if the player reached level 40 fishing, switch from trout/salmon to lobsters

 

 

 

 

 

The Beginner Pitfall

So far so good, everything is clear: just some basic instructions for your bots.

The problem is that TRiBot won't be able to execute anything written in plain English. We write scripts in a programming language called Java.

After writing the Java source code, we use a program called a "compiler" to convert the source code into executable "bytecode", and we end up with a working script that can be executed by the client.

 

If you don't have any programming experience, a lot of things (probably everything) will seem utterly incomprehensible at first.

Don't get discouraged or frustrated if you don't understand it from the first attempt. Everyone without exception, even the most brilliant of programmers, have started at the same point in which you are today: absolute beginner.

By deliberate practice, you will gradually become better at it.

 

 

 

Real Life Script

Here is a real-life script that you probably ran today, without even realizing it:

The comments (green) are not executed, they just explain the code, I included them just to make matters more clear. ("!" - means "NOT" )

DT3QOVH.png

 

 

 

 

 

TRiBot Script

 

Setting up a new script:

1. Download any Java IDE if you haven't already. An IDE is a code editor that will make scripting easier. You can find a free IDE by searching online. In this tutorial, we will be using Eclipse.

2. Create new project, this will be your script. You can name it as you wish.

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

4. Create a new package called scripts (lowercase 's'). It's very important to name the package as such.

5. Create a new class, this can also be named as you wish (by convention it starts with uppercase).

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

 

 

The actual coding:

The first step is to extend the class 'Script':

    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 method run from the Script class. Either click the error warning and select "Add unimplemented methods", or write the code yourself as it's shown below (run method):

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 an infinite loop:

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

 

 

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.

 

 

 

 

 

Code example

 

Here is a very simple script that first checks whether or not the inventory is full, and decides what to do next based on this information.

If the inventory is full, the script will walk to the nearest bank and deposit everything. Otherwise if the inventory is not full, the script will walk to the potato field and will pick potatoes continuously until the inventory will be full:

 

Xfw2ryP.png

 

 

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's 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();

 

 

 

 

The end
 

I strongly advise you to learn Java before getting into scripting. Here are some useful links:

 

Here is the link for the TRiBot API, which is one of the most valuable scripting tools, so make sure to bookmark it: https://tribot.org/doc/

 

Thank you for reading my first scripting tutorial. I hope that you've learned something from it, and that you're one step closer to writing your own scripts.

And remember: you will only get better at this by deliberate practice. It's hard, but not impossible.

 

Edited by Einstein
  • Like 13
  • Thanks 2

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)

Share this post


Link to post
Share on other sites

All my tutorials have been updated, and will be updated again in the future as I improve my ability to explain things.

 

This is the last 'Updated' message, as from now on, any subsequent updates will be done without notification. You can see when the last update occurred, by reading the message in the bottom left corner of the threads:

pIV9i9Z.png

 

 

 

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 release includes:

      Fix shift clicking option selecting


      Fix high paint delay settings saving


      Update prayer IDs for the quick select menu


      Remove RS3 support


      Fix hooks



      RS3 Support Removed

      The RS3 client hasn't been updated since our Old-School version of TRiBot was released, as many of you may have noticed. Keeping all of the RS3 code in the client made the client as a whole harder to maintain, larger, slower, and messier. As hardly anyone still uses the RS3 client, and since the RS3 API was hardly functioning, we made the decision to completely remove it from TRiBot.

      For the average user, this means that the client will be smaller, cleaner, and faster. Future updates will also take less work meaning there will be more frequent updates.

      If you were one of the few users still using the RS3 client, we apologize for the inconvenience. No future support for RS3 is planned. There are many other botting clients which has support for RS3, so we recommend finding an alternative if you wish to continue botting on RS3.
      • 21 replies
    • Please welcome our new developers, @JoeDezzy1, @erickho123, @Encoded, and @wastedbro.

      These members will be responsible for working on, maintaining, and improving TRiBot.

      This means that bug fixes and improvements will now come at a much faster pace! We're committed to providing users with the best botting experience possible!
      • 30 replies
    • This release includes:

      More 3rd party libraries for script writers to use


      Apache Commons Codec


      Apache Commons Collections


      Apache Commons Configuration


      Apache Commons IO


      Apache Commons Lang


      Apache Commons Math


      GSON


      Guava


      JFoenix




      Hint arrow API


      Game#getHintArrowX


      Game#getHintArrowY




      Fix player hooks including Player#getSkullIcon and Prayer#getPrayerIcon
      • 49 replies
    • This update includes:

      Fix broken hooks


      Fix login bot for the message "No reply from login server. Please wait 1 minute and try again."


      Fix bug relating to which bot tab is sent human input


      General#randomLong bug fix involving negative numbers


      Fix GE API



      Please note: There are still some issues with the login bot due to a change in the game mechanisms handling the login screen. We're working on a fix and will upload it when ready.
      • 37 replies
    • This release will:

      Fix key event handling issue


      Fix other event handling issue


      Fix RSServer hook


      Update world hopper to have it use OCR, thanks to Todd


      Use proper disposal of old Graphics objects


      Reformat code


      Rearrange code


      Organize code imports


      Apply around 8000 automated code refactorings


      Make preparations for Java 9


      Fix 11 various bugs


      Add more reliable debugging support


      Fix mouseEntered/Exited event dispatching bug


      Fix minimap walking bug where it opens the map


      Fix broken hooks for today's game update
      • 100 replies
  • Recently Browsing   0 members

    No registered users viewing this page.

×