help with families and instance variable

Get help using Construct 2

Post » Sat Oct 27, 2012 11:45 pm

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!


https://www.dropbox.com/s/liwaob2e8w9xwni/privateVariable.capxYung2012-10-27 23:47:57
B
27
S
9
G
5
Posts: 108
Reputation: 5,744

Post » Mon Oct 29, 2012 7:01 pm

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.

keepee2012-10-29 19:01:54
B
28
S
8
G
1
Posts: 469
Reputation: 4,683

Post » Mon Oct 29, 2012 11:18 pm

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.
B
27
S
9
G
5
Posts: 108
Reputation: 5,744

Post » Tue Oct 30, 2012 12:01 am

You must have missed something..
It won't keep adding if it gets reset back to 0 at the start of each tick.
B
28
S
8
G
1
Posts: 469
Reputation: 4,683

Post » Tue Oct 30, 2012 12:08 am

I tried it exactly as Keepee said and it worked perfect.
B
27
S
8
G
5
Posts: 429
Reputation: 6,902

Post » Tue Oct 30, 2012 4:29 pm

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!
B
27
S
9
G
5
Posts: 108
Reputation: 5,744

Post » Tue Oct 30, 2012 4:35 pm

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?
B
27
S
9
G
5
Posts: 108
Reputation: 5,744

Post » Tue Oct 30, 2012 7:25 pm

You could make 'totalHealth' a local variable and it would be reset each tick. A static variable keeps it's value.
B
54
S
28
G
18
Posts: 1,520
Reputation: 25,020

Post » Tue Oct 30, 2012 7:50 pm

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!
B
27
S
9
G
5
Posts: 108
Reputation: 5,744


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 1 guest