himmerberg Posted February 28, 2018 Share Posted February 28, 2018 (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 February 28, 2018 by himmerberg Quote Link to comment Share on other sites More sharing options...
H0rn Posted February 28, 2018 Share Posted February 28, 2018 (edited) 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 February 28, 2018 by H0rn Quote Link to comment Share on other sites More sharing options...
himmerberg Posted February 28, 2018 Author Share Posted February 28, 2018 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. Quote Link to comment Share on other sites More sharing options...
H0rn Posted February 28, 2018 Share Posted February 28, 2018 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. Quote Link to comment Share on other sites More sharing options...
himmerberg Posted February 28, 2018 Author Share Posted February 28, 2018 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? Quote Link to comment Share on other sites More sharing options...
Juggles Posted February 28, 2018 Share Posted February 28, 2018 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 1 Quote Link to comment Share on other sites More sharing options...
H0rn Posted February 28, 2018 Share Posted February 28, 2018 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 { } Quote Link to comment Share on other sites More sharing options...
himmerberg Posted February 28, 2018 Author Share Posted February 28, 2018 (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 February 28, 2018 by himmerberg Quote Link to comment Share on other sites More sharing options...
Apaec Posted February 28, 2018 Share Posted February 28, 2018 (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: 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 February 28, 2018 by Apaec 2 Quote Link to comment Share on other sites More sharing options...
scriptersteve Posted February 28, 2018 Share Posted February 28, 2018 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(); } } 1 Quote Link to comment Share on other sites More sharing options...
gptaqbc Posted February 28, 2018 Share Posted February 28, 2018 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! Quote Link to comment Share on other sites More sharing options...
liverare Posted March 2, 2018 Share Posted March 2, 2018 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: Quote Link to comment Share on other sites More sharing options...