help with families and instance variable

0 favourites
  • 9 posts
From the Asset Store
Game with complete Source-Code (Construct 3 / .c3p) + HTML5 Exported.
  • Hi guys,

    I am facing a problem with instance variable, with families. Basically I have a bunch of sprites, and each of them will inherit a family variable, in this case, Health.

    What I want to do is then for each instance of the sprite under this family, I would like to customize their health variable, then add them ALL into another family variable called Totalhealth.

    For some reason I can't do this without specifically doing it one by one. Is there a smarter way to do this?

    I have provided a screenshot as well as the capx.

    Thanks alot!

    <img src="" border="0">

  • Not sure why this thread hasn't been replied to

    it seems pretty simple..

    "TotalHealth" sounds like it should be a Global Variable, as it won't actually differ between any of the instances...

    +every tick

         ->set TotalHealth to 0

    +For each Family1

         ->Add Family1.Health to TotalHealth

    This should work.

  • keepee: thanks alot for the suggestion! I actually tried that, but it seems to go into an infinite loop and kept adding to it continously.

  • You must have missed something..

    It won't keep adding if it gets reset back to 0 at the start of each tick.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I tried it exactly as Keepee said and it worked perfect.

  • Yeah it worked! Definitely a mistake on my side.

    The text was supposed to sit AFTER these two events, if not I'll get the infinite loop.

    Thanks alot! This will be a great timesaver!

  • One perhaps stupid question, why is this bit at the first needed?

    -> every tick

       ->set totalHealth to 0

    It seems like if it' not there, the value will do infinite loop. Wasn't the static variable implemented and were meant for this?

  • You could make 'totalHealth' a local variable and it would be reset each tick. A static variable keeps it's value.

  • ramones: Thanks for the suggestion! After messing around with it, I finally managed to get the local variables to be static!

    Thanks a lot everyone!

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