Jump to content

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


Vilius

Recommended Posts

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
Link to comment
Share on other sites

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

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

}

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

}

 

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

  • 3 weeks 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...