How to HeapsIO -- "*intended*" use

– Unsorted contemplations of how HeapsIO “should” be used. –


(This is an answer to Rahil in “Entity systems 101”)

Yeah, actually one should maintain one’s own data structures for that. (It may just be a list, an array, some reference vars etc. that’s okay :+1:)

About findAll(), it’s in the nature of doing what it does. It searches through the entire tree of the object’s (here h2d.Scene) children and children’s children (so recursively) etc. to apply a condition/function on each of them. Then it creates (and under the hood allocates RAM) a fresh new Array of data and then you (I, in that case) call that findAll() every(!!!) time calling the update function (as I don’t even use dt – delta time – that’s really on every frame possible !!!)… wow! see? – I wanted to rely on the HeapsIO API and avoid maintaining my own Arrays for the sake of keeping the tutorial simple. So, good point. :+1: I should mention that maybe in the class description or sort of right away… :thinking:

Yes, this whole thing confused me too when I started with Heaps. It’s really that h2d.Scene extends h2d.Layers, which extends h2d.Object. It’s all there already. And when you have a game with no layers or just attach an object to another (“node” tree like in Godot, see below) you can just use the new Object(parent) approach. Otherwise, yes, it’s s2d.add( o, layer_index ).

Trees (adding one object to another) allow to move the root – an enemy goblin for instance – with all of its children: the goblin’s health bar, that is drawn on top of him, the text on the health bar itselft saying “life”. The little icon of a heart attached next to it. etc. etc. and you can also rotate etc. them all at once.

Layers allow to structure different kinds of objects. For instance a platformer has a layer for UI_layer=4, platformsLayer=3, playerAndEnemiesLayer=2, backgroundSceneryLayer=1 and veryDistantBackgroundSceneryLayer=0 (like mountains, really far away). These vars are just Ints, that represent the priority in which order objects in that layer are to be drawn. But you decide for yourself what you need.
Then, for a top-down game, you can use e.g. .Ysort() on a layer so what’s in front is drawn in the end (on top of what’s behind). Draw earth and lawn, then players and houses, then clouds, then UI, because it’s above everything else.

1 Like

wow, thanks so much, so much clarity ~~ :heart:

It’s really that h2d.Scene extends h2d.Layers , which extends h2d.Object . It’s all there already.`

:exploding_head: lolll, i didn’t even think about the inheritance of Object! That’s crazy! No wonder why it has so many dang functions!..

etc. to apply a condition/function on each of them.

LOL, I saw the size of that function and just knew it wasn’t meant to be used often. I just avoid the fancy language features altogether… it’s like C# lambdas, i just have no clue what it’s doing underneath… and lol, yeah, i knew for sure getting an array every frame is a terrible idea. :joy: I was surprised at how Arrays are used so much in places where i think Lists should be used, but maybe Haxe’s implementation is… special. :rabbit:

allow to move the root

:open_mouth: i didn’t even think about the root idea. :open_mouth: I thought they were trees because they sorta branch out :joy:

Can both the scene tree and layers be used together? Like, let’s say you have all those layers you mentioned, full of Objects via .add, and then you add a goblin which has all those children you mentioned (via addChild or new(parent)), does it work out fine?

layer[3] = enemy layer (or lair :japanese_ogre:)
[3][0] = ghost (added via layers.add)
[3][1] = ghoul
[3][3] = goblin (added via layers.add)
[3][3] = goblin health bar (added via object.addChild)
[3][3] = goblin aura (added via object.addChild)
[3][4] = ghost
[3][5] = ghoul

well, maybe i’ll test this soon anyway…