Sorting multiple object instances by a variable

0 favourites
  • 7 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • 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?

  • 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:

  • 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.

  • *bump* anyone able to help? :V

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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.

    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.

  • 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?

  • 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.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)