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
8 minutes ago, Einstein said:

XZfGuCk.png

 

 

Undeniable proof that this this tutorial will make you write the @BestScripts in the world.

 

Sorry @BestScripts, I've just seen it. Welcome to the forums and good luck with your code. :D

 

Lol. Yeah i was just trying to find an entry point. Later when ill have time im going to write some scripts. Dont know if ill publish them yet tho

  • Like 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Our picks

    • This update features:

      Fixed broken hooks from today's update


      Fix wilderness level with RuneLite (Thanks @Todd)


      Add support for Kotlin .class files in scripts (Thanks @wastedbro)


      Overhaul Inventory API (Thanks @wastedbro)


      Add List support for common methods


      Change method grouping to make more sense (by functionality)


      Refactor methods to utilize Java 8 streams instead of cumbersome loops




      Recognize chatbox minimization (Thanks @JoeDezzy1)


      Fix Screen#isInViewport when NPC chat is open (Thanks @JoeDezzy1)


      Fix login bot bugs (Thanks @erickho123)


      Fix hint arrow return values (Thanks @Encoded)


      Fix depositAllExcept functionality (Thanks @wastedbro)


      Change containing box interface bound and adjust for Y values (Thanks @erickho123)
        • Like
      • 148 replies
    • This release will:

      Fix prayers and world hopper API (Thanks @JoeDezzy1 and @erickho123)


      Improve banking API (Thanks @Encoded)


      Adds methods for returning and using Java Lists, rather than arrays


      Slightly randomizes some hardcoded behaviour


      Removes sleeps from waitConditions; the efficiency saving potential is negligible in these use-cases, therefore cleaner code is preferable


      Other back-end improvements





      Note: If you are using LG, please restart both the RS client and TRiBot.
        • Sad
        • Haha
        • Thanks
        • Like
      • 90 replies
    • This release will:

      Add new internal framework for capturing exceptions


      Fix issue with not selecting the last column in world hopper (Thanks @Todd)


      Add a message about pin usage in Banking#openBank (Thanks @Todd)


      Disable the firewall by default (Thanks @Todd)


      Fix handling of the welcome screen after login (Thanks @Encoded)


      Fix wrong amount bank withdrawal (Thanks @Encoded)


      Fix Screen#isInViewport


      Fix Game#isInViewport (Thanks @Encoded)


      Call onBreakEnd for ListenerManager Breaking Listeners (Thanks @Encoded)


      Fix Prayer#getPrayerPoints NumberFormatException (Thanks @JoeDezzy1)



      Note: If you are using LG, please restart both the RS client and TRiBot.
        • Thanks
        • Like
      • 28 replies
    • 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
        • Like
      • 64 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
        • Like
      • 68 replies
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...