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.

Eating food when below % of health

Featured Replies

Hello!

I'm trying to write my first combat script, but I can't figure out how to make it eat food + it has to be below a certain health percentage.

I only have some expierence with HTML5,CSS and Python so I'm trying to learn Java as I go.

 

This is my code so far:

https://pastebin.com/TcGbX2Hw

 

 

All critique and tips are welcome!

 

 

Edit:

I'm currently reading up on Enum states, is it better to use?

And how do I define all states like: BANKING, EATING etc.?

Edited by himmerberg

if (myPlayer().getHealthPercent() < 35) {

eatFood(); 

}

 

You can find literally anything you need on https://osbot.org/api/

 

You just have to know what you're looking for :)

Edited by H0rn

  • Author
15 minutes ago, H0rn said:

if (myPlayer().getHealthPercent() < 35) {

eatFood(); 

}

 

You can find literally anything you need on https://osbot.org/api/

 

You just have to know what you're looking for :)

I can't find in the api how I make my player look for the item in it's inventory "Tuna".

How do I define it what to eat?

Also eatFood(); returns as a bad method.

You can search the inventory using things like: 

if (getInventory().contains("Tuna")) {

// Do something

}

 

also you'd have to create eatFood(); yourself, that was just an example.

  • Author
2 minutes ago, H0rn said:

You can search the inventory using things like: 


if (getInventory().contains("Tuna")) {

// Do something

}

 

also you'd have to create eatFood(); yourself, that was just an example.

if (myPlayer().getHealthPercent() < 35) {
    if (getInventory().contains("Tuna")) {
        if (getInventory().getSelectedItemName() == null) {
            getInventory().getItem("Tuna").interact("Eat");
        } else {
            getInventory().deselectItem();
        }
    }

}

Do you think this will work?

8 minutes ago, himmerberg said:

I can't find in the api how I make my player look for the item in it's inventory "Tuna".

How do I define it what to eat?

Also eatFood(); returns as a bad method.

eatFood()... you have to code it yourself. 
It's not a  method in the API, he was giving you an example in pseudo code 

Keep in mind that myPlayer().getHealthPercent() only works in combat.

I'm not sure why you're checking for a selected item but I would check for that differently:

 

if (getInventory().isItemSelected()) {
	getInventory().deselectItem();
}
else {

}

 

  • Author
13 minutes ago, H0rn said:

Keep in mind that myPlayer().getHealthPercent() only works in combat.

I'm not sure why you're checking for a selected item but I would check for that differently:

 


if (getInventory().isItemSelected()) {
	getInventory().deselectItem();
}
else {

}

 

Is getHealthPercentCache() better to use?

Or do I have to make it look at the skillstab to determine the health?

 

You mean why I only check for Tuna?

Edited by himmerberg

Here's an example of checking health percentage, and eating if below a threshold:

private int getHpPercent() {
  int staticHp = getSkills().getStatic(Skill.HITPOINTS);
  int dynamicHp = getSkills().getDynamic(Skill.HITPOINTS);
  int hpPercent = 100 * (dynamicHp / staticHp);
  return hpPercent;
}

// --- in your onLoop somewhere --- //
// ...
int hpThreshold = 50; // 50% for example
if (getHpPercent() < hpThreshold) {
  log("Man, I could really use a lobster rn");
  String foodToEat = "Lobster";
  if (getInventory().contains(foodToEat)) {
    if (getInventory().interact("Eat", foodToEat)) {
    	log("Man, that was a tasty lobster!"); 
    }
  }
}
// ...

How skills work:

eMqGxrN.png

 

Good luck

-Apa

Edit: I've had a look at your code, it looks like you're trying to do too much in one go! If this is your first script, start of in stages. Here's what I would do from here on:

  • Maybe try a simple side project, for example making a very basic woodcutting script. This will help you get the hang of some inventory/rs2object basics
  • If you're super keen on fleshcrawlers, build the script in stages. First do the eating. Then the fighting. Then the banking. Don't try and do it all at once
  • Send me a PM if/when you get stuck, i'm always happy to help. It's a super steep learning curve, but if you know a bit of programming already (no, CSS or HTML won't help you here ahah), then you're in a good shape.
  • Once you feel confident with the API, start branching out and learning about Java itself, object orientation and the concepts involved. It's much easier to learn this stuff when you have the basic syntax down!

Edited by Apaec

public void eat() {
    if (getInventory().interact("Eat", this.config.getFoodName())) {
        new ConditionalSleep(2000, 300) {
            @Override
            public boolean condition() throws InterruptedException {
                return myPlayer().getAnimation() == 829;
            }
        }.sleep();

       
    }

}
12 minutes ago, scriptersteve said:

public void eat() {
    if (getInventory().interact("Eat", this.config.getFoodName())) {
        new ConditionalSleep(2000, 300) {
            @Override
            public boolean condition() throws InterruptedException {
                return myPlayer().getAnimation() == 829;
            }
        }.sleep();

       
    }

}

So beautiful!

You've got a lot going on in your loop, which isn't good. Your loop function should be clean and concise:

if (needToEat()) {
	if (canEat()) {
		eat();
	} else {
		bank();
	}
} else if (needToLoot()) {
	if (canLoot()) {
		loot();
	} else {
		bank();
	}
} else {
	if (notKilling()) {
		if (canKill()) {
			kill();
		} else {
			spinTheCameraAroundSoWeDontLogOut();
		}
	}
}

Separate routines is the way to go: 

 

Create an account or sign in to comment

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.