Jump to content

how do you use scriptExecutor?


JGhost

Recommended Posts

I'm trying to run a script using the scriptExecutor but i'm not sure if i'm using the API correctly.

package scriptDispatcher;

import scripts.CookingAssistantScript;

import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;
import org.osbot.rs07.event.ScriptExecutor;

import java.awt.*;

@ScriptManifest(author = "JGhost", info = "Script Dispatcher", name = "Script Dispatcher", version = 0, logo = "")

public class main extends Script {
	ScriptExecutor mainExecutor;
	ScriptExecutor dispatcher = new ScriptExecutor(bot);
	Script mainScript;
	Script taskScript;
	
	@Override
	public void onStart() {
		log("Script Dispatcher Started");
		mainExecutor = this.bot.getScriptExecutor();
		mainScript = mainExecutor.getCurrent();
		taskScript = new CookingAssistantScript();


	}

	@Override
	public int onLoop() throws InterruptedException {
		try {
			if (!dispatcher.isRunning()){
				dispatcher.start(taskScript);				
			}
			
			else{
				log("script is running!");			
			}
			
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return random(200, 300);
	}

	@Override
	public void onExit() {
		log("");
	}

	@Override
	public void onPaint(Graphics2D g) {

	}

}

I'm trying to go from this:

Bot -> MainScriptExecutor -> MainScript

 

to this:

Bot -> MainScriptExecutor -> MainScript -> SecondaryScriptExecutor -> SecondaryScript

 

is this achievable through the API?

Edited by JGhost
Link to comment
Share on other sites

A few points to note:

- You should probably stick to a single ScriptExecutor (the one provided by the Bot instance). I doubt two script executors can run for the same Bot instance.

- You would need to control the ScriptExecutor asynchronously (outside the context of your main script). If the ScriptExecutor is not running, your onLoop will never be called again, and you won't be able to start up the next script.

- Take a look at calling the 'prepare' method before 'start'. Be sure to terminate the previous script before attempting to run a new one.

 

Not sure if this is even possible, but it's worth a shot :E

Link to comment
Share on other sites

 

 
I fail to see how Lemon API relates to enabling OSBot to run multiple scripts. Can you elaborate?

70c54f31-8b69-41e5-8985-99a16052455e_zps1dc56ee2-eae8-4666-90b3-50eff3b4ac63_zps

Lemon2_zpscezl6kw9.png

66f64f2e-e2b8-407d-b0bf-ea237b079ace_zps

 

 

Main.java lets u add a TaskScript. In main you can add(new TaskOne()); In side of TaskOne you have isActive which makes that specific task active when it return true. It also contains the looped code for said task. With it, you can write several "Tasks" or scripts that can all be added to the queue and ran within the main.java.

 

I usually separate different tasks within a single script into different taskscripts, but you could also use different taskscripts as different scripts. For example, TaskOne() onActive() returns true if if the bank contains less than 500 bows, and will cut logs into bows, TaskTwo() might return true if you have 500 + bows in the bank, and could be set to perform a different task, like begin alching the bows. In this case I guess you could say you have two different scripts all within the same script. One for fletching, one for alching.

 

Main:

   public void onStart() {
       super.onStart();
       
       Settings settings = new Settings(this);
       settings.setCallback(b -> {
           start = System.currentTimeMillis();
           //addTask(new Task());
           addTask(new EatTask());
           addTask(new OpenBankTask());
           addTask(new BankingTask());
           addTask(new SpiritTreeTask());
           addTask(new GrandTreeTask());
           addTask(new LadderOneTask());
           addTask(new DaeroTask());
           addTask(new WaydarTask());
           addTask(new LumboTask());
           addTask(new WalkToDungTask());
           addTask(new WalkToSpotTask());
           addTask(new CombatTask());
});
}

In this case, I have different tasks for different steps in my script. These could easily be replaced with completely unrelated tasks, allowing you to have multiple scripts in one.

 

Example Task

import lemons.api.tasks.templates.AbstractTask;

public class Task extends AbstractTask {
   
   private final long startAmount;
   
   @Override
   public void onStart() {
       super.onStart();
   }
   
   public void onTaskStart() {
     //setup code
     startAmount = getInventory().getAmount(995);
   }
   public void run() {
   //code is looped
   getInventory().interact(995, "Drop");
   }

   @Override
   public void onTaskFinish() {
   }
  
   @Override
   public boolean isActive() {
       return (getInventory().contains(995));
   }
}
Edited by LoudPacks
  • Like 1
Link to comment
Share on other sites

 

A few points to note:

- You should probably stick to a single ScriptExecutor (the one provided by the Bot instance). I doubt two script executors can run for the same Bot instance.

- You would need to control the ScriptExecutor asynchronously (outside the context of your main script). If the ScriptExecutor is not running, your onLoop will never be called again, and you won't be able to start up the next script.

- Take a look at calling the 'prepare' method before 'start'. Be sure to terminate the previous script before attempting to run a new one.

 

Not sure if this is even possible, but it's worth a shot :E

 

Thanks Frost!

I did try using prepare before starting the script but i still received an error message. The API doesn't exactly say what prepare(Script script) does, but id assume it's necessary to call before attempting to start a script.

 

 

FrostBug, on 24 Feb 2016 - 03:55 AM, said:

 

I doubt two script executors can run for the same Bot instance.

 

Sounds like that makes sense, although that would be unfortunate for me if that's really the case. I didn't know that the ScriptExecutor was limited to only handle 1 script per bot.

 

FrostBug, on 24 Feb 2016 - 03:55 AM, said:

 

- You would need to control the ScriptExecutor asynchronously (outside the context of your main script).

 

I think that control of the ScriptExectuor is key to getting this idea of mine to work. The only problem is I don't understand how it works or what is the process/conditions it uses to run a script. It wasn't labeled "For internal use only" so I'm assuming it has some practical use.

 

I've tried prepare(Script script) before start(Script script), but i kept getting an error.

 

Does anyone know if it is possible create a new ScriptExecutor(Bot bot) in a seperate thread, use prepare(Script script) to set up a new script, then switch the currently running ScriptExectutor (this.bot.getScriptExecutor() in main) then switching it with the new ScriptExecutor?


 
 

LoudPacks, on 24 Feb 2016 - 11:42 AM, said:

In this case, I have different tasks for different steps in my script. These could easily be replaced with completely unrelated tasks, allowing you to have multiple scripts in one.

 

Thanks for the explanation Loud! I'll definitely be giving this a try.

 

Edited by JGhost
Link to comment
Share on other sites

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