GML (Minus) Basic maths

E

Ellys

Guest
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.
 

NightFrost

Member
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.
 

FrostyCat

Redemption Seeker
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.
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.
 

NightFrost

Member
Ah yes, that happens. I pretty much never use the object referencing in that manner, so it seems I've started forgetting the basics.
 
E

Ellys

Guest
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.
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?
 
E

Ellys

Guest
Ah yes, that happens. I pretty much never use the object referencing in that manner, so it seems I've started forgetting the basics.
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?
 

NightFrost

Member
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.
 
Top