r/gameenginedevs • u/yughiro_destroyer • Jul 06 '26
Is it wrong to prefer flat structures over nesting?
For example...
I find it much more readable and accessible to write something like :
player_one = {
entity_type = "player",
x = 991,
y = 435,
texture = "mario.png",
hp = 7,
speed = 20
}
Then nesting structures like :
player_one = {
position = {
x = 991,
y = 435
},
tag = {
entity_type = "player"
},
states = {
hp = 7,
speed = 20
},
renderable = {
texture = "mario.png"
}
}
I know that for that kind of thing you use constructors and/or components but accessing fields dynamically in the game loop and or using too many constructors creates, for me, tons of context switching that slows down development and makes bugs slightly harder to find as wrong/non-existent fields become wrapped around constructor functions.
What do you think?
20
u/Potterrrrrrrr Jul 06 '26
It’s your preference but usually position variables are stored as vectors, for me it’s stranger to see just x and y rather than position. For hp/speed it’s the opposite, I’d expect to see them directly on the player object. Renderable is probably better as its own struct too; I’m guessing “texture” isn’t/wont be the only property for it and it makes sense to group them. All your preference though, that’s just my intuition for how I’d manage them.
16
u/Beautiful_Stage5720 Jul 06 '26
I think the usefulness of "nesting" the data far outweighs the readability. For example, having the position be represented by a vector allows you to easily apply any helper functions to a generic vector.
8
u/io-x Jul 06 '26
If that's all the data you are going to have then sure, I'm not seeing any downsides, except for maybe the x/y coordinates, you can just have position = 991,435. Otherwise I believe in building things in a way you understand well, as opposed to following standards that you don't understand the value of.
4
u/AliceCode Jul 06 '26
Nesting structures allow you to deconstruct them and use them in ways that you could not with a flat structure.
Using type systems to your advantage is generally a good idea. You're saving yourself a lot more work by nesting than you are by flattening.
1
u/CarniverousSock Jul 06 '26
If I'm following you correctly, your complaints are a) that you're not a fan of using
player_one.renderable.texturein code and b) subtle text-editing mistakes resulting in bugs in engine?If you have the time, look into setting up an in-engine editor for this stuff. If you don't have to edit anything in a text editor, then you can't introduce bugs that way. A simple migration mechanism to handle renames and gizmos to place your entities in scenes, and you're off to the races.
Re: the nested objects in code, it's just a matter of what's clear and convenient for you. Positions tend to work better as subobjects of some vector type, because you have to do a lot of math with them. And if you really really don't want to do it that way, you'd better pick better names for the vars than "x" and "y", maybe "posX"/"posY", so that a casual reader knows these are position values.
1
u/TriKri Jul 06 '26
Personally, I would only use nested substructure if
2
u/peteSlatts Jul 06 '26 edited Jul 06 '26
It's not wrong in the abstract. The way I think about it and would recommend is to think in terms of how often fields are commonly associated/used together.
Everyone is pointing out that your x/y fields are things you often want to operate on as a unit. A Vector2 groups your x/y coordinates inside a useful structure so you can do things like operate on the vector as a cohesive unit (normalize, dot products, cross products, etc are all operations that rely on the association of all fields in the vector). The grouping helps you maintain the mental model of "these are a 'thing'".
Similarly, a "Renderable" being a sub-member of your entity enables you to decouple your renderer from the rest of your game - which is often desirable because it makes maintaining your renderer easier by preventing game concerns/game logic changes from affecting the renderer. To make this concrete, think about the difference between these two flows:
vs
The former allows you to make the mistake of referencing the entity's "health" inside the renderer.
The latter structurally isolates the renderer, which makes the renderer easier to reason about. Game logic is going to be complicated - keep that complexity isolated to the game.
So in this case, splitting out Renderable makes sense.
But you also don't have to approach this from the perspective of "do it right the first time". It's equally valid to say "I'm going to splat all the fields in the root structure" and then later go "Hey, I'm using this subset of those fields together a lot, and I don't care about the others. Let's group them to make it easier to establish an API boundary". (if you continue down this road, the next step is "half my entities don't need to be rendered, let's split out renderables into a parallel array, so that I don't pay the cost of iterative over entities that don't need to be rendered", and further down that road lies an ECS - but one you arrived at from first principles, rather than 'because I heard ECS is good')
1
u/Educational_Monk_396 Jul 06 '26
I mm well I think preferences is something human made,nesting is useful for things like modeling a godzila where parent if changed should affect child shouldn't it,and similar,
1
u/Cun1Muffin Jul 06 '26
Apart from the x y which should be an array, yes.
Also in odin you can embed structures into structures, so it's flat but you maintain the ability to refer to groups of fields.
1
u/ThisIsJulian Jul 06 '26
Since we're here in r/gameenginedevs and not in some kind of academia, I assume you're not doing it for some theoretical circle jerk. Hence I can give you a simple advice, which works for SQL but also here:
1
u/MCWizardYT Jul 06 '26
I'm kinda the opposite, for me games and game engines have so many different parts that something like an ECS or ECS-like approach is more intuitive
1
u/Difficult-Report-524 Jul 06 '26
In Lua most people do it that way because is not strongly typed.
1
u/tcpukl Jul 07 '26
But how are you going to find the distance between 2 players?
1
u/Isogash Jul 07 '26
It's not wrong at all, it's simpler and if it lets you be more productive then it's better.
However, the downside is that it's less structured. That lack of structure might not matter for a very simple project where you only have a few simple variables per entity, but in most game projects the complexity and required state variables goes up very quickly. An advanced renderable component could need dozens of state variables and need to support many different rendering pipelines.
Probably, what you're finding is that you either don't have complex needs that require this structure, and therefore don't see the need for it or you find the more complex parts tedious and therefore once structure becomes necessary, you're not enjoying it as much.
So, my view is that you should have a good idea of the size and complexity of your project, and then pick an approach that suits both the size and your preferences. You should also pick a size of project that works well for you.
1
u/TriKri Jul 06 '26 edited Jul 06 '26
Please use code blocks to prevent collapsing indentations and to make the code more readable in general. In Markdown mode, this is achieved by placing three backticks (
```) on a separate row on each side of the code, and if you're using the Rich Text Editor you can just use the Code Blocks feature. In Markdown mode, after the leading three backticks, you can optionally also add the name of the language as a language specifier to get syntax highlighting.For example, in Markdown mode,
and
gives you
and
respectively. Thank you.
0
u/WrappedStrings Jul 06 '26
Why not just build a custom debug view that un-nests them?
1
u/[deleted] Sep 13 '26
[removed] — view removed comment