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
Posted (edited)

Maybe I'm just an idiot, but any time I try adding TriBot.jar as an external jar nothing's showing up. As a result I have no referenced libraries.   import org.tribot can also not be resolved. I've ran the tribot launcher multiple times to refresh this TriBot.jar file (currently TRiBot3.05.jar)

Edit: ignore me it's Thursday and jagex just pushed an update so the loader is behind and not updating my tribot version

Edited by 2Header

Share this post


Link to post
Share on other sites
Posted (edited)
8 hours ago, 2Header said:


Edit: ignore me it's Thursday and jagex just pushed an update so the loader is behind and not updating my tribot version

These can be deleted I've finally come to my senses and looked in the right folder 

Edited by 2Header
  • Haha 1

Share this post


Link to post
Share on other sites

this might be a dumb question, but is the Tribot.jar file that is attached in his Referenced Libraries the same Jar you download on the home page?

When I download the Jar on the homepage I get TriBot_loader.jar. If I use that as an external library I dont seem to be able to reference org.tribot.scripts.*.

 

image.thumb.png.8580e5a32aeb25144c9d967d0a3aa951.png

Share this post


Link to post
Share on other sites
Posted (edited)
1 hour ago, dudeinbanff said:

this might be a dumb question, but is the Tribot.jar file that is attached in his Referenced Libraries the same Jar you download on the home page?

When I download the Jar on the homepage I get TriBot_loader.jar. If I use that as an external library I dont seem to be able to reference org.tribot.scripts.*.

 

image.thumb.png.8580e5a32aeb25144c9d967d0a3aa951.png

No, its the tribot.jar in your .tribot/dependecies
If on windows it's ->C:\Users\"User_Name"\AppData\Roaming\.tribot\dependancies

You can use %appdata% in the start->search bar to open the Roaming folder .And navigate to the .tribot folder(dot->.tribot)

Edited by Red_Spark
  • Like 2

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

    • Today marks a big day for TRiBot! To make it easier for users to use TRiBot, we've created installers available for every platform! These installers are all bundled with the latest version of OpenJDK 1.8 (Java 😎, which is LG compatible.

      Using TRiBot is now easy. Simply download the installer for your platform, install TRiBot, and run it. The TRiBot Loader will correctly identify the bundled JDK so there's no need to change the Java selection.

      Windows

      EXE installer: TRiBot-windows-x64-4.0.3.exe


      MSI installer: TRiBot-windows-x64-4.0.3.msi


      Portable version: TRiBot-windows-x64-4.0.3.zip


      Mac OS

      Installer: TRiBot-macos-4.0.3.dmg


      Portable version: TRiBot-macos-4.0.3.tgz


      Unix/Linux

      Installer: TRiBot-unix-4.0.3.sh


      RPM installer (CentOS/Fedora): TRiBot-linux-4.0.3.rpm


      DEB installer (Debian): TRiBot-linux-4.0.3.deb


      Portable version: TRiBot-unix-4.0.3.tar.gz


      Platform Independent

      JAR file: tribot-loader-4.0.3.jar


      Note that this jar file does not include the bundled JDK.



      Windows and Mac OS users may notice a warning message stating that the installer/application is un-recognized or un-trusted. Please ignore this message and proceed with running the installer/application. We need to acquire a code signing certificate so that we can sign the installers letting the operating system know that these files can be trusted. It will take a week or more to acquire one, so please hold tight.

      Other notable changes to the TRiBot Loader:

      Support getting the version from OpenJDK distributions


      Add check for bundled JDK


      Copy OpenJDK tools.jar to the bundled JDK if not present


      Set the current java as the first available list entry


      Ignore Java versions which are symbolic links


      Make the bundled JDK the preferred Java version


      Update icon images


      Reduce the number of HTTP calls
        • Like
      • 11 replies
    • TRiBot is looking to improve a lot of its customer relationship management, customer on boarding process, customer experience, design elements, community engagement and pretty much everything else you can imagine when it comes to marketing.

      Our goal: To ensure that the marketing done TRULY reflects the experience and does not shine an inaccurate light on what TRiBot is lacking in.

      So I ask, what do you love about TRiBot and what do you hate about TRiBot? What does O S Bot, Rune M8, PowR Bot and Dre amBot do better? (yes I purposely didn't spell it right 😂).

      Love, 

      RileyZ
        • Like
      • 7 replies
    • Hello TRiBot,

      Today we have a significant release that has been in the works for the last month addressing several key issues, features and bugs in the backlog.

      With these changes, we are also including a new TRiBot Loader which will allow you to select any version that is released. This adds the flexibility of allowing you to revert to a previous version should an issue arise, run development only builds, view an accurate change log between versions etc. we are very proud to offer this feature and think it will add a lot more functionality down the road as we continue to release new versions.

      These changes include 80+ commits by our development team, a list of them is summarized below and also available for your viewing pleasure in the new TRiBot Loader.

      In addition, we have taken additional steps to improve as a development team by adding continuous integration and deployment into our workflow to assist in delivering timely releases such as bug fixes as well as new features on a weekly basis depending on our development cycle.
        • Thanks
        • Like
      • 39 replies
    • Over the last three weeks, I've been working on upgrading our server infrastructure. It's finally ready and is now live!

      Why?

      Increased reliability - less server errors


      Increased availability - less downtime


      Increased security - keeping us and you secure


      Increased capacity - ability to serve you better


      Increased speed - less waiting for things to load


      Faster development - server and service updates will come faster


      What are the changes?

      Move from a single AWS EC2 instance to AWS ECS (Elastic Container Service)


      Distributed computing


      Load balancing


      Git management of server files and filesystem


      Redis caching


      How?

      AWS ECS (with 10 EC2 instances)


      AWS ElastiCache (Redis)


      AWS Load Balancing


      AWS EFS (Elastic file system)


      Please bare with us as I continue to tune the server for maximum performance. Slow loading speeds may occur temporarily. I thank everyone for their patience.

      Please post on this thread if you experience any issues other than slow loading times.
        • Like
      • 51 replies
    • 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
      • 151 replies
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...