Jump to content

My Code Crashes


Recommended Posts

Posted

I'm learning how to interact with items in the inventory. Also trying to learn how to interact with items on the ground.

When I run the code, my OsBot freezes, and I have to task manager to close it. Here's what I have at the moment.

import org.osbot.rs07.api.model.RS2Object;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;
import org.osbot.rs07.utility.ConditionalSleep;

@ScriptManifest(author = "Kingbutton", info = "Fire Cooking", logo = "", name = "LumbCook", version = 0)
public class Main extends Script {

	@Override
	public int onLoop() throws InterruptedException {

		if (fireCheck()) {
			log("hello bitch");
		} else {
			makeFire();

		}

		return 50;
	}

	public boolean fireCheck() {
		RS2Object fire = objects.closest("Fire");

		if (fire != null) {
			return true;
		} else {
			return false;
		}
	}

	public void makeFire() {

		if (!myPlayer().isAnimating() && !myPlayer().isMoving()) {

			RS2Object log = objects.closest("Logs");

			if (log.interact("Light")) {
				new ConditionalSleep(random(600, 1200)) {

					@Override
					public boolean condition() throws InterruptedException {
						return myPlayer().isAnimating();
					}
				}.sleep();
			}
		}
	}

}

 

Posted (edited)
11 minutes ago, Chris said:

VNxpXer.png

Not 100% what you meant by this, But I took my guess.

This is the change I made.

import org.osbot.rs07.api.model.RS2Object;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;
import org.osbot.rs07.utility.ConditionalSleep;

@ScriptManifest(author = "Kingbutton", info = "Fire Cooking", logo = "", name = "LumbCook", version = 0)
public class Main extends Script {

	@Override
	public int onLoop() throws InterruptedException {

		makeFire();

		return 50;
	}

	public void makeFire() {

		RS2Object fire = objects.closest("Fire");
		RS2Object log = objects.closest("Logs");

		if (fire != null) {
			if (!myPlayer().isAnimating() && !myPlayer().isMoving()) {
				if (log.interact("Light")) {
					new ConditionalSleep(random(600, 1200)) {

						@Override
						public boolean condition() throws InterruptedException {
							return myPlayer().isAnimating();
						}
					}.sleep();
				}
			}
		}

	}

}

It's not freezing anymore but my code isn't doing anything. So am I using the wrong api to interact with the log that's on the ground?

Edited by kingbutton
Posted
import org.osbot.rs07.api.model.RS2Object;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;
import org.osbot.rs07.utility.ConditionalSleep;

@ScriptManifest(author = "Kingbutton", info = "Fire Cooking", logo = "", name = "LumbCook", version = 0)
public class Main extends Script {

	@Override
	public int onLoop() throws InterruptedException {

		if (fireCheck()) {
			log("hello bitch");
		} else {
			makeFire();

		}

		return 50;
	}

	public boolean fireCheck() {
		RS2Object fire = objects.closest("Fire");

		return fire != null;
	}

	public void makeFire() {

		if (!myPlayer().isAnimating() && !myPlayer().isMoving()) {

			GroundItem log = getGroundItems().closest("Logs");

			if (log != null && log.interact("Light")) {
				new ConditionalSleep(3000) {

					@Override
					public boolean condition() throws InterruptedException {
						return myPlayer().isAnimating();
					}
				}.sleep();
			}
		}
	}

}

 

Posted
3 hours ago, kingbutton said:

Ight thanks MAN!

Can you explain to me why in the fireCheck() method I have to return fire != null.

Like why can't i do it in the makeFire() method?

You can do it in the makeFire() method, For example something like this:

public void makeFire() {
		RS2Object fire = objects.closest("Fire");
		if (!myPlayer().isAnimating() && !myPlayer().isMoving() && fire != null) {

			GroundItem log = getGroundItems().closest("Logs");

			if (log != null && log.interact("Light")) {
				new ConditionalSleep(3000) {

					@Override
					public boolean condition() throws InterruptedException {
						return myPlayer().isAnimating();
					}
				}.sleep();
			}
		}
	}

It's mostly down to preference, however the way you had it before makes it clearer (to me) when you want the script to light a fire.

Also at the moment you're trying to interact with logs on the ground. If you want to interact with logs in your inventory do this:

Item log = getInventory().getItem("Logs");

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