GML Unknown variable x when used with distance_to_object

M

Maladroit

Guest
Hello, I've been using Game Maker for several months now, and I'm using Game Maker 8.0 Lite. One problem that I've been consistently having is the "unknown variable x" error. I have 21 total objects, and I'm not merging games, so I don't think that the problem comes from issues with Game Maker confusing object indices and instance id's. The aspect of the game that this error concerns is an assistant object following the player object. This is the code within the assistant object's Step Event:

//Follow the Player
if global.pause == 0 {
if (keyboard_check_pressed(ord('L')) or distance_to_point(object_player.x + 20, object_player.y - 20) > 700) {
position_change(object_player.x, object_player.y, object_assistant, 0)
}
if (distance_to_point(object_player.x + 20, object_player.y - 20) < 50) { //this is the error line
hsp /= 1.3;
vsp /= 1.3;

} else {
hsp -= 0.001 * (x - object_player.x);
vsp -= 0.001 * (y - object_player.y);
}

When I run the game, I receive an error as such:

ERROR in
action number 1
of Step Event
for object object_assistant:
Error in code at line 9:
if (distance_to_point(object_player.x + 20, object_player.y - 20) < 50) {
^
at position 42: Unknown variable x

If I forgot to mention anything or if I wasn't specific enough about something, please tell me (sorry in advance). Thanks.
 

Simon Gust

Member
your object_player might not exist at that time.
Add a check
Code:
if (instance_exists(object_player))
{
   // rest of the code
}
 
M

Maladroit

Guest
I figured out the problem, and it turns out that the problem had to do with the position_change command I was using. When the assistant was at least 700 pixels away from the player, it was supposed to change position to the player with the position_change command, but I changed it to basically this:
if (keyboard_check_pressed(ord('L')) or distance_to_point(object_player.x + 20, object_player.y - 40) > 700) {
with object_assistant {
x = object_player.x;
y = object_player.y - 40;
hsp = 0;
vsp = 0;
}
}
It works perfectly now. Thank you! *v*
 
Top