Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
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 will:

      Fix LG for both OSBuddy and RuneLite


      Fix issue where the resizable client isn't able to be made smaller (Thanks @JoeDezzy1)


      Fix detection of the logout game tab when resizable mode and side panels are enabled (Thanks @JoeDezzy1)


      Add initial support for Sentry to allow us to identify and easily debug exceptions happening with all TRiBot users


      Add methods to determine if the bank is actually loaded, and not just the overarching interface (Thanks @wastedbro)



      Upcoming updates:

      Improved CLI support


      Full Sentry support


      Much more
      • 46 replies
    • This release will:

      Fix NPE in Camera API (Thanks @wastedbro)


      Update deposit box interface ids (Thanks @Encoded)


      Add various bank methods (Thanks @wastedbro)


      Banking#getWithdrawXQuantity


      Banking#getDefaultWithdrawQuantity


      Banking#arePlaceholdersOn




      Fix resizeable minimap bug (Thanks @wastedbro)


      Remove Java 8 requirement


      Please note: TRiBot is not yet fully compatible with Java 10+




      Fix the break handler issues by ensuring the break handler thread never gets paused


      Fix broken settings hooks



      Upcoming updates:

      Improved CLI support


      Much more



      Note: If you are using LG, please restart both the RS client and TRiBot
      • 68 replies
    • This release will:

      Add support for using custom F key bindings to switch between game tabs (Thanks @erickho123)


      Fix tab opening for "Skills" and "Kourend Tasks" (Thanks @erickho123)



      Note: If you are using LG, please restart both the RS client and TRiBot
      • 34 replies
    • This release will:

      Fix an issue where breaks would stop firing


      Fix Combat#getWildernessLevel, use dynamic search for text and cache ID for later calls


      Fix an NPE in the Combat API


      Fix Mouse#leaveGame bug where the mouse wouldn't actually leave the game screen
      • 21 replies
    • This release will:

      Add LG support for Runelite


      Fix NPCChat issues


      Fix a bug where the camera angle setter would just hold down a key for 5 seconds (the timeout)


      Slightly adjust the rotation via keys to be more accurate


      Add the ability for asynchronous camera movement via keys


      Make Camera rotation via mouse more fluid, with more antiban, and work much better in resizable mode


      Add a "Camera#setCamera" method, allowing the rotation and angle to be set in parallel


      Increase the likelihood of using the mouse for camera movements


      Add support for adjusting the camera to positionable entities (Positionable#adjustCameraTo)



      Upcoming updates:

      Improved CLI support


      Much more



      Note: If you are using LG, please restart both the RS client and TRiBot
      • 59 replies
  • Recently Browsing   0 members

    No registered users viewing this page.

×