-
Posts
1495 -
Joined
-
Last visited
-
Days Won
1 -
Feedback
100%
Everything posted by Vilius
-
As expected
-
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: So no need to be scared, you just really need to try and do it, hope this tutorial helped.
- 31 replies
-
- 26
-
Holy mother of spaghetti code
-
Its monday here, valentines day ended, fuck off
-
Hmm, Im alone, one parent is dead.
-
http://osbot.org/forum/topic/92518-help-scripts-not-showing-psa/
-
I'm guessing it wont take long.
-
The developers are making updates to the SDN, scripts should show up when its updates. Please don't come to the chatbox and ask why there is no scripts. -Love, Vilius and the chatbox people
-
help, scripts not showing in script selector.
Vilius replied to nebula's topic in Community Discussion
I'm guessing the sdn is down or something -
Yes, 07. Let us know in chatbox or on skype if you want to join :P
-
Yeah ofc, anything above 80cmb works
-
Anyone interested in pking tommorow? 80+ cmb pls Who will pk? Me, Zappa, Keven(maybe) need a few more people
-
Make crepes, put slice of banana in it, cook it in a pan till its crispy brown Get some grape jelly put on top, whipped cream/sour cream And some apple juice with those crepes. Luv it
-
[WIP] selection library, click & point, select it in game
Vilius replied to Botre's topic in Software Development
Oh wow, should I release my graphics library? -
[WIP] selection library, click & point, select it in game
Vilius replied to Botre's topic in Software Development
yeah he do, you just click on a tile and it marks it. -
[WIP] selection library, click & point, select it in game
Vilius replied to Botre's topic in Software Development
Explv has that shit on his location assistant -
[WIP] selection library, click & point, select it in game
Vilius replied to Botre's topic in Software Development
but why tho? -
Oh, well. I guess the true love of my life is a computer. Which would mean that I'm going to seducevely touch its keys while I write code.
-
You could try this: startButton.addActionListener(e -> { started = true; monsterSelect = selectYourMonster.getText().toString(); gui.setVisible(false); }); And if you are doing this to stop your script: public void onStart(){ gui.setVisible(true); while(started != true){ Thread.sleep(); } } Don't do it, do it like I show in this tutorial: http://osbot.org/forum/topic/91963-using-synchronized-to-your-advantage/