I’ve ran into an interesting problem. I have some code inside an update(dt) function. The code is pretty simple, it looks like this:
if(this.condition){
console.log("true branch is running");
} else {
console.log("false branch is running");
}
I have tried using condition == true and condition === true and condition == 1 as well as condition === 1.
Printing the variable into the console, shows ‘1’ for it’s value. If the condition is false, only the else branch is running. If the condition is true, then both branches are running.
Anyone has an idea what the hell is going on here?
You will need to share your project for someone to debug it as even if this was the output in console it is possible that the program is giving the right output and the problem lies somewhere else. As its getting changed each farm or some kind of other weird thing is happening.
Yeah that’s what I started with. But again as stated in one of my replies above, explicitly stating the condition on the else part via else if, fixed the issue completely.
It does not fix the issue, but makes a workaround about the real issue. It will break later.
A number one evaluates to true in if... else... statements. Zero evaluates to false. If your if.. else.. turns around like that, it only means that the value this.condition gets a different value each frame, evaluating to true and false. You must debug and find the root cause of what is assigning an invalid value to it, rather than allowing an invalid value to be assigned and later just ignoring it - it will give you bugs that are very hard to debug later.
The update function had a part, that floats the object back to it’s original location, and turns the whole floating stuff off. The issue was that the getLocalPosition() and getPosition() reported different values, and one of the functions was not updated to use getPosition() instead of the local, hence it kept turning back on to true.
I still wonder why the log always printed true for the variable and never false.