Noob question about IF statement

Discussion in 'Programming' started by The_Real_Olla, Mar 17, 2019.

  1. The_Real_Olla

    The_Real_Olla Member

    Joined:
    Jan 5, 2019
    Posts:
    7
    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: Mar 17, 2019
  2. Mert

    Mert Member

    Joined:
    Jul 20, 2016
    Posts:
    212
    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_Real_Olla and LV154 like this.
  3. The_Real_Olla

    The_Real_Olla Member

    Joined:
    Jan 5, 2019
    Posts:
    7
    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: Mar 17, 2019
  4. CROmartin

    CROmartin Member

    Joined:
    Jul 17, 2016
    Posts:
    80
    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.
     
    The_Real_Olla likes this.
  5. The_Real_Olla

    The_Real_Olla Member

    Joined:
    Jan 5, 2019
    Posts:
    7
    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!
     
    Mert likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice