What’s the correct way to generate GUI-nodes?
At certain points in the game I need to add buttons to my HUD.
The number and type of buttons vary so I need to generate them on the fly.
Should I somehow use a factory to do so as with regular game objects?
EDIT: my strategy would be to place each button (GUI-node) in a game object and use factory to generate these game-objects that hold one GUI-button each. Would it be the right way to do it?
You could use separate game objects for each button or other dynamically added component, but it would be cumbersome to work with. Quite often you want to tie together the behaviour of a screen or popup consisting of buttons, list items and other UI elements, for instance by disabling/enabling elements or adding a transition when the screen/popup is shown or hidden. Having multiple game objects would make this harder and you’d have to use a lot of message passing to keep everything in sync.
I would recommend that you use a single .gui and .gui_script to build your gui screen. For runtime created gui nodes I think it works really well to have like a prototype node or set of nodes and use the gui.clone_tree() function to clone a hierarchy of nodes.
Note: If you wish to delete dynamically created gui nodes you need to call gui.delete_node() on all the nodes, not only on the root node.
Great! Please note the difference between gui.clone() and gui.clone_tree() where clone() simply clones a single node while clone_tree() clones a node and all of it’s children.
As gui.clone and gui.clone_tree solves most situations ( especially in one single gui-scene ), using a gui template could be the solution if the need is in several gui scenes ( eg a generic button ).