Popular Post Vilius Posted February 15, 2016 Popular Post Share Posted February 15, 2016 (edited) People are intimidated by Task/Node based scripts because they think its difficult to write and understand. Well you shouldn't be scared, because I'm going to cover everything you need to know. Before going on to the tutorial, I'm expecting from you, To have at least basic knowledge of java To have at least basic knowledge of the OSbots API So what are Task/Node based scripts? Well its simply states but in OOP design. Alright, so we are going to make an abstract class for a Task. We will use this code which I will explain in a bit. import org.osbot.rs07.script.MethodProvider; public abstract class Task { protected MethodProvider api; public Task(MethodProvider api) { this.api = api; } public abstract boolean canProcess(); public abstract void process(); public void run() { if (canProcess()) process(); } } Breaking down this code just comes simply to a few things. A constructor which accepts the MethodProvider class, but why shouldn't it accept the Script class? Well because we only want the task to know about the OSbot api, not the whole methods that the Script class can have, like onLoop(), onStart(), etc. protected MethodProvider api; public Task(MethodProvider api) { this.api = api; } An abstract boolean public abstract boolean canProcess(); An abstract method public abstract void process(); And a run method public void run() { if (canProcess()) process(); } When a class will inherit the methods from the Task class, that class will have a constructor, an abstract method and boolean. The boolean canProcess() will be the condition on which the process() method will execute code. So basically we do the checks and processing with the run() method, we would just check if the condition is true and let the process() method execute code accordingly. Now, we got that abstract class Task, we are going to make it do work for us. We are going to make a drop task, the class will extend the Task class and it will inherit the abstract methods which the Task class has. The drop task will have a condition, the condition will be api.getInventory().isFull(), the condition will return true after the inventory is full and let the process() method execute script accordingly, the code will be api.getInventory().dropAll(); Our class will look something like this import org.osbot.rs07.script.MethodProvider; public class DropTask extends Task { public DropTask(MethodProvider api) { super(api); } @Override public boolean canProcess() { return api.getInventory().isFull(); } @Override public void process() { api.getInventory().dropAll(); } } As you can see we declare a constructor public DropTask(MethodProvider api) { super(api); } Now you might ask why the constructor looks a bit differently from the abstract Task class and what super(api) means? the super() keyword invokes the parents class constructor AKA the Task's class constructor. Now going further we see @Override public boolean canProcess() { return api.getInventory().isFull(); } So that is how our condition is handled, we just return the booleans variable based on what the getInventory().isFull() returns. In term, if the inventory is full the condition will be true, if the inventory is empty the condition will be false. Further on we see @Override public void process() { api.getInventory().dropAll(); } This is basically what our custom task will do after the canProcess() is true, aka when the condition is true. If inventory is full -> we will drop the items. ' After we got our custom task done, we need to let our script execute the tasks, now how we will do it? Simply we will open our main class which extends Script, meaning that the main class is a script. We declare a new ArrayList at the top of our class, to which we will add tasks to. Our main class should look like this import java.util.ArrayList; import org.osbot.rs07.script.Script; public class Main extends Script{ //this is our array list which will contain our tasks ArrayList<Task> tasks = new ArrayList<Task>(); @Override public int onLoop() throws InterruptedException { return 700; } } Now we have our ArrayList which we have a non primitive data type in it (<Task>), in term think of it like the ArrayList is inheriting the methods from Task class. All though its a different topic called https://en.wikipedia.org/wiki/Generics_in_Java you can look into that if you want. Alright, so we have our ArrayList, time to add our tasks to it. In our onStart() method, which only executes once when the script starts we simply add tasks.add(new DropTask(this)); So basically we are adding a new task to the ArrayList, by doing that, to the .add() we add a new object of our DropTask by doing .add(new DropTask(this)); our DropTask has a constructor for MethodProvider which we simply pass by doing DropTask(this) the keyword this references this class that contains the code. Now why does it work by referencing this class, its because the Main class extends Script, the Script class extends MethodProvider as its stated in the OSBots API docs. So we added our tasks to the ArrayList, now we need to check the conditions state and execute the code accordingly. We simply add this to our onLoop() method. tasks.forEach(tasks -> tasks.run()); Which will iterate trough all the tasks in the ArrayList and execute the run() method, as we remember the run() method in our abstract Task script simply checks the condition and executes code if the condition is true. After all this our main class should look like import java.util.ArrayList; import org.osbot.rs07.script.Script; public class Main extends Script{ ArrayList<Task> tasks = new ArrayList<Task>(); @Override public void onStart(){ tasks.add(new DropTask(this)); } @Override public int onLoop() throws InterruptedException { tasks.forEach(tasks -> tasks.run()); return 700; } } Now after all this you can start making your own tasks and adding them to the list. Alright for those who want quick pastes you can find them here in this spoiler: Task class import org.osbot.rs07.script.MethodProvider; public abstract class Task { protected MethodProvider api; public Task(MethodProvider api) { this.api = api; } public abstract boolean canProcess(); public abstract void process(); public void run() { if (canProcess()) process(); } } DropTask import org.osbot.rs07.script.MethodProvider; public class DropTask extends Task { public DropTask(MethodProvider api) { super(api); } @Override public boolean canProcess() { return api.getInventory().isFull(); } @Override public void process() { api.getInventory().dropAll(); } } Main class import java.util.ArrayList; import org.osbot.rs07.script.Script; public class Main extends Script{ ArrayList<Task> tasks = new ArrayList<Task>(); @Override public void onStart(){ tasks.add(new DropTask(this)); } @Override public int onLoop() throws InterruptedException { tasks.forEach(tasks -> tasks.run()); return 700; } } So no need to be scared, you just really need to try and do it, hope this tutorial helped. Edited April 7, 2016 by Vilius 25 1 Quote Link to comment Share on other sites More sharing options...
Acerd Posted February 15, 2016 Share Posted February 15, 2016 nice guide Quote Link to comment Share on other sites More sharing options...
Realist Posted February 15, 2016 Share Posted February 15, 2016 nice guide Quote Link to comment Share on other sites More sharing options...
Token Posted February 15, 2016 Share Posted February 15, 2016 Gotta love the states vs tasks fight. Big boys write everything in onLoop() :ph34r: :ph34r: :ph34r: 10 Quote Link to comment Share on other sites More sharing options...
PST Posted February 15, 2016 Share Posted February 15, 2016 Good guide Quote Link to comment Share on other sites More sharing options...
Saiyan Posted February 15, 2016 Share Posted February 15, 2016 NOOOOOOOOO DONT LEAK LE TASKS PLZ I want to keep the [TASK SYSTEM] as my tags Quote Link to comment Share on other sites More sharing options...
jakeybob Posted February 15, 2016 Share Posted February 15, 2016 can you explain again :P Quote Link to comment Share on other sites More sharing options...
AresScripts Posted February 15, 2016 Share Posted February 15, 2016 People are intimidated by Task/Node based scripts because they think its difficult to write and understand. Well you shouldn't be scared, because I'm going to cover everything you need to know. Before going on to the tutorial, I'm expecting from you, To have at least intermediate knowledge of java To have at least basic knowledge of the OSbots API So what are Task/Node based scripts? Well its simply states but in OOP design. Alright, so we are going to make an abstract class for a Task. We will use this code which I will explain in a bit. import org.osbot.rs07.script.MethodProvider; public abstract class Task { protected MethodProvider api; public Task(MethodProvider api) { this.api = api; } public abstract boolean canProcess(); public abstract void process(); public void run() { if (canProcess()) process(); } } Breaking down this code just comes simply to a few things. A constructor which accepts the MethodProvider class, but why shouldn't it accept the Script class? Well because we only want the task to know about the OSbot api, not the whole methods that the Script class can have, like onLoop(), onStart(), etc. protected MethodProvider api; public Task(MethodProvider api) { this.api = api; } An abstract boolean public abstract boolean canProcess(); An abstract method public abstract void process(); And a run method public void run() { if (canProcess()) process(); } When a class will inherit the methods from the Task class, that class will have a constructor, an abstract method and boolean. The boolean canProcess() will be the condition on which the process() method will execute code. So basically we do the checks and processing with the run() method, we would just check if the condition is true and let the process() method execute code accordingly. Now, we got that abstract class Task, we are going to make it do work for us. We are going to make a drop task, the class will extend the Task class and it will inherit the abstract methods which the Task class has. The drop task will have a condition, the condition will be api.getInventory().isFull(), the condition will return true after the inventory is full and let the process() method execute script accordingly, the code will be api.getInventory().dropAll(); Our class will look something like this import org.osbot.rs07.script.MethodProvider; public class DropTask extends Task { public DropTask(MethodProvider api) { super(api); } @Override public boolean canProcess() { return api.getInventory().isFull(); } @Override public void process() { api.getInventory().dropAll(); } } As you can see we declare a constructor public DropTask(MethodProvider api) { super(api); } Now you might ask why the constructor looks a bit differently from the abstract Task class and what super(api) means? the super() keyword invokes the parents class constructor AKA the Task's class constructor. Now going further we see @Override public boolean canProcess() { return api.getInventory().isFull(); } So that is how our condition is handled, we just return the booleans variable based on what the getInventory().isFull() returns. In term, if the inventory is full the condition will be true, if the inventory is empty the condition will be false. Further on we see @Override public void process() { api.getInventory().dropAll(); } This is basically what our custom task will do after the canProcess() is true, aka when the condition is true. If inventory is full -> we will drop the items. ' After we got our custom task done, we need to let our script execute the tasks, now how we will do it? Simply we will open our main class which extends Script, meaning that the main class is a script. We declare a new ArrayList at the top of our class, to which we will add tasks to. Our main class should look like this import java.util.ArrayList; import org.osbot.rs07.script.Script; public class Main extends Script{ //this is our array list which will contain our tasks ArrayList<Task> tasks = new ArrayList<Task>(); @Override public int onLoop() throws InterruptedException { return 700; } } Now we have our ArrayList which we have a non primitive data type in it (<Task>), in term think of it like the ArrayList is inheriting the methods from Task class. All though its a different topic called https://en.wikipedia.org/wiki/Generics_in_Java you can look into that if you want. Alright, so we have our ArrayList, time to add our tasks to it. In our onStart() method, which only executes once when the script starts we simply add tasks.add(new DropTask(this)); So basically we are adding a new task to the ArrayList, by doing that, to the .add() we add a new object of our DropTask by doing .add(new DropTask(this)); our DropTask has a constructor for MethodProvider which we simply pass by doing DropTask(this) the keyword this references this class that contains the code. Now why does it work by referencing this class, its because the Main class extends Script, the Script class extends MethodProvider as its stated in the OSBots API docs. So we added our tasks to the ArrayList, now we need to check the conditions state and execute the code accordingly. We simply add this to our onLoop() method. tasks.forEach(tasks -> tasks.run()); Which will iterate trough all the tasks in the ArrayList and execute the run() method, as we remember the run() method in our abstract Task script simply checks the condition and executes code if the condition is true. After all this our main class should look like import java.util.ArrayList; import org.osbot.rs07.script.Script; public class Main extends Script{ ArrayList<Task> tasks = new ArrayList<Task>(); @Override public void onStart(){ tasks.add(new DropTask(this)); } @Override public int onLoop() throws InterruptedException { tasks.forEach(tasks -> tasks.run()); return 700; } } Now after all this you can start making your own tasks and adding them to the list. Alright for those who want quick pastes you can find them here in this spoiler: Task class import org.osbot.rs07.script.MethodProvider; public abstract class Task { protected MethodProvider api; public Task(MethodProvider api) { this.api = api; } public abstract boolean canProcess(); public abstract void process(); public void run() { if (canProcess()) process(); } } DropTask import org.osbot.rs07.script.MethodProvider; public class DropTask extends Task { public DropTask(MethodProvider api) { super(api); } @Override public boolean canProcess() { return api.getInventory().isFull(); } @Override public void process() { api.getInventory().dropAll(); } } Main class import java.util.ArrayList; import org.osbot.rs07.script.Script; public class Main extends Script{ ArrayList<Task> tasks = new ArrayList<Task>(); @Override public void onStart(){ tasks.add(new DropTask(this)); } @Override public int onLoop() throws InterruptedException { tasks.forEach(tasks -> tasks.run()); return 700; } } So no need to be scared, you just really need to try and do it, hope this tutorial helped. I like this one ALOT more than that last one. Nice Job! 1 Quote Link to comment Share on other sites More sharing options...
Chris Posted February 15, 2016 Share Posted February 15, 2016 Ugly 1 Quote Link to comment Share on other sites More sharing options...
Chris Posted February 15, 2016 Share Posted February 15, 2016 Rip 2k16 bois 1 Quote Link to comment Share on other sites More sharing options...
TheGreatests Posted June 9, 2016 Share Posted June 9, 2016 Ok, so how exactly do we start a new task list? Quote Link to comment Share on other sites More sharing options...
Pure_ Posted June 9, 2016 Share Posted June 9, 2016 overall idea is fine i suppose, needs renaming and implementing Runnable cuz whynot 1 Quote Link to comment Share on other sites More sharing options...
Trees Posted September 13, 2016 Share Posted September 13, 2016 Or use an enum comparator. Quote Link to comment Share on other sites More sharing options...
Savages Posted September 14, 2016 Share Posted September 14, 2016 all this looks difficult like how do u do it. Quote Link to comment Share on other sites More sharing options...
Globbee Posted September 15, 2016 Share Posted September 15, 2016 all this looks difficult like how do u do it. well u can start by actually reading it and not just farming post count Quote Link to comment Share on other sites More sharing options...