Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | chuckeles | |
Old Version | Published before Godot 3 was released. |
I’m coming to Godot Engine from Unity. In Unity, you use components to build entities (game objects). I have learned in the past (the hard way…) to prefer composition over inheritance, so I’d like to use a similar approach in Godot.
I’m new to the engine, but if I understand it correctly, you can only attach 1 script to a node. My question is this:
How would you make and use a component system in Godot?
How to attach multiple components to one node (game object)?
Old github issue related with a similar idea: AshelyCPP for game entities · Issue #2311 · godotengine/godot · GitHub
Bojidar Marinov | 2016-02-26 21:10
I don’t know if someone will read my comment now, but never mind.
It could be great to add a new type of node who acts like a container for components.
The inspector may have a drop down list to add as many component as we want.
This would require to rewrite all nodes in a “component version”. Basically in order to remove inheritance.
In the scene instead of this.
KinematicBody2D
-Sprite
-CollisionShape
We may have something like this for instance.
NodeEntity { 2D, KinematicBody2D, Sprite, CollisionShape }
In this case there no redundance of the Node, Node2D and CanvasItem members.
I guess this system could use less space in memory.
Of course all the previous nodes can be kept into Godot and nothing will be broken. (Don’t know if is a good english sentence…)
It could be just an additional choice.
DriNeo | 2016-03-02 16:35
I think that would defeat the central idea of the Godot engine, which is built on the nodes and the node tree. Anyway I used a similar strategy to the accepted answer below and it worked fairly well, so you should try to go with that.
chuckeles | 2016-03-02 18:49
@DriNeo, might be simple to do with the new plugin system for 2.1
Bojidar Marinov | 2016-03-02 20:11
In my mind the hypothetical " NodeEntity " inherits from Node so it can have childs and it could be a child of any node.
So Godot will not lose its originality, like the very hierarchical scene graph and the scene system.
DriNeo | 2016-03-02 20:35