Jump to content

Practical uses for MultiThreading


sliskoning96

Recommended Posts

I am trying to understand the uses of multithreading for OSBot. My idea is that I make a thread that runs async and monitors the player's health, stats, poison,... And when a certain action is required, for example eating a piece of food, does so. 

 

I created a (very) simple script demonstrating what I would like to accomplish with the multithreading. Is this the way to accomplish such a task?

 

PS: I am new to the OSBot API so I don't know if everything I wrote is correct :D 

import org.osbot.rs07.script.MethodProvider;

public class SupplyThread extends MethodProvider implements Runnable {

    private volatile boolean run = true;

    @Override
    public void run() {
        while(run) {
            try {
                if(myPlayer().getHealthPercent() < (75 * (Math.random())) || myPlayer().getHealthPercent() < 20){
                    if (getInventory().contains("Tuna")) {
                        if (getInventory().getSelectedItemName() == null) {
                            getInventory().getItem("Tuna").interact("Eat");
                        } else {
                            getInventory().deselectItem();
                        }
                    }
                    new ConditionalSleep(2000,1000) {
                        @Override
                        public boolean condition() throws InterruptedException {
                            return myPlayer().getAnimation() == 829;
                        }
                    }.sleep();
                }
                Thread.sleep(random(400, 650));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void stop(){
        run = false;
    }
}

 

Link to comment
Share on other sites

1 hour ago, dreameo said:

1. I know I have volatile (for boolean) added to my own guide for multithreading but I don't think it's needed.

2. This will interfere with 'main' bot since this interaction is async. 

Thank you for the response @dreameo. So if I understand making a thread to take care of supplies and running that thread async is not the way to accomplish such a task? Should I put all supply logic in the main thread then?

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