SOLVED IF expression ignored using global variable even if false

Axl Trauts

Member
Hi,

I am having a weird issue: When checking if the shark object player object is away from the player (to charge if closer), the IF statement runs even if the statement is false. The original relevant part of the Step event code on the Shark object is this:

GML:
var dista = point_distance(x,y,x,global.var_player_y);

if (dista <= 300 && locked == 0)
{    locked = 1;
    speed = 5;
}
Note that the Player's Y variable is stored on a global variable on a persistent object. I checked, it ALWAYS shows the same value as Y. However, the IF statement runs even if the expression is actually false. So I tried just using the Y variable and somehow it works. I checked drawing the player's Y variable, the global variable, the expression shows 0 if distance > 300 (Of course variable dista wast not local for the draw_text function to work). See attached files.

GML:
var dista = point_distance(x,y,x,obj_player_p1.y);

if (dista <= 300 && locked == 0)
{    locked = 1;
    speed = 5;
Why? I am not sure. I solved the issue but this bugs me.

This makes me wonder if IF expressions might have some issues checking global variables.
 

Attachments

chamaeleon

Member
This makes me wonder if IF expressions might have some issues checking global variables.
Lets just assume there is no problem whatsoever with if(), and lets instead put a breakpoint on the statement or use show_debug_message() to inspect what the values are, rather than relying on some other output later on?
 

Axl Trauts

Member
This would be a good oportunity to learn about debugging. Is this how it works? see attached.

dista = point_distance(x,y,x,global.var_player_y);

if (dista <= 300 && locked == 0)
{ show_debug_message(global.var_player_y);
show_debug_message(obj_player_p1.y);
show_debug_message(dista);
locked = 1;
speed = 5;
 

Attachments

chamaeleon

Member
Yes, more or less. Although I would include strings in the show_debug_message() to indicate what the number is..
GML:
show_debug_message("global.var_player_y = " + string(global.var_player_y));
show_debug_message("obj_player_p1.y = " + string(obj_player_p1.y));
show_debug_message("dista = " + string(dista)); 
show_debug_message("locked = " + string(locked));
Anyway, when you look at the numbers displayed using this, I'm fairly sure you'll find they are objectively such that the if statement is true.
 

chamaeleon

Member
I now attach two debugs using the Y variable and the Global variable with the stored Y. I moved those functions before the IF. Still no clue why it is happening, IF should be returning 0 on both scenarios.
Now you have removed evidence that you are inside the if statement when dista is greater than 300 because you don't have any show_debug_message() inside it.
 

Nidoking

Member
Have you saved the project and restarted Game Maker since this started happening? This happened to me once - the debugger showed the program going into an if block when the conditions were false, but when I tried to create a simpler project to replicate the issue, I couldn't reproduce it. The original game also didn't have the problem anymore.
 

Axl Trauts

Member
I did cleaned, closed and run it again. Same result.
By the way the global.var_player_y is started on the player object as 0 and on its step is assigned as the y value.
SO... I changed the 0 value as the Y value. Voilá. As the create event goes first, this happens. Thanks for your insight and help.
 
Top