Noob question about IF statement

Hey,

I am a noob and I am getting crazy because of simple "if" statment.

How come, below statement gets "true" more than once when I don't set global.playerdeath = 0 elsewhere?

If I put a counter inside of the brackets it continues to count even if global.playerdeath = 1. What am I doing wrong here?

if ((global.playerdeath = 0) and (global.playerhealth <= 0))
{
global.playerdeath = 1;
// Player explodes
}

I have also tried this:
if ((global.playerdeath == 0) && (global.playerhealth <= 0))
if (global.playerdeath == 0) and (global.playerhealth <= 0)

Any help on this is much appreciated! :)
 
Last edited:

Mert

Member
The best and most appropriate way to do that is;
Code:
if (global.playerdeath==0 && global.playerhealth<=0)
{
global.playerdeath = 1;
//Do stuff
}
The above code will only be executed once and not anymore.

Notice : Although Game Maker tolerates the usage of "=" in such cases, you must never and NEVER use it like that, not in just Game Maker but in any programming/scripting language. If you want to check if something is equal to something, you must use double "=="
 
The best and most appropriate way to do that is;
Code:
if (global.playerdeath==0 && global.playerhealth<=0)
{
global.playerdeath = 1;
//Do stuff
}
The above code will only be executed once and not anymore.

Notice : Although Game Maker tolerates the usage of "=" in such cases, you must never and NEVER use it like that, not in just Game Maker but in any programming/scripting language. If you want to check if something is equal to something, you must use double "=="
Thank you for you feedback, Mert! Noted, but even "if (global.playerdeath==0 && global.playerhealth<=0)" the code continues to execute.
As a test, I added global.score += global.score... and dumped global.score and global.playerdeath onscreen to check their values. When global.playerhealth reaches 0 global.playerdeath turns 1 as expected, but global.score continues to count ... it is so very odd ...
 
Last edited:
C

CROmartin

Guest
Hey @The_Real_Olla, you are not doing anything wrong with this line of code but probably somewhere else. There is also many factors that we must consider like where do you declare this two variables? Maybe you are just overriding them some how. It could be case that you recreating object and then because they are global variables it overrides them for every instance or maybe some other instance have affect on it. What actually happens in Player explosion destroyed? If you don't destroy instance timer wont stop, you must then put it under if statement (if global.playerdeath != 0 {timer -= 1}). Check carefully your code, you are probably in some line of code accidentally overriding it. Have in mind that there can only be one global.variable and that changing its value in other object will have impact on every other object. I hope you will find what is bugging you.
 
Thank you for you feedback, Mert! Noted, but even "if (global.playerdeath==0 && global.playerhealth<=0)" the code continues to execute.
As a test, I added global.score += global.score... and dumped global.score and global.playerdeath onscreen to check their values. When global.playerhealth reaches 0 global.playerdeath turns 1 as expected, but global.score continues to count ... it is so very odd ...
I found the problem. I had decleared global.playerdeath under "step" instead of under "init" .. meaning that global.playerdeath would turn 0 for one frame before it went back to be set to 1 again... Silly me! :)

Thanks for your replies guys, very helpful!
 
Top