Jump to content

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


Recommended Posts

Posted

This was a really nice tutorial! Thanks!

 

You did a really great job on your wording and flow through the different components. I already had a basic idea of how to get this going and this tutorial did a fantastic job cementing the ideas around splitting your states into their own classes. Thanks so much for the time you spent on this.

 

As a side note, this really should be Pinned..

  • Like 1
  • 9 months later...
  • 3 months later...
  • 1 year later...
  • 9 months later...
  • 2 weeks later...
Posted

Is there a way to have the script stop processing the task list once it matches on of the canProcess() criteria?

 

Currently it keeps trying to process other tasks or at least matching them, but if there are two that qualify it runs them. 

 

For example I have a "Debug" task that I want to use if the script falls through and doesn't match any other canProcess() criteria. But the logs show "I am lost" on every single loop, even it it processes one of the other classes. Is there something that can be added to to "do" part of the class that will make it break out of the current loop and start from the top of the task list?

The java keyword "continue" seems to be what I am looking for but no matter where I put it, Eclipse doesn't like it. Here is my debug class:

 

import org.osbot.rs07.script.MethodProvider;


public class Debug extends Task {

	public Debug(MethodProvider api) {
		super(api);
	}

	@Override
	public boolean canProcess() {
		return !api.myPlayer().isMoving();
	}

	@Override
	public void process() {
		api.log("I am lost");
		
	}

}

 

Posted
1 hour ago, sudoinit6 said:

Is there a way to have the script stop processing the task list once it matches on of the canProcess() criteria?

 

Currently it keeps trying to process other tasks or at least matching them, but if there are two that qualify it runs them. 

 

For example I have a "Debug" task that I want to use if the script falls through and doesn't match any other canProcess() criteria. But the logs show "I am lost" on every single loop, even it it processes one of the other classes. Is there something that can be added to to "do" part of the class that will make it break out of the current loop and start from the top of the task list?

The java keyword "continue" seems to be what I am looking for but no matter where I put it, Eclipse doesn't like it. Here is my debug class:

 


import org.osbot.rs07.script.MethodProvider;


public class Debug extends Task {

	public Debug(MethodProvider api) {
		super(api);
	}

	@Override
	public boolean canProcess() {
		return !api.myPlayer().isMoving();
	}

	@Override
	public void process() {
		api.log("I am lost");
		
	}

}

 

use a standard 'for' loop instead of a 'List.forEach' method in main. And you probably want break, not continue.

Posted
16 minutes ago, camaro 09 said:

use a standard 'for' loop instead of a 'List.forEach' method in main. And you probably want break, not continue.

Thank you for your response.

My Main class is the same as the one in the first post and I don't see a List.forEach method to change to a for loop.

 

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;
	}

}

 

Posted
1 hour ago, sudoinit6 said:

Thank you for your response.

My Main class is the same as the one in the first post and I don't see a List.forEach method to change to a for loop.

 


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;
	}

}

 

Read until the end of the first post

  • Like 1
Posted
15 hours ago, camaro 09 said:

Read until the end of the first post

OK, I am a little closer, I changed the loop to this:

 

for (i = 0; i < tasks.size(); i++){
    		x = tasks.get(i);
    		x.run();
    	}

Which works great, now I am having trouble with the "break;". No matter where I put it in the tasks classes I get syntax error or break cannot be used outside of a loop or a switch. Any hints?

Posted
1 hour ago, sudoinit6 said:

OK, I am a little closer, I changed the loop to this:

 


for (i = 0; i < tasks.size(); i++){
    		x = tasks.get(i);
    		x.run();
    	}

Which works great, now I am having trouble with the "break;". No matter where I put it in the tasks classes I get syntax error or break cannot be used outside of a loop or a switch. Any hints?

public boolean run() {
  if (canProcess()) {
    process();
    return true;
  }
  return false
}

for (i = 0; i < tasks.size(); i++){
  x = tasks.get(i);
  if (x.run()) {
    break;
  }
}

This will allow you to break the loop and reset whenever a task runs

Posted
24 minutes ago, camaro 09 said:

public boolean run() {
  if (canProcess()) {
    process();
    return true;
  }
  return false
}

for (i = 0; i < tasks.size(); i++){
  x = tasks.get(i);
  if (x.run()) {
    break;
  }
}

This will allow you to break the loop and reset whenever a task runs

I came up with something that works but yours is cleaner and I will convert to it. Here is what I did: I declared a static variable "br" in my Task class and added br = 1; to the end of each class. Then I changed my loop to this:

 

int i;
    	Task x;
    	for (i = 0; i < tasks.size(); i++){
    		Task.br = 0;
    		x = tasks.get(i);
    		x.run();
    		if (Task.br == 1) {
    			break;
    		}
 
    	}

 

 

Like I said, yours is better, I just wanted to express that I am trying to learn and not just be spoonfed and I really appreciate your time and help.

  • Like 1
  • 3 weeks later...
Posted

Sorry I am a total noob coder.
We have this code to add the task in this example:

@Override
public void onStart() {
    tasks.add(new Attack(this));

How do I add another task , for example Banking?
Can I put all name of task in an array list and pass that array list as argument to tasks.add( List here)(this)) ?

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

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