Families / Create specific object type

Get help using Construct 2

Post » Wed Jan 25, 2012 3:00 pm

Hey guys,

So I have this project on a chess board, Queens, Knights, Pawns etc.

I created an event sheet that creates some queens dynamically, based on some global parameters (how many queens to create, where to place them etc).

I created another event sheet that creates some pawns dynamically, based on global parameters, with IDENTICAL code, except for the object name, where I replaced Queen with Pawn.

And so on for knight, bishop etc. All these Queen, Pawn, Knight etc belong to the ChessPieceFamily family.

I thought it would be nice to use this Families feature instead of copy/pasting code around (and forgetting to modify in all places).

I would like to have just ONE event sheet, which creates some ChessPieceFamily dynamically (using System->Create object), and based on some global variable I would tell it what exactly to create (pawn, queen etc).

Is this possible? I.e. filtering just one of all the object types in a family when using System->Create Object on a family?

Thanks a lot!
Geo.

PS: looking forward to family instance variables and family accessing object instance variables too
B
14
S
5
G
7
Posts: 235
Reputation: 5,175

Post » Wed Jan 25, 2012 3:10 pm

As a possibility, you can at the top of the event sheet, do a specific object spawn with a bunch of branching if statements for which object you want to spawn. After that, you can use events which act on the Family, however, as mentioned in the r75 event log, families do not support added behaviors, and instance variables.

To get around this, you could have a single object called ChessPiece and have different animations each of which corresponds to a different piece, queen, king, rook, knight, bishop, and pawn. Then you can just set the appropriate animation, and use event conditions which compare against a pieces current animation.
B
30
S
6
G
8
Posts: 232
Reputation: 6,119

Post » Wed Jan 25, 2012 5:07 pm

Just use one sprite with all the different piece graphics in different animation frame. So when you want to create a specific piece, you create this sprite and you set the proper animation frame.
Then you can differenciate each piece by its animation frame and apply custom rules on them (how they move, how they attack, promotion, etc.)

Note that this way when you promote a pawn you really just change it's look.

Also don't forget to set the animation speed to 0.Yann2012-01-25 17:08:48
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Thu Jan 26, 2012 10:38 am

Great, thanks guys, this approach seems to be working. It would have been nice to have kind of a Family Factory, but I guess this workaround will do just fine for now.

I ran into another issue with event sheets, but that's a different topic...

Thanks again!
B
14
S
5
G
7
Posts: 235
Reputation: 5,175

Post » Thu Jan 26, 2012 2:21 pm

If you pass a family to 'create object', it'll pick a random object type to create. Instead just create the actual object type you want to create! You can then pick it in the family by its UID.
Scirra Founder
B
359
S
214
G
72
Posts: 22,949
Reputation: 178,544

Post » Thu Jan 26, 2012 8:11 pm

Thanks Ashley. I was trying to write "generic" code, e.g. have an event sheet that creates a "family", and depending on where I include this event sheet (i.e. depending on some global variable I set previously), the CreateObject would choose a particular object type from the family. I mean, I was trying to avoid passing the actual object type (and have a lot of "if"s in the object sheet which does the creation).

The approach/workaround suggested by cacotigon and Yann actually works. Plus, I found out today about the function plugin which greatly simplifies my work. Sure, it would be nice if I could somehow tell the CreateObject, when passing the Family, what object type I want (by passing some index, or some string perhaps, or by passing some ad-hoc "tag" that would be set on the object type when it gets added to the family). But for now I can live without

Thanks.
Geo
B
14
S
5
G
7
Posts: 235
Reputation: 5,175


Return to How do I....?

Who is online

Users browsing this forum: Timedoor Indones and 23 guests