GML (Minus) Basic maths

Discussion in 'Programming' started by Ellys, Aug 12, 2019.

  1. Ellys

    Ellys Member

    Joined:
    Aug 11, 2019
    Posts:
    6
    I want to take away 3 from an already set variable in a switch statement.

    switch (oCharacter.HPdiff){
    case 0:
    oCharacter.Gold = -3
    break;

    This essentially sets the characters gold to -3 instead of removing -3. Game maker doesn't like it if I set it out without the =
    oCharacter.Gold -3

    Any suggestions (yes i'm new to code)
    Thanks in advance.
     
  2. NightFrost

    NightFrost Member

    Joined:
    Jun 24, 2016
    Posts:
    1,865
    You take tha variable, substract an amount, and feed the result back to the variable.
    Code:
    oCharacter.Gold = oCharacter.Gold -3;
    
    GML supports a short form writing of this as:
    Code:
    oCharacter.Gold -= 3;
    
    Keep in mind, since you seem to be doing an object reference (oCharacter) every instance of that object in room will have their gold reduced by 3. If there's just one instance, as is typical with player character objects, then it doesn't matter as there's no need to specify an instance.
     
  3. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,317
    That's not what actually happens. It picks one instance of oCharacter, gets Gold from it, subtracts 3, then apply that result to the Gold variable in every instance of oCharacter. If the instances each started with different values, it won't be a proper subtraction by 3 for at least one of them.

    There's a reason why I personally forbid using object IDs to refer to specific instances in a multi-instance situation, even though it's valid in GML. Too many people just don't know the evaluation process well enough to follow through on the consequences.
     
  4. NightFrost

    NightFrost Member

    Joined:
    Jun 24, 2016
    Posts:
    1,865
    Ah yes, that happens. I pretty much never use the object referencing in that manner, so it seems I've started forgetting the basics.
     
  5. Ellys

    Ellys Member

    Joined:
    Aug 11, 2019
    Posts:
    6
    Ironically enough i ran into this problem before seeing your message and couldn't figure out why. If that's the case with altering a variable for every object in a room what is the alternative to this?
    I'm trying to have a constant gold throughout every room and altering this when needed, I assumed having this in the create tab of my character wouldn't be an issue. But since he is in every room this wouldn't work now? is there a way to use the scripts in some fashion?
     
  6. Ellys

    Ellys Member

    Joined:
    Aug 11, 2019
    Posts:
    6
    Ironically enough i ran into this problem before seeing your message and couldn't figure out why. If that's the case with altering a variable for every object in a room what is the alternative to this?
    I'm trying to have a constant gold throughout every room and altering this when needed, I assumed having this in the create tab of my character wouldn't be an issue. But since he is in every room this wouldn't work now? is there a way to use the scripts in some fashion?
     
  7. NightFrost

    NightFrost Member

    Joined:
    Jun 24, 2016
    Posts:
    1,865
    Yes, since your player character is not persistent and created in every room, its variables get set to whatever you supply in create event at every room start. If you need to persist information between rooms, simplest option is to set global variable at game start.
     

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