Tutorial An Introduction to the TaskScript

Discussion in 'Tutorials & Resources' started by SlashnHax, Mar 1, 2015.

  1. An Introduction to the TaskScript

    Contents:
    • Introduction
    • Overview
    • Pros and Cons
      • Pros
      • Cons
    • The Framework
      • TaskScript
      • Task
    • Example script bot (Willow PowerChopper)
      • Identifying the required Tasks
      • Filling out the Tasks
        • Chop
        • Drop
      • Adding the Tasks to the TaskScript
    • Parting words
    Introduction
    Welcome to my tutorial, in this tutorial we will learn about the TaskScript. I'll try to break it down as much as possible without making it too long. TaskScripts are probably the most common type of script bot used nowadays.
    Overview
    We will identify the pros/cons of the TaskScript, go through the parts of a TaskScript and then create a simple powerchopper.
    Pros and Cons
    Pros
    • Breaks scripts bots up into modular parts
    • Reduced clutter due to modularity
    • Easier debugging, due to modularity
    • Reusable Tasks, due to modularity
    Cons
    • Large projects can become messy if you don't package correctly
    • It's probably overkill for simple scripts bots
    The Framework
    TaskScript (Documentation)
    The TaskScript is the main class of your script bot. You add Tasks to it within the onStart method and then it will run the Tasks it needs to every loop.​
    Task (Documentation)
    Tasks are classes that extend the Task abstract class. The Task class has two abstract methods;
    1. validate(): This determines if the Task should be executed or not.
    2. execute(): This is the running code of the Task.
    Example script bot (Willow Powerchopper)
    Identifying the required Tasks
    Powerchoppers are pretty simple, and can be broken up into 2 Tasks; Chop and Drop.
    So what we do is create the main script bot class and the 2 Tasks, I prefer to have my Tasks in a tasks package.
    Project overview:
    [​IMG]
    Main script bot Class:
    Code (Text):
    1.  
    2. package com.slashnhax.tutorials.task_script_tutorial;
    3.  
    4. import com.runemate.game.api.script.framework.task.TaskScript;
    5.  
    6. public class ExamplePowerchopper extends TaskScript {
    7.     @Override
    8.     public void onStart(String... args){
    9.  
    10.     }
    11. }
    12.  
    The 3 Task stubs:
    Code (Text):
    1.  
    2. package com.slashnhax.tutorials.task_script_tutorial.tasks;
    3.  
    4. import com.runemate.game.api.script.framework.task.Task;
    5.  
    6. public class Chop extends Task {
    7.     @Override
    8.     public boolean validate() {
    9.         return false;
    10.     }
    11.  
    12.     @Override
    13.     public void execute() {
    14.  
    15.     }
    16. }
    17.  
    Code (Text):
    1.  
    2. package com.slashnhax.tutorials.task_script_tutorial.tasks;
    3.  
    4. import com.runemate.game.api.script.framework.task.Task;
    5.  
    6. public class Drop extends Task {
    7.     @Override
    8.     public boolean validate() {
    9.         return false;
    10.     }
    11.  
    12.     @Override
    13.     public void execute() {
    14.  
    15.     }
    16. }
    17.  
    Filling out the Tasks
    • Chop
    We want Chop to execute when either our Player's animation is -1 and when our Inventory isn't full and there are valid willow trees around. This goes in our validate() method.
    Our validate method now looks like this:
    Code (Text):
    1.  
    2. @Override
    3. public boolean validate() {
    4.     return Players.getLocal().getAnimationId() == -1 && !Inventory.isFull() && !GameObjects.newQuery().names("Willow").actions("Chop down").results().isEmpty();
    5. }
    What we want to do when Chop executes is find the nearest tree, walk to it if needed and then chop it. This goes in our execute() method.
    Our execute method now looks like this:
    Code (Text):
    1.  
    2. @Override
    3. public void execute() {
    4.     GameObject tree = GameObjects.newQuery().names("Willow").actions("Chop down").results().nearest();
    5.     if (tree != null) {
    6.         if (!tree.isVisible()){
    7.             Camera.turnTo(tree);
    8.             if(!tree.isVisible()){
    9.                 Path p = BresenhamPath.buildTo(tree);
    10.                 if(p != null)
    11.                     p.step();
    12.         } else if(tree.interact("Chop down")){
    13.             Execution.delayUntil(()->Players.getLocal().getAnimationId() != -1, 5000);
    14.         }
    15.     }
    16. }
    17.  

    • Drop
    We want Drop to execute when our Inventory is full, that's the only criteria.
    Our validate should look like this:
    Code (Text):
    1.  
    2. @Override
    3. public boolean validate() {
    4.     return Inventory.isFull();
    5. }
    6.  
    When drop executes we will drop all of the logs in the Inventory. If the Inventory is open we will loop through all of the items in the inventory with the name "Willow logs" and drop them, otherwise we will open the inventory.
    Our execute method will look like this:
    Code (Text):
    1.  
    2. @Override
    3. public void execute() {
    4.     if(InterfaceWindows.getInventory().isOpen()) {
    5.         for(SpriteItem item: Inventory.getItems("Willow logs")){
    6.             if(item.interact("Drop"))
    7.                 Execution.delayUntil(()->!item.isValid(), 1000);
    8.         }
    9.     } else {
    10.         InterfaceWindows.getInventory().open();
    11.     }
    12. }
    13.  
    Adding the Tasks to the TaskScript
    Adding the Tasks to your script bot is simple, you just use the add(Task... tasks) method supplied by TaskScript. And while we're doing that we may as well set the loop delay to something reasonable.
    Our main script bot class should look similar to:
    Code (Text):
    1.  
    2. import com.runemate.game.api.script.framework.task.TaskScript;
    3. import com.slashnhax.tutorials.task_script_tutorial.tasks.Chop;
    4. import com.slashnhax.tutorials.task_script_tutorial.tasks.Drop;
    5.  
    6. public class ExamplePowerchopper extends TaskScript {
    7.     @Override
    8.     public void onStart(String... args){
    9.         setLoopDelay(250, 500);
    10.         add(new Chop(), new Drop());
    11.     }
    12. }
    13.  
    The Manifest
    Nothing special about the Manifest, here's mine:
    Code (Text):
    1.  
    2. <manifest>
    3.     <main-class>com.slashnhax.tutorials.task_script_tutorial.ExamplePowerchopper</main-class>
    4.     <name>Example Powerchopper</name>
    5.      <description>Example Powerchopper, uses TaskScript. Chops and drops, nothing special</description>
    6.     <version>1.0.0</version>
    7.     <compatibility>
    8.         <game-type>RS3</game-type>
    9.         <game-type>OSRS</game-type>
    10.     </compatibility>
    11.     <categories>
    12.         <category>WOODCUTTING</category>
    13.     </categories>
    14.     <!--Required to publish on the bot store-->
    15.     <internal-id>HaxExamplePowerchopper</internal-id>
    16.     <!--The rest are optional-->
    17.     <hidden>false</hidden>
    18.     <open-source>true</open-source>
    19. </manifest>
    20.  

    Parting Words
    Thanks for reading my tutorial, if you have any questions feel free to leave a comment. I know that the example is extremely basic, but the purpose of this tutorial is to demonstrate the functionality of TaskScript​
     
    #1 SlashnHax, Mar 1, 2015
    Last edited: Apr 1, 2015
  2. Thanks for giving the community more tutorials, really helps new programmers like myself!
     
  3. Awesome. Absolutely Awesome
     
  4. Thanks for this tutorial, great resource to get up and running with scripting for RuneMate!
     
  5. I found that this code a litle bit different than your example bot in store.
    In store: Task Chop Validate method: used newQuery
    In tutorial: Task Chop Validate method: used getLoaded

    Thank you very much for making this tutorial it helps to start to newbies like i am.
     
    #6 greyb3ast, Aug 24, 2015
    Last edited: Aug 24, 2015
  6. newQuery is better and more powerful, I used getLoaded cause I was tired and it's less typing xD
     
  7. @SlashnHax Please take the time to handle objects correctly if you're going to write a tutorial. New programmers will follow this and not realize they need to store things like player and null check it before calling a method on it.

    #crackingdown
     
    NZL likes this.

Share This Page

Loading...