1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

Windows Find ID of creating instance by created instance

Discussion in 'Programming' started by Krenzathal77, Oct 25, 2018.

  1. Krenzathal77

    Krenzathal77 Member

    Apr 19, 2017
    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 -
    var (whocalled) = instance_position(x,y,obj_enemy)
    // Follow Enemy in air
    x = whocalled.x;
    y = whocalled.y;
    z = whocalled.z;
    zspeed = whocalled.zspeed;
  2. FrostyCat

    FrostyCat Member

    Jun 26, 2016
    Use the return value of instance_create() to set whocalled, then you can use it in the Step event later.
    with (instance_create(x, y, obj_follower)) {
      whocalled = other.id;
    Equivalent alternative form without with:
    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:
    rytan451 and Alix like this.
  3. TinyGamesLab

    TinyGamesLab Member

    Jun 21, 2018
    Simple, just set the variable from the creator object when creating such instance.
    Var inst;
    Inst = instance_create(x,y,obj_hit_object);
    Inst.creator = self();
    rytan451 likes this.
  4. Krenzathal77

    Krenzathal77 Member

    Apr 19, 2017
    Brilliant, just what I'm looking for. Thanks!

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice