- Home /
how to check only once when a boolean is always true on update. it keeps looping
hey I've got a C# script that I can't seem to make a simple logic. I want it to check only once because this variable is always true making it loop again and again. I want it to check only once so that it will work just once even though the variable is always true. so basically what i need is a simple logic script.
here's a simple script on my dilema to give you an idea on what my problem is
private bool hold;
private bool jumpOnlyOnce;
Update()
{
if(hold == true)//when I Hold, it will return to true every frame
{
if(jumpOnlyOnce == true)
{
jumpOnlyOnce = false;//when its true it will jump
}
else
jumpOnlyOnce = true;
}
}
// i actually got this logic from a Pause Button, the Difference
//is. a Pause Button only gets push once so its not going through
//every frame that's why this wont work since this script is hold
//which is true until its let go. I want it to work only once
//even though hold is always true.
The code you've written sets the jumpOnlyOnce variable to false but resets it to true during the next Update() loop. This is what creates the looping problem. Try removing the line that resets the jumpOnlyOnce variable to true. Set that variable to true when the hold variable is false
Answer by Eno-Khaon · Aug 17, 2015 at 05:18 AM
So, if I understand right, you're asking if there's a way to automatically assume that the boolean value is "true" after the first time so it doesn't have to keep checking?
Sorry to tell you this, but that's COMPLETELY unnecessary. Also, there aren't really good ways of doing that.
A check of whether a boolean is true or false is one of the fastest tests you can perform in programming. In general, when you're worried about squeezing out minute fragments of performance, you convert things INTO booleans for a faster comparison. For example, you might compare floats and store the result as a bool to call up more quickly.
But for a once-per-frame test, you'll need a few billion instances of that script before you would even possibly notice any performance impact from testing whether it's true or false.
However, what you would more likely notice first is that the Update() functions themselves, let alone billions of instantiated objects and their object and transform data, would be taking a far greater toll on your performance than a single boolean would.
i believe he wants it to not check every frame so that the jumps wont be flipping back and forth every frame. think super mario. hold jump for full height, but tap it for a short jump. if he lets it loop as is he would only ever get a short jump.
Answer by etopsirhc · Aug 17, 2015 at 05:20 AM
what you want to do is to check for a change in the variable
if (hold == true && lastHold == false){
// do your jump logic
lastHold = hold;
}else if(lastHold == true && hold == false){
lastHold = hold;
}
this will only trigger when hold and lastHold are not the same, then before each part closes lastHold gets the value of hold to keep them the same any time you dont want them to run.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Nested Array? Populating a menu using a loop. 0 Answers
GameObject tag to if condition 1 Answer