Windows Find ID of creating instance by created instance

What's the best way for an instance/object to reference the instance that created it and then store that as a variable, but without doing it via checking for a collision or which one is nearest?

I have an enemy instance which creates an instance of a hit object and I want it to follow the enemy instance in the air and if it moves left or right. The moving bit is fine, but I keep running into problems tying to store the ID.

I've been using the usual ways to store instance ids such as -

var (enemyid) = instance_place( x , y , obj_enemy )

This doesn't work because in the game (and for this particular move) the enemy instance has no mask or bounding box (it's a deliberate gameplay choice).

var (enemyid) = instance_nearest( x , y , obj_enemy )

This works some of the time but can return an error quite a lot for some reason.

I even tried the var (enemyid) = instance_nearest( x , y , obj_enemy ) in the hit object's create event so that it would grab the nearest enemy instances ID and then I stored that in a permanent variable called EID (enemyid = EID) but that doesn't work either.

Surely there must be some way for an instance to check the id number of whatever other instance created it at the point of creating and then store that?

I know you can store the id of whatever instance is being created by the creating instance, using instance_create (there's a tongue twister) but what about the other way around?

Anyway any help would be great. I'm using GM:S 1.4, Win 7, and the code I'm trying to get to work is below.

In STEP event -
Code:
var (whocalled) = instance_position(x,y,obj_enemy)

// Follow Enemy in air

x = whocalled.x;
y = whocalled.y;
z = whocalled.z;
zspeed = whocalled.zspeed;
 

FrostyCat

Member
Use the return value of instance_create() to set whocalled, then you can use it in the Step event later.
Code:
with (instance_create(x, y, obj_follower)) {
  whocalled = other.id;
}
Equivalent alternative form without with:
Code:
var inst_follower = instance_create(x, y, obj_follower);
inst_follower.whocalled = id;
The Manual clearly talks about this behaviour, and I repeat that same fact in one of my articles:
Created or copied instance. instance_create() (GMS 1.x and legacy) / instance_create_layer() (GMS 2.x) and instance_copy() return the ID of the created or copied instance. NEVER use instance_nearest() to establish this relationship --- something else could be closer by.

Subsidiary instance(s). Same as created or copied instance. If the subsidiary instance needs to reference its creator, the creator should assign its instance ID to an instance variable in the subsidiary instance. Conversely, if the creator needs to reference its subsidiary (or subsidiaries), it must store the return value of instance_create() immediately. NEVER use instance_nearest() to establish or maintain this relationship --- being the closest does NOT imply being the most relevant, especially in close quarters.
 
Simple, just set the variable from the creator object when creating such instance.
Example:
Var inst;
Inst = instance_create(x,y,obj_hit_object);
Inst.creator = self();
 
Top