Hi!
I come from C++, so I’m quite used to strong-typing and actually prefer it. That’s why when I want to store some data e.g. about an attack (Damage, Speed, ElementalType etc.), first thing coming to mind is a STRUCT. I know they do not exist in GDscript, so I’m trying to understand the pros and cons of using Classes vs Dictionaries for that.
DICTIONARIES provide most things I could want (no memory manegment, can be easily serialized) but come with the drawback of not enforcing typing/layout: field access typos won’t be caught at compile and there is no autocomplete for fields.
CLASSES (or inner classes to be specific) seems like a good solution for that, the main drawback not being easily serialized and being passed by reference?
- My hesitation in using classes comes from that they appear to be rarely mentioned for such use in docs or discussions. Some engine functions prefer using dictionaries with named field rather than custom types (eg. PhysicsDirectSpaceState3D.intersect_ray()). What’s the reason it’s done like that?
- What do you use for storing small, coupled data with no methods? Anything other I’m missing?
(not sure if this post belongs in General or in Help?)
If you need serialization, use classes derived from Resource. In my experience, those are best suited as data management workhorses in Godot.
1 Like
Yes, I use Resources for things that I want to save/store, great for that.
But for example I’m currently working on “action queue”: it’s a list of Callables with accompanying condition describing when they should be called. It’s populated during runtime and there is no need for saving. I heard that Resources have some significant overhead, and since this table can have heavy use (a lot of adding and removing) I’m hesitant to store each entry with resource.
What kind of overhead would that be? It’s a class like any other.
If you don’t need serialization, you can use a class that inherits RefCounted which is the default if you omit the extends declaration in GDScript. Use an inner class if it’s semantically closely tied with its container class. Those will also default to inheriting RefCounted.
Dictionaries may be fine but as you already noted, you don’t get type checking there unless it’s a typed dictionary that holds the same types of key/value for all elements. The property lookup in classes will also likely be faster than key lookup in dictionaries.
2 Likes
I meant memory footprint/initialization time, but I see docs call Resources “a little more complex than RefCounted”, so idk why I thought it’s worse. I will try to use RefCounted like Id use structs, thanks!
Still, any idea why the engine itself prefers to return you a dictionary in some cases?
Just a side note if you dont need to export to the editor, then you could in theory use C# structs.
dictionaries seem simplier and you could use typing to help yourself, of course it won’t be as good as in C but GDScript is high level language and it is very forgiving for not having good types
as the sidenote i’ll say what i repeat to everone - if you don’t need structs to make your game and want them only for aesthetic/comfort then it’s better to try to adapt new language, this way you won’t hold yourself back and be able to make game and not fight with the code
Here I have to disagree @NOTIdealDev
I completely understand preference for dynamic typing and referencing by name, but personally I see that more as an obstruction than help. I actively choose to hard type everything, even though it might be a bit slower. One reason being that if come back to my hobby project after 2 months, I don’t want to be guessing if on_damaged(data) has info in data[“dealer”] or data[“damage_dealer”] ; I just press “.” and see the correct option.
Actually, I would be completely satisfied if you could pre-define dict fields and types, but I guess that’s almost a struct 
1 Like
Yeah, the only overhead is that a resource will have a slightly bigger memory footprint than a plain reference counted object.
As for why the engine returns dictionaries in some cases, my guesses are: to avoid forward/backward compatibility issues, legacy convention, and possibly smaller memory footprint of the dictionary object. Some kind of struct/tuple thing would be ideal, of course.
Just treat RefCounted derived class as a struct and you’ll be fine.
1 Like
Thanks, will do.
The only thing I see might be a problem is that I can’t pass them by rpc()'s, so I’ll probably create some simple de/serializers.
1 Like
All i say is that focusing your entire attention on clean code and making it organized instead of working on a game and proving it’s worth making is almost always worse than having prototype you can work from and potentially even small audience, at the end game dev is about making video games and not coding itself
2 Likes
Sure, I get that point of view.
But for me it’s also a kind of therapy hah, where after a day of working at the most messy spaghetti type code project with infinite technical debt cause investors and board want FASTER, I can tinker on my small, perfect game and refactor things till I’m completely satisfied 
1 Like