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

Drag And Drop Connecting two instances

Discussion in 'Programming' started by giraffeman210, Jul 23, 2019.

  1. giraffeman210

    giraffeman210 Member

    Joined:
    Sep 23, 2018
    Posts:
    81
    Right now I have a bar spawning at the top of an enemy that follows it. Here is my problem, when I create a second of the same instance the bars only follow the first one instead of each new one having its own bar. Is there a way for an instance to know which instance created it and only follow that one?

    I am using dnd and gms2.

    Thank you!
     
  2. TheSly

    TheSly Member

    Joined:
    Jan 16, 2017
    Posts:
    1,000
    What you need to do is record the instance id of the bar that each enemy has associated with it, say in a variable called bar_id. Then in the enemy code that moves the bar, ensure it only runs if the id of the bar is bar_id. That way you can limit the movement to only happen to it's own bar.

    When you create the bar instance, you can use the target section to set a variable to the id of the instance created.

    Like this, assuming you are creating the bar from within the enemy object.
    23-07-2019 11-47-37 AM.png
     
    Last edited: Jul 23, 2019
  3. giraffeman210

    giraffeman210 Member

    Joined:
    Sep 23, 2018
    Posts:
    81
    Thanks but I am still a little confused. If I am writing the variable in the target section won't each bar end up with the same id as they are spawned?
     
  4. giraffeman210

    giraffeman210 Member

    Joined:
    Sep 23, 2018
    Posts:
    81
    Ok I think I am starting to get what you are saying. Sorry I am a little slow on understanding all of this stuff. How would I get the instance id to store in a variable?
     
  5. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,689
    That's what the "Target" field is for, the variable name to store the instance ID into goes there. TheSly has it set to bar_id. When you update the bar's coordinates from the enemy's Step event, use the Jump to Point action with its Apply to: expression set to bar_id and the coordinates set to other.x and other.y.
     
    Sabnock and giraffeman210 like this.
  6. giraffeman210

    giraffeman210 Member

    Joined:
    Sep 23, 2018
    Posts:
    81
    Thank you! That makes sense and I got it working! Is there now a way for the bar to know the instance id of the enemy that created it?
     
  7. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,689
    Right after the Create Instance action that TheSly showed you, use the Assign Variable action to set bar_id.owner_id to id. Then the originating instance ID is accessible anywhere within the created obj_bar instance except for its Create event. It's a trivial variation of synchronizing coordinates.
     
    giraffeman210 and Sabnock like this.
  8. giraffeman210

    giraffeman210 Member

    Joined:
    Sep 23, 2018
    Posts:
    81
    So once this is set, if I want something touching the bar object to effect the synced object would I just use object_enemy and it would know which one?
     
  9. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,689
    You must learn the difference between an object and an instance. Whenever "which one?" becomes an open question because there are multiple instances of the object, you can't reference off an object ID like obj_enemy anymore, you must reference off a specific instance ID. You cannot meaningfully talk about "human's hair colour" in a room full of people, you can only talk about the hair colour of specific individuals (e.g. "Alice's hair colour").

    Finding the correct instance ID in a collision is covered in the linked article:
    Assuming that owner_id is set as described in post #7, a collision event with obj_bar would use other.owner_id to refer to the bar's owner, not obj_enemy. Accessing the owner's variables would be done like this:
    Code:
    other.owner_id.hp
    Not like this:
    Code:
    obj_enemy.hp
    This isn't difficult to understand, you just need to start letting things other than object IDs sit on the left side of a variable reference dot.
     

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