How do I create objects from an array of their names and the set their instance variables?

0 favourites
  • 6 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • I have an array of 20 object names.

    When I call a function called updateObjects, I need to loop through the array and create the objects based on the object names in the array.

    After each created object, I need to change an instance variable value of that object (I'm just assigning the loop index to the created object).

    How can I refer to the newly created object in order to change it's instance variable?

    Tagged:

  • Make a subevent under the one you create the objects and use "Pick Last Created"

  • Thanks for your reply!

    The difficulty with "pick last created" is that it requires me to select an object, but I don't know which of the 20 objects to pick.

    Even families don't seem to help in this situation, since the objects can be members of two different families.

    I considered adding them all to just one family, but then I would have to move all the instance variables from the two families into this one unified family, which in a complex game sounds like a total nightmare..

    I wish I could just do what I do in JavaScript and store a reference to the newly created object..

  • Objects can be added to multiple families. You shouldn't need to get rid of the old ones or consolidate variables.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • It's difficult to give an accurate answer in these types of cases as can vary depending on what you need to do:

    Example:

    How many families are we talking about?

    What do you do if an object is part of two families?

    If as you said you have only two families to deal with and 20 objects, then a quick idea could be:

    On your Array store both (Object Name & Family Type)

    Y = 0 -----------Store Object Name

    Y = 1 -----------Store Family Type

    Then to pick the correct family after creating the objects you could do a few else conditions checks.

    ---------------------------------------

    However, if you have many families then you will have to make a lot else conditions, so to keep it cleaner may be a better idea to create Functions to keep everything organized.

    Example:

    Put all the objects in one Family = "Family_List" so we can pick the last created to extract the UID to pass it on inside the Function that we need to call.

    You can keep your current family's.

    Function = Family1_Initialization

    Function = Family2_Initialization

    And so on...

    They will have a (Parameter = UID) so you can pass the last create UID to pick it again inside that function.

    Should look something like this:

    These ideas are just based on the current info you give.

  • Objects can be added to multiple families. You shouldn't need to get rid of the old ones or consolidate variables.

    Thanks for your responses!

    The problem with the new family would be that it would not "see" any of the instance variables of the other families (in the set action dialogue), so I couldn't change them.

    tarek2 I think I might need to go with your idea of storing the family type in the y-axis of the array. That seems like the least inconvenient option that would let me keep a lot of my existing code.

    Thanks!

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