Jump to content

[Tutorial][Indepth] A better take on task based scripts


Vilius

Recommended Posts

 

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. smile.png

 

I like this one ALOT more than that last one. Nice Job!

  • Like 1
Link to comment
Share on other sites

  • 3 months later...
  • 3 months later...

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.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...