I find myself stuck again with a peculiar issue that I don't really understand.
I'm currently trying to implement a "weapon" into a shooter game (using it as a learning project).
Here is the code pointed by the error message. I'll explain what I'm trying to do right after.
if instance_exists(Player)
{
nbtarget=instance_number(ENEMY);
var i;
for (i=0;i<nbtarget;i++)
{
Target= instance_find(ENEMMY,i)
if Target != noone
{
if Target.size <3 {hit=Target}
}
if hit != noone
{
if point_distance(x,y,hit.x,hit.y)<startradius
{
repeat(2)
{draw_lightning(x, y, hit.x, hit.y,and other parameters)}
if damageTik = true
{
DMG = instance_create(hit.x,hit.y,Obj_dmg)
DMG.dmg = Current_dmg
}
}
}
}
}
damageTik= false;
}
else
{
instance_destroy()
}
So, the behavior I was looking for is a lightning that targets every enemy within a radius around the player, only if the enemy size is smaller than 3. Also, the targeted enemy takes a certain damage per second. Everything is working fine.
But, since I implemented that, when I test it, it works fine for a certain amount of time, and then I get this error:
FATAL ERROR in action number 2 of Draw Event for object lightning_weapon: Unable to find any instance for object index '103752' name '<undefined>' at gml_Object_lightning_weapon_DrawEvent_2 (line 15) -if point_distance(x,y,hit.x,hit.y)<startradius
From what I understand, it looks like the instance referred to in the variable hit
has been destroyed before running the code at line 15. But I'm checking if such instance exists just before (hit != noone)
, so how can this be? Any pointer will be appreciated.
I try to move the "instance_destroy()
" part of the enemy code out from the Step Event (of the enemy parent object), I tried to move it in the End Step Event, then in the Begin Step Event, I even tried to make a variable to tell the enemy he is dead and destroy it in the next step... Nothing fixed my issue.
Maybe the problem is not with that part of the code, but after half a day looking at this I can't find any new thing to try.
I also try to do the same with a ds_list
, but failed to make it work (only one enemy is hit by the lightning):
var target = ds_list_create();
with(ENEMY)
{
if (point_distance(PLAYER.x,PLAYER.y,ENEMY.x,ENEMY.y) < other.radius)
ds_list_add(target,ENEMY.id);
}
nbtarget = ds_list_size(target)
var i;
for (i=0;i<nbtarget;i++)
{
hit = ds_list_find_value(target,i)
if hit != noone
{
{draw_lightning(x, y, hit.x, hit.y,other parameters...)}
if fire = true
{
DMG = instance_create(hit.x,hit.y,ODamage)
DMG.dmg = dmg
}
}
}
fire = false;
If I manage to make one or the other to work, it's perfect.
hit
? Also, it would be a bit better if you could make some proper indentation to your first code, the brackets are looking a mess. \$\endgroup\$hit
is the problem, try usingtarget
instead. \$\endgroup\$if
in your first code block. \$\endgroup\$