GMS 2 [SOLVED] Variable adding too many times in one step

Divinik

Member
So I have a variable (shots_fired) that counts how many time a gun is fired, but for some reason it will continuously add to the variable, even with a condition check:

Code:
if parent.image_index >= 1
     {
      image_angle = 10;
      x = parent.bbox_left+((parent.bbox_right-parent.bbox_left))*0.93;
      y = parent.bbox_top+((parent.bbox_bottom-parent.bbox_top))*0.19;
      if take_shot = true
        {
        shots_fired++;
        take_shot = false;
        }
    }
It will add into the variable until the animation stops. Any ideas why this might be happening? the code is part of script in the step event of all actor's active in battle mode.
 
Try changing this:
Code:
if take_shot = true
to be
Code:
if take_shot == true
and see if that makes any difference. If it doesn't, do you only have a single instance in the room that has this code in it? Perhaps add after the shots_fired++; line adding in a show_debug_message with the instance id just so you can see whether it is the same instance that is running this code.
Another thing to check is where you reset the take_shot value to true again, as it does not appear to be in this section of the code you are showing. Maybe that is getting set back instantaneously and therefore it thinks it should be running that bit of code again the next step.
 

Divinik

Member
Try changing this:
Code:
if take_shot = true
to be
Code:
if take_shot == true
and see if that makes any difference. If it doesn't, do you only have a single instance in the room that has this code in it? Perhaps add after the shots_fired++; line adding in a show_debug_message with the instance id just so you can see whether it is the same instance that is running this code.
Another thing to check is where you reset the take_shot value to true again, as it does not appear to be in this section of the code you are showing. Maybe that is getting set back instantaneously and therefore it thinks it should be running that bit of code again the next step.
I figured out the problem, since it runs on a loop if the total shots fired is not equal to the shots supposed to be fired, it goes back to the previous sequence, and I forgot to reset the image index of the actor sprite.
 

ParodyKnaveBob

The Laughing Rogue
Glad you solved it. $:^ ] (Don't forget to edit your first post to add "solved" to the title!)

On two unrelated notes:
1. Lol @ shots_fired for your Boolean variable name, I love it. $X^ b
2. I wonder, reading this...

Code:
      image_angle = 10;
      x = parent.bbox_left+((parent.bbox_right-parent.bbox_left))*0.93;
      y = parent.bbox_top+((parent.bbox_bottom-parent.bbox_top))*0.19;
...do you need the x,y coordinates to stay in the upper left? If you changed your sprite origin to the center, then you could do away with offsetting the x,y fractionally every time it tilts, resets, etc.:

Code:
      image_angle = 10;
...would be all you'd need, other than a (hopefully small?) redesign for centered x,y instance creation.

Just a suggestion. Regards!
 
Top