# Families and family variables.

Get help using Construct 2

### » Wed Oct 31, 2012 6:02 pm

Having trouble getting my head around this one.

I have a spaceship, with 4 shields, aft/bow/port/starboard. I've put all of these into a family, with a family variable of health.

10 times per second, I want to check these shields, and if they're not at full strength, they recharge, if there's enough power to recharge them.

So my condition has 'Every 0.1 seconds. Shields - Strength < 100. Power > 1'

Then the action is to recharge each chosen shield and drain the power from the ship. It works in one respect - each shield does recharge. However, the power is only drained once, even if all 4 are recharging. What I assume it does is, if the conditions pass, it does the action once - so if 2 shields are recharging, it recharges each shield, but it still only does the power drain once because it's only one action.

So how do I get it to drained once seperately for each shield? I'd like to do them one by one, so that if for example, there's enough power to charge 1 up, but 2 require recharging, one will recharge but the 2nd won't.
B
26
S
6
G
4
Posts: 456
Reputation: 5,858

### » Thu Nov 01, 2012 2:47 am

There are many different things you could do. The most simple of which would be to target the shields individually instead of grouping them together.

Then you could have events for each shield.

Every tick
EnergyBank > 0
Shield1Energy < (fullamount)    ->Subtract 1 from EnergyBank
->Add 1 to shield1Energy

Shield2Energy < (fullamount)    ->Subtract 1 from EnergyBank
->Add 1 to shield2Energy

If you wanted to reuse your 1 shield object then you just have to select the individual instances of these objects to check for instead of Shield1, ..2 etc.

I hope that gives you some food for thought.

Edit: if your not looking for the easiest solution and want the shortest way of coding it instead, then play around with adding 'For each object' system command to what you currently have.GenkiGenga2012-11-01 02:54:01
B
139
S
27
G
7
Posts: 1,086
Reputation: 13,881

### » Thu Nov 01, 2012 5:01 am

Thanks, I couldn't really have individual events for each shield as it was more complex than just those calculations, so I needed to group them. After an evening of scratching my head, I finally worked it out. For anyone in the same position, the answer was indeed to use the 'For Each' command, although I had tried that earlier but couldn't work it out.

System : Every 0.1 seconds - No action
Shields : Strength < 100
(sub event)
System : For each shields - No action
(sub event)
If enough energy, recharge the shields.

The double sub-event solved it. Basically nothing happens until it gets through to the final event, and that event is specific to each individual shield, but now runs 4 times (or however shields need recharging) instead of just once, and recharging all shields in a single action.
B
26
S
6
G
4
Posts: 456
Reputation: 5,858