- Home /
Trying to pick one or the other
I have 2 progress bars one for hunger one for thirst. I pick up two different items, item 1. is water item 2 is food after I pick them up they go into the inventory when I open the inventory I see both items the problem is when I click to use the water both the thirst bar and hunger bar go up. The way it should work is if I click to use water then only the water bar should go up and if I click to use the food only the food bar should go up. Here is the script can anyone tell me what I am doing wrong?
function UseEffect ()
{ if (Thirst.tcurHP == true && Hunger.fcurHP == false);
{
Thirst.tcurHP += 25;
}
if (Thirst.tcurHP == false && Hunger.fcurHP == true);
{
Hunger.fcurHP += 25;
}
On the first if, you use Hunger.fcurHP as a boolean (true/false), as you do in the second if, but then you use Hunger.fcurHP as an int so something is wrong there.
That's what I don't know how to do, how do I set up the boolean so that if I pick the water in the inventory only the thirst bar goes up? and vise versa if I pick the food then only the food bar goes up?
You're using the same variables as both booleans and integers, make them separate.
Answer by Hoeloe · Aug 26, 2013 at 09:25 AM
I'm not sure what those variables are. Is Thirst.fcurHP the current thirst level, and Hunger.fcurHP the hunger level? If so, you should learn about types. In most languages, variables can only be one type at once - and in this case, yours are an integer, or int. When you compare something to true or false, it has to be a boolean type. The problem you're having here is that you're using what's called an "implicit cast". You can convert variables from one type to another, and sometimes you can do so without explicitly stating that you want to. In this case, int can be implicitly converted to boolean by assuming 0 = false and any value greater than 0 is equal to true. Thus, your tests are fallacious. You need to set a different flag when you select the object, which determines which object you've just selected. The reason both bars go up is that you're actually testing the levels of the thirst and hunger, not what item you're using, which is what you need to do.
Typing is a very important aspect of any program$$anonymous$$g, so you should really have a solid understanding of it before you venture into any program$$anonymous$$g.
There's one here I found after a quick search for beginner typing tutorials, it might help: http://www.youtube.com/watch?v=VLBC5$$anonymous$$IZWN8
To further clarify from your 1st example, something like
if(item.name == "Water") Thirst.tcurHP += 25;
if(item.name == "S$$anonymous$$k") Hunger.fcurHP += 25;
or more likely something like if(item.type == "Drink") so that you don't have to explicitly name everything, but that leads into Enum, Lists, and a whole enchilada
Your answer
Follow this Question
Related Questions
How to make an inventory for my text advenutre? 0 Answers
How to set up a gui text box to count inventory items? 1 Answer
Equipting a Item/Weapon 1 Answer
Database error updating inventory 0 Answers