Jump to content

Eating food when below % of health


Recommended Posts

Posted (edited)

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
Posted
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?

Posted (edited)
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
Posted (edited)

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
  • Like 2
Posted

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

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