0
\$\begingroup\$

I'm currently trying to make the fire object (created by the fireball) stick to the enemy, by specifying the enemy type and its id. I'm getting the following error:

"Unable to find any instance for object index '-4' name undefined". Here's my fireball [collision with enemy] code:

var sp=other.speed
var dir=other.direction
var oi=other.id
var on=object_get_name(other)
with (instance_create(other.x-lengthdir_x(new_push_amount, pd),other.y-lengthdir_y(new_push_amount, pd),fire))
{
speed = sp
direction = dir
enemyid = oi
enemyname=on
}

And here is the Create,

enemyname = ""
enemyid = 0

and the Step events of fireball:

speed = instance_find(enemyname,enemyid).speed
direction = instance_find(enemyname,enemyid).direction

Maybe I'm using the wrong functional to get the instance... In any case, help would be really appreciated!

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

First thing. object_get_name needs object index, not instance index, like this:

var on = object_get_name(other.object_index); // or maybe (other.id).object_index, I don't have ability for check it now

Second. You must check what instance_find returned. Because instance may not exists.

var obj = instance_find(enemyname, enemyid);
if instance_exists(obj)
{
    speed = obj.speed;
    direction = obj.direction;
}

Third. instance_find needs object_index, not an object's name (which is a string) and index (which is not equal id).

I don't understand why you don't use just id of the instance.

var spd = other.speed;
var dir = other.direction;
var inst_id = other.id;

with instance_create(other.x-lengthdir_x(new_push_amount, pd), other.y-lengthdir_y(new_push_amount, pd), fire)
{
    speed = spd;
    direction = dir;
    enemyid = inst_id;
}

Or same, but less lines:

var obj = instance_create(other.x-lengthdir_x(new_push_amount, pd), other.y-lengthdir_y(new_push_amount, pd), fire);
obj.speed = other.speed;
obj.direction = other.direction;
obj.enemyid = other.id;

And Step End event (not Step, otherwise there will be a lag):

if instance_exists(enemyid)
{
    speed = enemyid.speed;
    direction = enemyid.direction;
}
else
{
    instance_destroy(); // ? I don't know, is need you it or not
    // but anyway don't forget to destroy object
}
\$\endgroup\$
1
  • \$\begingroup\$ Thanks, this worked! I took the time and got into it, it's been long since I last used Game Maker. Also, sorry for the long response. \$\endgroup\$ Feb 4, 2017 at 22:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .