• 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!

GML How to check if two instances are at the same position

A

Andrew Lee Kirkman

Guest
I am currently using GML and the code I have looks like this:

if (obj_player_one.state = PLAYERSTATE.PLAYERDOWNED){
if (obj_player_one.x = obj_player_two.x && obj_player_one.y = obj_player_two.y){
if (reviveTimer >= 100){
obj_player_one.state = PLAYERSTATE.PLAYERPASSIVE;
obj_player_one.sprite_index = spr_idle;
reviveTimer = 0;
} else {
reviveTimer++;
}
}
}

This is supposed to work much like a revive system. The problem from this code is this line here,

if (obj_player_one.x = obj_player_two.x && obj_player_one.y = obj_player_two.y)

Is there any other way for me to check to see if both player one and two are at the same position or is there a syntax/logic error? Thanks!
 
A

Andrew Lee Kirkman

Guest
This is a script underneath a step event in a little if statement!

if (keyboard_check(ord("X"))){
PlayerRevive();
}
 

Tsa05

Member
Note that in GameMaker Studio (and many other programming languages), single equals (=) should be used for assigning values, while double equals (==) should be used for comparing equality. I think it's still working in the case of your code, but it's worthwhile to switch now, since it might not work in future updates:
= Used to assign a value to a variable. Note that this can also be used for comparing variables in GameMaker Studio 2 and you may see this in examples and other peoples codes. However, this is a legacy from old GameMaker versions and you should use the == operators for comparing and = for assigning
Also, do you really want to check whether they are in the same position? Your code checks whether the characters are in the absolutely exact same position as each other, which is often nearly impossible to achieve unless you've got additional code that guarantees all characters snap to a grid or something.

An easy way to make this work, if you want to revive when *near* another player, is to use something like point_distance().
For example:
Code:
if (obj_player_one.state = PLAYERSTATE.PLAYERDOWNED){
     if (point_distance( obj_player_one.x, obj_player_one.y,  obj_player_two.x, obj_player_two.y )   <   100 ){ 
          if (reviveTimer >= 100){
               obj_player_one.state = PLAYERSTATE.PLAYERPASSIVE;
               obj_player_one.sprite_index = spr_idle;
               reviveTimer = 0;
          } else {
               reviveTimer++;
          }
     }
}
In the above code, we check to see whether the distance between the points [player_one.x, player_one.y] and [player_two.x, player_two.y] are less than 100. If so, the code continues. You could change the range to any amount you wanted. This code would prevent the problem of needing to be in the exact same pixel as the fallen ally.
 
A

Andrew Lee Kirkman

Guest
That was actually the perfect answer and the code works exactly as I wish for now! The second issue I was having though was that, say I stopped holding x, reviveTimer does not reset to 0. How could I do that logically using this code?
 
Top