Sorting multiple object instances by a variable

Get help using Construct 2

Post » Wed Oct 03, 2012 9:26 pm

So I have 2 objects with a lot of instances of each on screen. They all have a "z" variable which determines what order they should be in. So my problem is... how do I get them to sort by this variable?

If it were just one object you could do a ForEach (ordered) but it's multiple objects, so it needs to order them both and not just one at a time.

I tried to group the 2 objects in a family and then ForEach (ordered) using that family, but the problem is that I can't check the instance's z variable anymore since I'm referencing the family and not the individual objects.

Is this possible without any add-ons?
B
7
S
2
G
3
Posts: 28
Reputation: 2,260

Post » Wed Oct 03, 2012 10:06 pm

The simplest way would be to put the objects in a family and add a family instance variable z2. Then you could do this:

every tick:
sprite1: set z2 to self.z
sprite2: set z2 to self.z

for each family1 ordered by z2:
B
79
S
24
G
54
Posts: 4,746
Reputation: 40,755

Post » Wed Oct 03, 2012 11:06 pm

Are "sprite1" and "sprite2" conditions? If so, how would I do that?

I assumed that a family variable is just a shared variable among all objects... so it would just be updating that shared variable a bunch of times until it's the last object's z variable.
B
7
S
2
G
3
Posts: 28
Reputation: 2,260

Post » Thu Oct 04, 2012 11:16 pm

*bump* anyone able to help? :V
B
7
S
2
G
3
Posts: 28
Reputation: 2,260

Post » Fri Oct 05, 2012 1:28 am

[QUOTE=Konidias]
I assumed that a family variable is just a shared variable among all objects... so it would just be updating that shared variable a bunch of times until it's the last object's z variable.[/QUOTE]
No, each object has it's own value. So you just put the z variable on the family instead of on the individual objects. Or if you give the family another var 'z2' like r0j0hound said, it will save you having to change your existing code.
B
54
S
28
G
18
Posts: 1,520
Reputation: 25,020

Post » Fri Oct 05, 2012 1:55 am

Okay thanks for clearing that up... but I'm still confused as to what is being set up here:

every tick:
sprite1: set z2 to self.z
sprite2: set z2 to self.z

What are these?
B
7
S
2
G
3
Posts: 28
Reputation: 2,260

Post » Fri Oct 05, 2012 12:03 pm

I was trying to make the text look like events. Sprite1 and Sprite2 are just the names of two different object types, and all I'm proposing is to add the objects to a family, add a family variable, and then for each object type set the family variable to the same value as the instance variable. At that time you can do the for each ordered solution with the family.
B
79
S
24
G
54
Posts: 4,746
Reputation: 40,755


Return to How do I....?

Who is online

Users browsing this forum: flemmig, Matthew de, semaso, Skebab, Virpoja and 24 guests