How do I create a new instance and transfer the parent's variables

0 favourites
  • 7 posts
From the Asset Store
Kids Game
$49 USD
New Sounds Added Update: 115 new sound effects added for no additional cost!
  • I'm making a simulator where creatures can mutate and evolve over time. When the parent makes a baby, I want to copy the parent's variables (movement speed, jump force, size, etc) to the child, but with some mutation.

    Basically, I want to do something like that:

    • Set child's speed to parent.speed*random(-0.1, 0.1)
    • Set child's jumpForce to parent.jumpForce*random(-0.1, 0.1)
    • etc....

    But obviously, because of the way construct picks objects, either I pick only the child and I can't "see" the parent's variables, or I pick both the parent and the child, but I will then affect the instance variables of both, which I don't want.

    Possible solution I can think of:

    • I create a local variable for each instance variable, copy the parent's variables to these local vars, then pick the child and copy the local vars to the child's var
    • Create an array and do the same thing as ↑
    • I create a family called "parent" and use that to talk to the parent (as the family) and the child (as the object itself) at the same time

    Is there other easier ways that I can't think of?

  • Family is often the easiest way to work with two different instances of the same object in one event. However, since you need to copy instance variables, this means that you'll have to define those variables on the family level.

    If you don't want to do this, then you'll have to use a bunch of temporary local variables..

  • Is there other easier ways that I can't think of?

    Maybe i understand this wrong, but if you have the IID of the parent and the child you could do this for example:

    System| Pick Sprite Instance (IID of Child) -> Sprite| Set variable to Sprite(IID of Parent).variable

  • (removed, I was wrong)

  • Maybe I understand it all wrong, but test it yourself :)

    drive.google.com/uc

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • No, sorry, you are right and I was wrong.. Wow, I always thought IID depends on the picked scope.

  • Whaaaaat?!!!! :O

    I've been using Construct 2 since 2013 and only today do I learn that I can pick an object through expression using their IID!!!!

    This is so powerful!

    • set variable parentID to critter.IID
    • create object critter
    • set critter's speed to critter(parentID).speed

    mind explode...

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