Problem with Families Instance Variables

Get help using Construct 2

Post » Mon Jun 24, 2013 3:13 pm

Hi guys,

I just bought the Personal Edition yesterday to enjoy the full potential of Construct 2 and at the same time Support the great job provided by the guys at Scirra.

I have been fiddling with Families to improve processes that would be happen several times in future when new enemies for example are integrated.

But there are a few things that did not turn the way I thought they would. I have tried to fix them but the more I tried, the worse it became. I spent some time trying to figure out why it wasn't working, but the more I read the Forum / Mnaual, the more I got confused.

This is why I have decided to post my first thread asking for your help!

1. Enemy_Type
I tried to regroup different type of monsters in a same family like this:

I created several instance variables and I encounter problems with some of them such as the enemy_type

The idea was that depending on the type of enemy, I would subtract a different amount to the health variable of the Playable Character as shown below:

But the way I did it does not seem to work so I'm a bit confused. For example I have set the Health, which is a family instance variable,to a different value depending on the type of monster (zombie = 100, sprite = 150) and it seems to work fine.

But the enemy_type variable does not work, though I have a condition to subtract a different amount according to the type. But instead it subtracts everything (25 + 50 = 75).

2. Creating new assets
I have attached a cone and a radius to my enemies to detect when the PC is in their Line of Sight or to close to them. To do that, I have made a container such as following:

This works pretty well. But because I wanted to speed up the process when adding new type of enemies, I thought I could use families to do that so I don't create containers every time I add a new enemy:

But when I did it, it did not work as intended. To test if this was working or not, I said to destroy the enemy if the PC is overlapping the cone or the radius. Instead it would destroy all the enemy as if touching one radius was like touching all of them.

Is it because I am not choosing an instance of an enemy and this means this is not possible to do it this way?

3. Instance Variables
So as you can see I'm not sure how to use Family and Family Instance Variables.
Again to detect if the PC was in the LoS of the enemy, I created two family Instance Variables to tell if the enemy is alerted and if he can see the PC as below (this time I'm using containers):

But it's not working as it should. Some of the enemies display the correct icons, some don't... So again I'm not sure how the boolean works for Family Instance Variables (see capx at the bottom of the page). Please let me know if I do something wrong, I would greatly appreciate.

The original version before playing with families was like this, and the LoS worked correctly:

Working Demo here:

I know it was a bit long and I tried to explain the best I could my problems. This has probably been answered but if it has, I did not get it. I'm probably going to revert to a previous version without famly until I understand how they work.

Thanks again for reading me, and thanks for your help. 15:39:04
Posts: 85
Reputation: 899

Post » Mon Jun 24, 2013 4:11 pm

Hey !

What a nice post, explained, clear, .. everything !
Let's try to answer.
Just to start with, I'm also a "new full C2" user, and I've played around and found some of the same problems you did. Here is what I found :
1/ Ennemy_type
Let's talk about your function "onHit". You have to understand the picking mechanic here. As you didn't pick anything, everything is picked. All ennemies. All zombies and all purple sprites. The first action is then applied to all zombies that have "ennemy_type" set to 1 (so all zombis), and then the same appends for the purple sprite.
So first, you have to pick your ennemy. How? By UID. Use ennemy.UID as "onHit" argument when you call it, and add a condition "Pick Ennemy By UID (Function.Param(0)) at the start of the onHit function.
Then your 2 events (36 and 37 in the screenshot) will work, just replace "Zombie" and "Sprite" objects with "Ennemy" family.
Having an ennemy_type variable is useless in this case.

2/ LoS
This one is going to be quick. Don't use a family for this. Don't use a container. Use the new "line of sight" behavior (introduced on r134, start of this month).

3/ Instance Variables
First of all, your icon visibility setting can't work like this.
For example, whene the event 24 is triggered (zombie alerted, overlapping roof), you set the icon invisible. But at the same time, the event 23 is triggered, so you also set it to visible, every tick. Try to use sub events, like this
- Zombie is overlapping roof, roof visible : set invisible
- else : set visible
Same goes for LoS (events 26 to 28)

Next, you have to pick the ennemy in your function here too ! event 21, add a condition "Ennemy : pick by UID", and use the Function.Param(0) to specify the ennemy UID.

I don't have time to go in the capx right now, but I will try to help more tonight, it can work, just need some more debug :)
Posts: 461
Reputation: 6,097

Post » Mon Jun 24, 2013 4:14 pm


try this :

dont know if it can help you :)
Posts: 152
Reputation: 5,142

Post » Mon Jun 24, 2013 4:32 pm

Thanks a lot for your help, I'm going to try and understand what you just explained.

EDIT: Ok I found how to update Construct 2.
"As for the r134, I bought my version on Steam and I have the r132, not sure how to update it."DoudouSupreme2013-06-24 16:58:50
Posts: 85
Reputation: 899

Post » Mon Jun 24, 2013 7:57 pm

So I fixed issue 1 like Anata suggested. But I would like to understand your solution Guizmus as it was also suggested for issue 3, and I will probably need it in future.

I have never used:
"Ennemy : pick by UID", and use the Function.Param(0) to specify the ennemy UID.

So I'm not sure how it works and I'd like to understand as I am familiar enough with using parameters in "traditional" functions. I have read the following below but I'm not sure to understand what it does really or how this works.

Retrieve a parameter passed to a function call by its zero-based index. For example, Function.Param(0) returns the value of the first parameter.

So this mean that 0 in Function.Param(0) is replaced with the current UID of the instance we're trying to retrieve?
Posts: 85
Reputation: 899

Post » Mon Jun 24, 2013 11:24 pm

Well after looking a bit further on the forum, I think I found how it works if I'm correct. I found an example (from Prox) where they used functions and uid in a simple .capx

So I suppose this is how it works:

1. You create the function called for example "Hop":
function --> On "Hop"

2. You define the parameters (if you need some) that will be sent to Hop.
In this case we need to send the UID of the object to Hop, so like this:
obj_creature --> pick instance with UID --> function.parameter(0)

3. You call the function and send it the parameter to use such as:

so I suppose that would be the prototype of the function:
hop(obj_creature --> pick instance with UID --> function.parameter(0))

(0) being the first parameter of the function. If there were more parameters, for example if we wanted to create a sprite at the positon of the mouse every 2 seconds we could have something like that:

Let me know if that's wrong or if I understood the logic correctly.

ThanksDoudouSupreme2013-06-25 00:15:21
Posts: 85
Reputation: 899

Return to How do I....?

Who is online

Users browsing this forum: Baidu [Spider], NN81 and 3 guests