Layers using nested groups

I was forced to use Defold for a collaboration. I clearly prefer Solar2d for every feature. Except 1 – layers.

Not a big deal, but I decided to create layers:

	V.sceneGroup = self.view

	V.bg = display.newGroup()
	V.base = display.newGroup()
	V.action = display.newGroup()
	V.veneer = display.newGroup()
	V.top = display.newGroup()

	V.sceneGroup:insert(V.bg)
	V.sceneGroup:insert(V.base)
	V.sceneGroup:insert(V.action)
	V.sceneGroup:insert(V.veneer)
	V.sceneGroup:insert(V.top)

Not perfect, because layers and groups are different – For my current project I need parts of an object to be in different layers for interleaving. Move together, but retain their layer. But easier to do coordinated move than all those toFront and toBack.

Then I wondered: is there a performance or stack corruption or other problem that I should worry about from all that nesting?

Your approach is essentially the recommended way (per docs) of using groups for layering.

As for performance, to quote the docs, “display groups are essentially tables.”

You should be able to use 20 to 30 groups without any issues; there’s actually no rule of thumb here, but generally 2D games use between 5 to 20 layers. I would say that you’re more likely to have an issue with too many textures than you would with “too many groups.”

1 Like

Hey there!
I used the same approach to create parallax in a couple of games.
I’ve used around 30 groups in some levels and measured performance against having a single group. No difference at all! So don’t worry about it.

1 Like

Thank you, that is great to hear. Somehow I missed that in the docs, I looked. Well, sorry I missed it. But thank you both.

1 Like

If you need something like editor of defold, you can use Tiled and ponyTiled. I always use it to layout my projects.

Thank you, but I don’t particularly like the Defold editor. Perhaps because I am an old guy, I prefer simpler editors. I use Sublime Text, does everything I need. I will note that for my colleagues, though. Thanks.