• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Windows [SOLVED] Having trouble getting the "==" equality to work

T

TimTim

Guest
Hi guys, Im trying to use the "==" to say if an object travelling across the screen hits or crosses the x position of another object, do something. It does not do anything. Im making a pong-based game and trying to say if the ball passes the opponent's line (player is fixed on the y axis so his x axis remains the same), something happens.

in the step event of obj_fire:

if obj_fire.x == obj_player2.x
{
global.player2hp -= 3
}

Everything is spelled correctly and there are no errors that come up. It just simply doesnt do anything. I tried this using ">=" and that actually changed the score (global.player2hp). The "==" does not.
Any help here would be appreciated.
 

Neptune

Member
Is the same as ( == ) -- if (my_string == "Cat") {}
Is equal to ( = ) -- if (obj_fire.x = obj_player2.x ) {}

Use the comparison operator for things like strings...
 

FrostyCat

Redemption Seeker
Hi guys, Im trying to use the "==" to say if an object travelling across the screen hits or crosses the x position of another object, do something. It does not do anything. Im making a pong-based game and trying to say if the ball passes the opponent's line (player is fixed on the y axis so his x axis remains the same), something happens.

in the step event of obj_fire:

if obj_fire.x == obj_player2.x
{
global.player2hp -= 3
}

Everything is spelled correctly and there are no errors that come up. It just simply doesnt do anything. I tried this using ">=" and that actually changed the score (global.player2hp). The "==" does not.
Any help here would be appreciated.
A lot of rookies have this flimsy habit of implementing "crossing-the-line" behaviours as == comparisons. This nonsense needs to stop.

The problem with this == approach is how easy it is to overstep the line and miss it altogether. For example, if the threshold is at 100 and you check for it using ==, starting at 0 and incrementing by 5 works but not starting at 0 and incrementing by 7. The same threshold checked with >= is fine both ways.

This gets even more problematic when the increments contain fractional components. Not only can the aforementioned overstepping still happen, but floating point arithmetic errors are also involved now. Even theoretically certain thresholds can sometimes be missed if you insist on ==.

The next time you need to implement similar patterns, stay away from ==. Use <= or >= and err on the side where the off amounts will likely lie.
 

Neptune

Member
The next time you need to implement similar patterns, stay away from ==. Use <= or >= and err on the side where the off amounts will likely lie.
Another good way to have a "safety margin" is to use absolute value.

if ( abs(obj_player2.x - obj_fire.x) < 5 ) { /*take damage */}
 

Sasha

Member
If obj_fire is some type of bullet, you can check if it's in a player's "hit range".


if point_distance(obj_fire.x, obj_fire.y, obj_player2.x, obj_player2.y) < 60 {
obj_player2.hp -= 3
}
 
T

TimTim

Guest
aahh okay, I understand now why "==" is a bad choice. Im very new at this so even making simple games can get confusing haha. Thanks very much for the replies, guys.
 
T

TimTim

Guest
I still can't figure out a good system for this. I just want a set amount of health reduced every time the object passes the player. Using ">=" or "<=" results in health loss in every step until it leaves the screen (obviously because Im in the step event). @Vether, your "safety margin" works great but only if there is one instance of obj_fire in the room. Im allowing multiple instances of obj_fire to exist at the same time which subtracts the health multiplied by the number of instances. Would you have a way of getting around that?
I know this seems trivial and I could probably just use Outside Room or something but I would really like for the health drop to happen right at the "battle line" (the player's x axis).
Thanks so much
 

jo-thijs

Member
I still can't figure out a good system for this. I just want a set amount of health reduced every time the object passes the player. Using ">=" or "<=" results in health loss in every step until it leaves the screen (obviously because Im in the step event). @Vether, your "safety margin" works great but only if there is one instance of obj_fire in the room. Im allowing multiple instances of obj_fire to exist at the same time which subtracts the health multiplied by the number of instances. Would you have a way of getting around that?
I know this seems trivial and I could probably just use Outside Room or something but I would really like for the health drop to happen right at the "battle line" (the player's x axis).
Thanks so much
In your safety margin approach, replace "obj_fire.x" with "x".

Personally, I would use ">=" and "<=" to detect passing and keep a variable inside obj_fire that is false if it hasn't dealt any damage yet and is set to true when it deals damage.
 
Top