Families and Objects' private instance variable

Get help using Construct 2

Post » Wed Sep 12, 2012 9:21 am

Hello everyone!
I read this two pages froms Scirra's site
Families
Beginners Guide - P6
And I'm trying to merge the two tips.

My goal: I'd like to have enemies with different life points but manage the life points subraction with the families system (just to avoid writing the same line so many times as the number of enemies).

I'm facing this issue:
=> if I use a family Instance Variable, the value for each enemy can't be different as it could be with private instance variables.
=> if I don't use family Instance Variable, I cannot access the Object instance variable while I'm verifying the collision of a bullet, for example, with the "enemies" family. I can only verify the collision and destroy th Object (as shown in the manual). But If I just want to subtract some life for each collided bullet?

Can someone help me, please?
I couldn't find any answer in Scirra's pages.
Thank you in advance
B
21
S
5
G
5
Posts: 16
Reputation: 5,421

Post » Wed Sep 12, 2012 10:56 pm

[QUOTE=HatsuSakura]
=> if I use a family Instance Variable, the value for each enemy can't be different as it could be with private instance variables.
[/QUOTE]

It can be. You can do
[code]
+Bullet: On collision with Enemies
   -> Enemies: Subtract 1 from Health

+Enemies: Health = 0
   -> Enemies: Destroy
[/code]
where 'Enemies' is a family.
B
54
S
28
G
18
Posts: 1,520
Reputation: 25,020

Post » Wed Sep 12, 2012 11:02 pm

[QUOTE=ramones] [QUOTE=HatsuSakura]
=> if I use a family Instance Variable, the value for each enemy can't be different as it could be with private instance variables.
[/QUOTE]

It can be. You can do
[code]
+Bullet: On collision with Enemies
   -> Enemies: Subtract 1 from Health

+Enemies: Health = 0
   -> Enemies: Destroy
[/code]
where 'Enemies' is a family.[/QUOTE]

Also, when you use an instance variable for the family, it only have the purpose to make easy your job by sharing the same instance name with all the objects inside, but each individual instance of the family will manage their instance variables individually (or not, if you set to all of them change the variable)
ImageImageImageImageImageImage
B
93
S
20
G
13
Posts: 1,215
Reputation: 18,990

Post » Fri Sep 14, 2012 7:27 am

Thanks to @ramones and @TELLES0808 for your replies.
In the way you suggested me it seems I have to set the value Health for each enemy everytime it's spawned, because it cannot be assigned a priori.

I would have liked that families' IV overrided the single object's IV so I could declare private IV this way:
enemy -> health = 1
bad_enemy -> health = 3
verybad_enemy -> health = 5
motherf####r_enemy -> health = 10
boss -> health = 50

and then apply a rule like

+Bullet: On collision with Enemies
   -> Enemies: Subtract 1 from Health
+Enemies: Health = 0
   -> Enemies: Destroy

But I understood it's not so...

Now, if I declare a family's IV Health = X, X will be the value for every health of every enemy. For sure indipendent for each enemy, but with the same start value and this is what I would like to avoid: the same start value for each enemy.
I suppose I can make a workaround assigning the Health value after spawning the enemy, but it's not an elegant solution: it's hardcoded and I prefer to modify a parameter of an object rather than a code line, if I want to make a change.
Any suggestion?
Or my workaround is the only way?

Thank you all in advance
B
21
S
5
G
5
Posts: 16
Reputation: 5,421

Post » Fri Sep 14, 2012 11:53 am

You also have to set family variable for enemies names. So you can set ie. family.name = set family.health

Family.enemy - Set Family.health to 1
Family.bad_enemy -> Family.health = 3
Family.verybad_enemy -> Family.health = 5
Family.motherf####r_enemy -> Family.health = 10
Family.boss -> Family.health = 50
ImageImageImageImage
B
140
S
58
G
36
Posts: 2,547
Reputation: 31,170

Post » Fri Sep 14, 2012 1:11 pm

[QUOTE=HatsuSakura]
Now, if I declare a family's IV Health = X, X will be the value for every health of every enemy. For sure indipendent for each enemy, but with the same start value and this is what I would like to avoid: the same start value for each enemy.
[/QUOTE]
If you click on the family and change the health variable it will apply to all enemies in the family. But you can select the boss sprite and change it's health to 50 without affecting the other enemies.


B
54
S
28
G
18
Posts: 1,520
Reputation: 25,020

Post » Sat Sep 15, 2012 8:12 pm

Thank you all!!
Sorry if I wastred some of your game-developing time

@ramones it was very simple but I was a little bit blind..
I was looking for something ignoring the things before my eyes

B
21
S
5
G
5
Posts: 16
Reputation: 5,421


Return to How do I....?

Who is online

Users browsing this forum: Justin1967 and 29 guests