GameMaker [Solved] Testing on a 2d array for 0

S

Snayff

Guest
Evening all,

I am using an array for managing my velocity, like so:
currVelocity = [0,0] // x,y​

At times I want to test if one or both elements is at 0, i.e. we are not moving. However, it never returns 0, even when not moving. I am sure I am missing something simple but the debug was no help as the values don't show as numbers and I am not sure what else to test for.

Any advice would be greatly appreciated!

-Snayff
 
Last edited by a moderator:

Yal

🐧 *penguin noises*
GMC Elder
How about:
Code:
show_debug_message("Xspd:"+string(curVelocity[0]) + ", Yspd:"+string(curVelocity[1]))
Also, GM by default uses doubles for all numbers, or using their proper name, double precision floating point numbers. Floating point numbers has a bunch of quirks to them since they can't be represented with full precision, and to cut the chase this basically means equality comparisons are a bit iffy, and it's safer to check a range than a direct equality. So for instance check if the abs() of both components of the speed is smaller than 0.05, or something along those lines. The absolute value is always positive or zero - it's basically the number without its minus sign.
 
S

Snayff

Guest
Thanks for the reply Yal. I have similar in place already using draw_text, and they do show as 0. The biggest confusion is that my state machine moves to idle when testing for 0 but certain other things don't! I have to assume it is the way I am amending the speed.

A range worries me as issues could creep in later when relying on the assumption of no motion but I think you might be right and the float might be the culprit. I will test the range now and see if it plays nice!
 

Yal

🐧 *penguin noises*
GMC Elder
A range worries me as issues could creep in later when relying on the assumption of no motion
You could always set the speeds to exactly 0 when you've range-checked them, then you KNOW they're zero. (if the speed was in the range, it was already so low that setting them to zero shouldn't cause any problems and/or look weird)
 
S

Snayff

Guest
Very good shout, Yal.

Edit. Turns out I already am in my collision script. Hmmm.

Edit2. I was trying to test on both sides of the array in one go. Oops. The below works (though now kills my projectile on the first frame of creation!)
currVelocity[VECTOR2X] == 0 || currVelocity[VECTOR2Y] == 0
 
Last edited by a moderator:
Top