- Joined
- Dec 10, 2014
- Messages
- 3,260
- Thread Author
- #1
An Introduction to the TaskScript
Contents:
- Introduction
- Overview
- Pros and Cons
- Pros
- Cons
- The Framework
- TaskScript
- Task
- Example Script (Willow PowerChopper)
- Identifying the required Tasks
- Filling out the Tasks
- Chop
- Drop
- Adding the Tasks to the TaskScript
- Parting words
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 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
The Framework- Breaks scripts up into modular parts
- Reduced clutter due to modularity
- Easier debugging, due to modularity
- Reusable Tasks, due to modularity
- Large projects can become messy if you don't package correctly
- It's probably overkill for simple scripts
TaskScript (Documentation)
Example Script (Willow Powerchopper)The TaskScript is the main class of your Script. 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;
- validate(): This determines if the Task should be executed or not.
- execute(): This is the running code of the Task.
Identifying the required Tasks
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:
Adding the Tasks to the TaskScript
Powerchoppers are pretty simple, and can be broken up into 2 Tasks; Chop and Drop.
So what we do is create the main script class and the 2 Tasks, I prefer to have my Tasks in a tasks package.
Filling out the TasksSo what we do is create the main script class and the 2 Tasks, I prefer to have my Tasks in a tasks package.
Project overview:
Main Script Class:
The 3 Task stubs:
Code:
package com.slashnhax.tutorials.task_script_tutorial;
import com.runemate.game.api.script.framework.task.TaskScript;
public class ExamplePowerchopper extends TaskScript {
@Override
public void onStart(String... args){
}
}
Code:
package com.slashnhax.tutorials.task_script_tutorial.tasks;
import com.runemate.game.api.script.framework.task.Task;
public class Chop extends Task {
@Override
public boolean validate() {
return false;
}
@Override
public void execute() {
}
}
Code:
package com.slashnhax.tutorials.task_script_tutorial.tasks;
import com.runemate.game.api.script.framework.task.Task;
public class Drop extends Task {
@Override
public boolean validate() {
return false;
}
@Override
public void execute() {
}
}
- 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:
Our validate method now looks like this:
Code:
@Override
public boolean validate() {
return Players.getLocal().getAnimationId() == -1 && !Inventory.isFull() && !GameObjects.newQuery().names("Willow").actions("Chop down").results().isEmpty();
}
Our execute method now looks like this:
Code:
@Override
public void execute() {
GameObject tree = GameObjects.newQuery().names("Willow").actions("Chop down").results().nearest();
if (tree != null) {
if (!tree.isVisible()){
Camera.turnTo(tree);
if(!tree.isVisible()){
Path p = BresenhamPath.buildTo(tree);
if(p != null)
p.step();
}
} else if(tree.interact("Chop down")){
Execution.delayUntil(()->Players.getLocal().getAnimationId() != -1, 5000);
}
}
}
- Drop
We want Drop to execute when our Inventory is full, that's the only criteria.
Our validate should look like this:
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:
Our validate should look like this:
Code:
@Override
public boolean validate() {
return Inventory.isFull();
}
Our execute method will look like this:
Code:
@Override
public void execute() {
if(InterfaceWindows.getInventory().isOpen()) {
for(SpriteItem item: Inventory.getItems("Willow logs")){
if(item.interact("Drop"))
Execution.delayUntil(()->!item.isValid(), 1000);
}
} else {
InterfaceWindows.getInventory().open();
}
}
Adding the Tasks to your script 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 class should look similar to:
The ManifestOur main script class should look similar to:
Code:
import com.runemate.game.api.script.framework.task.TaskScript;
import com.slashnhax.tutorials.task_script_tutorial.tasks.Chop;
import com.slashnhax.tutorials.task_script_tutorial.tasks.Drop;
public class ExamplePowerchopper extends TaskScript {
@Override
public void onStart(String... args){
setLoopDelay(250, 500);
add(new Chop(), new Drop());
}
}
Nothing special about the Manifest, here's mine:
Code:
<manifest>
<main-class>com.slashnhax.tutorials.task_script_tutorial.ExamplePowerchopper</main-class>
<name>Example Powerchopper</name>
<description>Example Powerchopper, uses TaskScript. Chops and drops, nothing special</description>
<version>1.0.0</version>
<compatibility>
<game-type>RS3</game-type>
<game-type>OSRS</game-type>
</compatibility>
<categories>
<category>WOODCUTTING</category>
</categories>
<!--Required to publish on the bot store-->
<internal-id>HaxExamplePowerchopper</internal-id>
<!--The rest are optional-->
<hidden>false</hidden>
<open-source>true</open-source>
</manifest>
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
Last edited: