Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Sorting sprite children... — Gideros Forum

Sorting sprite children...

SinisterSoftSinisterSoft Maintainer
edited January 2013 in General questions
Is there a way in gideros to sort a sprites children/class instances by one of the fields?

eg

-- a load of baddie children, all having different y's

scene.baddies:sort(y,true) -- the 1st param says what field to sort by, the 2nd true or false for accending or decending sort

Likes: MobAmuse

Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
https://deluxepixel.com
+1 -1 (+1 / -0 )Share on Facebook

Comments

  • It probably wouldn't be too bad to write one in lua. You can call addChild passing a sprite that is already a child to change its position, so if you kept a table of them sorting them in order then you could just loop through the table calling addChild passing in each sprite again.
  • Yes, but that would be slow. I'd like to do it without having to add children every game frame. Can you swap the order of two children?
    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
  • SinisterSoftSinisterSoft Maintainer
    edited January 2013
    This is the same problem I'm trying to solve (they are using Corona):

    http://developer.coronalabs.com/forum/2010/11/08/z-order-graphic-objects

    Basically I want to be able to reorder the objects based upon a z-order.

    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
  • You could swap the order using addChildAt. So if you had something at index 2 and something at 5 and needed to swap you could do:

    scene:addChildAt(scene.baddies[2],5)
    scene:addChildAt(scene.baddies[5],2)

    You would just need to make you swap the lower to greater index first, so you didn't throw off later swaps.
  • atilimatilim Maintainer
    Although it's possible to swap 2 children with @zvardin's 2nd post I think sorting with swapping can be much more slower than @zvardin's 1st post :)

  • You mean using Lua's table sort?

    A common c lib to do this (possibly popular function) would be better though... ;)

    Likes: MobAmuse

    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
    +1 -1 (+1 / -0 )Share on Facebook
  • atilimatilim Maintainer
    edited January 2013
    Lua's table sort (table.sort) uses his own quick sort implementation in C. Therefore, it's fast enough. Here the bottleneck will be removing and adding all children.

    Likes: SinisterSoft

    +1 -1 (+1 / -0 )Share on Facebook
  • I might just remap the existing children data from a table (only add, no remove) - hiding any not used... Will that be faster?
    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
  • atilimatilim Maintainer
    edited January 2013 Accepted Answer
    Most probably will be faster. Adding to the same parent (remapping/repositioning) have the advantage of getting rid of ADDED_TO_STAGE and REMOVED_FROM_STAGE events.

    Likes: SinisterSoft

    +1 -1 (+1 / -0 )Share on Facebook
  • @atilim Ok, I've converted it all to use tables and remap and it's fast enough.

    But how do I sort (using the lua sort) something like:

    objects={}
    .
    .
    .
    noObjects=noObjects+1
    objects[noObjects]={}
    objects[noObjects].depth=255
    objects[noObjects].type=0


    based upon the depth part of the table?
    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
  • zvardinzvardin Member
    Accepted Answer
    You can pass in a sort function, so you should be able to do something like:
    -- descending order
    table.sort(objects, function(a,b)
       return a.depth > b.depth
    end)

    Likes: SinisterSoft

    +1 -1 (+1 / -0 )Share on Facebook
  • SinisterSoftSinisterSoft Maintainer
    edited January 2013
    @zvardin Thanks, that works great. Verified with 300 objects depth sorted and no slowdown.
    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
  • Awesome to hear it works so well performance wise. Looking forward to see how you use it (asteroid game? )
  • SinisterSoftSinisterSoft Maintainer
    edited January 2013
    @zvardin - yes it will be used in that game. :)

    I create a table of currently visible objects (on screen) with a depth field. Some matter, (real properly 3D tracked things like baddies) some don't (asteroids, plasma weapons, smoke trails). I sort them so that baddies will move in front of or behind objects visibly (when they do properly code wise).

    Lua and garbage collection are the bottle necks for me to keep the speed up. That's why I was so concerned about adding and removing things. I like to do that as little as possible.

    I'm only spending an hour or two every couple of days or so on the game as I have a Windows program I wrote that I need to support/market during the day. It's coming along though. :)

    I'll post another video soon...

    Likes: gorkem

    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
    +1 -1 (+1 / -0 )Share on Facebook
  • SinisterSoftSinisterSoft Maintainer
    edited January 2013
    I've put an updated video on the other post. :)
    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
Sign In or Register to comment.