GameMaker: Studio

GameMaker: Studio

View Stats:
Using an object's index to set variables can possibly lead to troubles
So I found out something today.
obj_button.click_amount+=amount;
One would think that this is a harmless way to set the values of multiples buttons at once. However, there is an issue with doing it this way.
Lets say I have 5 instances of obj_button.
  • They all have different click_amounts. (5,10,15,20,25 respectively).
  • Then lets say you have another button (call it obj_upgrade or something). And it has an amount of 5.
  • When you click the upgrade button it runs the above code.
You would think that each instance of obj_button would add on to their click_amount 5 more. So their new values would be 10,15,20,25,30. But that is in correct. What actually happens is all the instances of obj_button copy the first instance of obj_button's click_amount and adds 5 to it. So all their click_amounts turn to 10.

The way I solved the problem was below.
with(obj_button){ click_amount+=other.amount; }