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

Legacy GM Fatal Error when last enemy is killed

D

Dieter

Guest
Hi, I'm receiving this weird fatal error when the last enemy is destroyed. I understand why the error is happening: there are no enemies anymore so it cannot find the enemy_parent object and crashes. However, this is why I put in a check first, to see if enemy_parent exists. For some reason, despite the check the code still runs. Does anyone have a clue why the code still runs? Many thanks in advance! I put the code and error message below:


if object_exists(obj_enemy_parent){

enemytarget = instance_nearest(x, y, obj_enemy_parent);
if canshoot {
alarm[2] = room_speed / 4;

var CanSee = !collision_line(x, y, enemytarget.x, enemytarget.y, obj_wall, false, true);
var IsInsideRadius = distance_to_point(enemytarget.x, enemytarget.y) < WeaponRange;

if(CanSee and IsInsideRadius){
scr_get_face(dirattack);
enemy_in_vision = true;
show_debug_message("Enemy Within Range");
var p = instance_create(x, y, obj_projectile);
dirattack = point_direction(obj_player.x,obj_player.y, enemytarget.x, enemytarget.y);
var xforce = lengthdir_x(20, dirattack);
var yforce = lengthdir_y(20, dirattack);
p.creator = id;
with (p) {
physics_apply_impulse(x, y, xforce, yforce);
}

} else {
enemy_in_vision = false;
}
canshoot = false;
}
}




############################################################################################
FATAL ERROR in
action number 1
of Step Event0
for object obj_player:

Variable <unknown_object>.y(1, -2147483648) not set before reading it.
at gml_Object_obj_player_StepNormalEvent_1 (line 17) - var CanSee = !collision_line(x, y, enemytarget.x, enemytarget.y, obj_wall, false, true);
##########################################################################
 

Alexx

Member
It would appear you are trying to get the x and y values of a non-existent object, ie there are no instances of enemytarget present.

Perform a check that an instance of this object exists before trying to reference it.
 
Last edited:

Bingdom

Googledom
object_exists does this:
This function returns whether an object with the specified index exists or not. Note that this checks to see if an object is present in the resource tree, and not actually in the game room. For that you should use the function instance_exists.

use instance_exists instead
 
D

Dieter

Guest
Tnx Bingdom, that fixed it! I completely misunderstood object_exists, I thought it meant what instance_exists does. Cheers!
 
Top