Concord icon indicating copy to clipboard operation
Concord copied to clipboard

Sorting Pools

Open pablomayobre opened this issue 4 years ago • 10 comments

Sorting Pools before looping through the Entities could be useful for rendering and collision checks.

The feature can be easily added to Lists and Pools would inherit it.

I have already implemented this at a00ee08

pablomayobre avatar Jan 06 '21 20:01 pablomayobre

Hi, where is it possible to see an exemple on how to use this ? The main read-me do not talk about this amazing feature.

Thanks for the amazing library.

chicogamedev avatar Feb 16 '21 06:02 chicogamedev

Yes, this can already be used in the dev branch. It's really simple to use:

function RenderSystem:draw () --An event in one of your systems

   --self.pool is the Pool we want to sort
   self.pool:sort(function (e1, e2)
      return e1.zindex > e2.zindex
   end)
   
   -- Your renderer code goes here

end

The sort function has the same signature as table.sort (since that's used internally)

pablomayobre avatar Feb 16 '21 16:02 pablomayobre

Thanks, amazing !

When will this be available in the stable branch ?

Thanks

chicogamedev avatar Feb 16 '21 18:02 chicogamedev

Once we complete the 4.0 Milestone, we will do a big release. It will probably happen this month, since most of those features are already implemented, and only Entity IDs (#38) and World:query (#44) are missing.

If you are watching the repository, you will most likely receive a notification on the new release, otherwise you can jump over to our Discord server to get notified once the new version is announced.

pablomayobre avatar Feb 17 '21 14:02 pablomayobre

Yes, this can already be used in the dev branch. It's really simple to use:

function RenderSystem:draw () --An event in one of your systems

   --self.pool is the Pool we want to sort
   self.pool:sort(function (e1, e2)
      return e1.zindex > e2.zindex
   end)
   
   -- Your renderer code goes here

end

Hello,

Just one quick question about this exemple. What does e1 and e2 stands for? I just can't wrap my head around the way this sort actually works.

Thanks

chicogamedev avatar Aug 03 '21 05:08 chicogamedev

e1 and e2 are the entities in the pool that are currently to be sorted

flamendless avatar Aug 03 '21 07:08 flamendless

Ah ok,

I thought that was something I need to replace.

Thanks for your answer.

chicogamedev avatar Aug 03 '21 07:08 chicogamedev

As said in my original comment, it works the same was as table.sort. In the sorting function (also called order function) receive two elements (in this case two Entities) of the Pool and you need to compare them and return a true/false value.

If you return true then the first element will appear before the second element. If you return false then the second element will appear before the first element.

pablomayobre avatar Aug 03 '21 14:08 pablomayobre

Indeed I should have made a bit of research before asking.

Is there any performances drawbacks to sort pools right in the draw function ?

chicogamedev avatar Aug 03 '21 14:08 chicogamedev

Should be fairly cheap since it will only happen once per frame, but it will also depend on the number of entities

pablomayobre avatar Aug 04 '21 17:08 pablomayobre