• 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 [SOLVED] I need help. I can't figure out what's wrong with my AI!

D

DJ Jub47

Guest
I have two kinds of enemies: shooters and chasers. I'm trying to write a script that lets the shooters shoot the chasers, the chasers get mad, they run after the shooter and kill it. But it gives me an error that says that my target variable is undefined, even if I reset the target to the player after the kill. Here's the code:
Chaser Step Event:
Code:
...if (point_distance(x,y,target.x,target.y)<=250 || target != obj_player){
    speed = 5;
}else{
    speed = 0;
}
Shooter Destroy Event:
Code:
if (instance_exists(obj_enemy2)){
    obj_enemy2.target = obj_player;
}...
Any help would be much appreciated! :D
 
M

Matt Hawkins

Guest
It looks like
if (instance_exists(obj_enemy2)){
obj_enemy2.target = obj_player;
}...
is causing the issue.
If you put target = 0; in the create code of obj_enemy2 it might fix it.
 

TheouAegis

Member
...Does the chaser ever have target defined in its create event? If target is an invalid reference, then it'll give errors. You should check if target is actually set to a valid ID first...
 
D

DJ Jub47

Guest
This is what the error says:

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

Unable to find any instance for object index '100002' name '<undefined>'
at gml_Object_obj_enemy2_StepNormalEvent_1 (line 5) - direction = point_direction(x,y,target.x,target.y);
##################################################
 

TheouAegis

Member
Is the target getting destroyed or deactivated? Checking if target even still exists first would fix this. It's like you saying you're going to eat the last fry, then your friend eats it before you - you still intend to eat the last fry, but now there is no last fry and your brain errors.
 

samspade

Member
I have two kinds of enemies: shooters and chasers. I'm trying to write a script that lets the shooters shoot the chasers, the chasers get mad, they run after the shooter and kill it. But it gives me an error that says that my target variable is undefined, even if I reset the target to the player after the kill. Here's the code:
Chaser Step Event:
Code:
...if (point_distance(x,y,target.x,target.y)<=250 || target != obj_player){
    speed = 5;
}else{
    speed = 0;
}
Shooter Destroy Event:
Code:
if (instance_exists(obj_enemy2)){
    obj_enemy2.target = obj_player;
}...
Any help would be much appreciated! :D
Couple thoughts. A little more code would help, but the most useful thing would probably be to show some debug messages or use the debugger to find out what target is being set to. Given that target is being set in the create event, it seems that most likely what is happening is that the target object is being destroyed and then be referenced before being reset. One quick solution might be to wrap the initial code in an if (instance_exists(target)) statement so it won't be called if the target doesn't exist; however, it's probably better to use the debugger or show debug message to figure out when it is being changed. You could use if instance_exists to help with that. For example:

Code:
if (!instance_exists(target)) {
    show_debug_message(string(target));
}
Put that in and probably put a debugging break there as well.
 
D

DJ Jub47

Guest
My GameMaker folder disappeared with everything in it, and the game is gone. I have a backup, but it's entirely different (don't know why, so don't ask). This issue is to be marked SOLVED. Thank you, @Matt Hawkins, @samspade, @TheouAegis, and @Bayesian for your help. You are all very much appreciated.
 
Top