How to assign one instance to another?

0 favourites
  • 6 posts
From the Asset Store
Footsteps SFX One contains 400 sounds of steps and jumps on different surfaces.
  • Hello everyone,

    I'm sorry for the trivial question, but I'm new to Construct, and I'm learning how to use it.

    I would like to understand how to pass for example the UID of an instance to another specific instance.

    If I spawn an instance through an event of a specific instance, how can I immediately assign the value of a variable to the spawned instance?

    Thanks!

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I would like to understand how to pass for example the UID of an instance to another specific instance.

    The UID is unique ID, each instance will have different and unique, you can't pass to another.

    If I spawn an instance through an event of a specific instance, how can I immediately assign the value of a variable to the spawned instance?

    You can put right after you spawn an instance, like spawn enemy then set Enemy.HP = 100.

  • > I would like to understand how to pass for example the UID of an instance to another specific instance.

    The UID is unique ID, each instance will have different and unique, you can't pass to another.

    > If I spawn an instance through an event of a specific instance, how can I immediately assign the value of a variable to the spawned instance?

    You can put right after you spawn an instance, like spawn enemy then set Enemy.HP = 100.

    Thank you for your response.

    Maybe I misunderstood myself in my post. What I want to pass as a reference is just the UID of the "parent instance" so that I have a unique reference of an instance.

    By storing the UID of an instance inside a variable of another instance, it will be easy for me to pass other values to that instance in the future since I have its unique id as a reference.

    For this reason, when I generate a new instance, I wanted to save immediately in a variable the UID of the main instance.

    Is my reasoning wrong?

  • As Maverick said you can pass the UID of the "parent" object to an instance variable on the "child" object at the time of the child being spawned by adding the action directly under the spawn action.

    Parent - Spawn Child

    Child - Set var_parentUID to Parent.UID

    You could also use the hierarchy system, which allows you to add and remove children to/from parent objects and pick them all in various ways.

    construct.net/en/make-games/manuals/construct-3/plugin-reference/common-features/common-actions (look at the hierarchy section)

  • Thank you so much for your response. I followed your suggestions, and this is the result (which seems to work correctly):

    If you have any suggestions to improve this code, please tell me. :)

    I understand, that if I create an instance in an event, and then I want to assign a value to that instance with an action (within that event), Construct automatically selects that instance, and not all instances of the same object. Is this correct?

    If in the same event I have to create more instances of the same object, how can I manage the situation if I want to assign a specific value to each specific instance? Is it possible to loop through all newly created instances within an event? Or do I have to create a specific function?

    Sorry for the thousand questions, but I am very determined to learn how to use this program.

  • If the position of the spawn is the same each time you can create an image point on your sprite and then you can specify that image point in the spawn action so you wouldn't need the XandY actions below.

    Correct about the instance picking yes.

    If you need to spawn multiple text objects you can use a repeat loop like you mentioned.

    Just add a sub event under the on sprite created condition and add a repeat loop.

    If the number of repeats is not constant you could populate the repeat number by passing a variable from somewhere else.

    If you need to pass different values to each of the instances created in the loop then you would also need to pass those via some other function.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)