Jump to content
himmerberg

Eating food when below % of health

Recommended Posts

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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 

  • Like 1
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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 1
Link to comment
Share on other sites

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: 

 

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.

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