Jump to content
View in the app

A better way to browse. Learn more.

OSBot :: 2007 OSRS Botting

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Script causing massive lag

Featured Replies

I am brand new to java / scripting. I have a background in C# and C++ so it hasn't been difficult for me to pick up thus far. (been playing around for about 2 hours now). But i just finished my first semi useful script that just chops willows, and banks them. It does it's purpose how i intended it to. However, when i am actually cutting the trees my CPU usage jumps from 5% to 60+%. and when i stop to go bank it jumps back down to 5% again. Maybe it's my sleep methods or something? can anyone give me some insight on my nooby mistake? lol thanks.

 



package Woodcutter;

import org.osbot.rs07.api.map.Area;
import org.osbot.rs07.api.map.constants.Banks;
import org.osbot.rs07.api.model.Entity;
import org.osbot.rs07.event.WebWalkEvent;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;

@ScriptManifest(author = "HexMurder", name = "A Test Script", info = "Null", version = 0.1, logo = "")

public final class WC_Script  extends Script
{
    final Area TREE_WILLOW_DRAYNOR = new Area(3081, 3223, 3092, 3239);
    
    
    @Override
    public final void onStart()
    {
        log("WC test bot launhced.");
    }
    @Override
    public final void onExit() {
        log("WC test bot closed.");
    }

    @Override
    public final int onLoop() throws InterruptedException
    {
        if(inventory.isFull())
        {
            bank();
        }
        else
        {
            chop();
        }
        return 0;
    }
    
    public void chop() throws InterruptedException
    {
        if(TREE_WILLOW_DRAYNOR.contains(myPosition()))
        {
            Entity tree = getObjects().closest("Willow");
            if(tree != null)
            {
                if(tree.isVisible())
                {
                    if(!myPlayer().isAnimating() && !myPlayer().isMoving())
                    {
                        tree.interact("Chop down");
                        sleep(random(800, 1600));
                    }
                }
                else
                {
                    getWalking().webWalk(tree.getPosition());
                }
            }
        }
        else
        {
            getWalking().webWalk(TREE_WILLOW_DRAYNOR);
        }
    }
    
    public void bank() throws InterruptedException
    {
        if(Banks.DRAYNOR.contains(myPosition()))
        {
            Entity bankbooth = getObjects().closest("Bank booth");
            
            if(bankbooth != null)
            {
                bankbooth.interact("Bank");
                sleep(random(1000, 2000));
            }
            if(getBank().isOpen())
            {
                bank.depositAll(1519);
                sleep(random(900, 1800));
            }
        }
        else
        {
            getWalking().webWalk(Banks.DRAYNOR);
        }
    }
}
[/CODE]

  • Author
13 minutes ago, Eagle Scripts said:

are you sure the CPU isn't coming from any NullPointerExceptions?

i have no idea. Not sure how i would check.

 

It seems that adding sleep(100); fixed it. Not sure why it was necessary. is this normal behavior?

 



public final int onLoop() throws InterruptedException
    {
        if(inventory.isFull())
        {
            bank();
        }
        else
        {
            chop();

            sleep(100); //this is what fixed it
        }
        return 0;
    }

[/CODE]

Edited by HexMurder

Your onloop has a 0ms return. Try setting that to ~700 or so and you should be good.

  • Author
1 minute ago, naaiz said:

Your onloop has a 0ms return. Try setting that to ~700 or so and you should be good.

the return of that function is a sleep? I had no clue. Thanks..

Just now, HexMurder said:

the return of that function is a sleep? I had no clue. Thanks..

Above my last comment you put a static 100ms sleep with //This is what fixed it

There is no need to sleep there. Like you said, the onloop returns a sleep. I usually put mine between 500-1000 ms which works completely fine in most cases.

Hard to explain without line numbers, but where it says return 0; in your code, change it to return 700, or whatever number you see fit.

Also, look into conditional sleeps.

  • Author
2 minutes ago, naaiz said:

Above my last comment you put a static 100ms sleep with //This is what fixed it

There is no need to sleep there. Like you said, the onloop returns a sleep. I usually put mine between 500-1000 ms which works completely fine in most cases.

Hard to explain without line numbers, but where it says return 0; in your code, change it to return 700, or whatever number you see fit.

Also, look into conditional sleeps.

Thanks man. Do you know of any well written sources i could take a look at by chance? been looking around and most of them are very very messy. (Not to say that mine is clean lol). I'm just trying to get a feel for how the api works, and usually reading others code is a quick way to pick it up.

1 minute ago, HexMurder said:

Thanks man. Do you know of any well written sources i could take a look at by chance? been looking around and most of them are very very messy. (Not to say that mine is clean lol). I'm just trying to get a feel for how the api works, and usually reading others code is a quick way to pick it up.

There's plenty of examples in the local scripts section, however often enough those are not that well written. You can get basic knowledge of the API there though. Also just looking through the API helps a lot.

If you want to get serious about scripting, I'd advise you to look into task based scripts, very well written guide here:

Like I said before, try to implement conditional sleeps aswell, makes things smoother.

That's the thread sleep.

Also you have no states and you're making really intensive calls every cycle. In your script when you're chopping trees, you don't sleep until your player is done chopping. Instead you are continuously calling this:
 

  if(TREE_WILLOW_DRAYNOR.contains(myPosition()))
        {
            Entity tree = getObjects().closest("Willow");
            if(tree != null)
            {
                if(tree.isVisible())
                {
                    if(!myPlayer().isAnimating() && !myPlayer().isMoving())
                    {
                        tree.interact("Chop down");
                        sleep(random(800, 1600));

... iterating over all the loaded objects every 800-1600ms (if your player is animating or moving then 0ms). 
                

  • Author
12 minutes ago, Alek said:

That's the thread sleep.

Also you have no states and you're making really intensive calls every cycle. In your script when you're chopping trees, you don't sleep until your player is done chopping. Instead you are continuously calling this:
 


  if(TREE_WILLOW_DRAYNOR.contains(myPosition()))
        {
            Entity tree = getObjects().closest("Willow");
            if(tree != null)
            {
                if(tree.isVisible())
                {
                    if(!myPlayer().isAnimating() && !myPlayer().isMoving())
                    {
                        tree.interact("Chop down");
                        sleep(random(800, 1600));

... iterating over all the loaded objects every 800-1600ms (if your player is animating or moving then 0ms). 
                

Thanks. I will look into using states. Not sure what they are but i will find out soon enough i'm sure. How would you recommend i make this less intensive right now? without using states? adding something like this?

 

	if(myPlayer().isAnimating())
                    {
                        sleep(random(500, 4500));
                    }
	

39 minutes ago, HexMurder said:

Thanks. I will look into using states. Not sure what they are but i will find out soon enough i'm sure. How would you recommend i make this less intensive right now? without using states? adding something like this?

 

 


	if(myPlayer().isAnimating())
                    {
                        sleep(random(500, 4500));
                    }
	

 

Change the if to a while, I think that will be better.

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.