Assigning child nodes to instanced node for specific actions

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Aaron
:warning: Old Version Published before Godot 3 was released.

I have an instanced node with certain actions, or “events”, which may be handled by custom child nodes assigned to the instance. So how do I assign child nodes to an instance such that the instance knows about the children and what action each child is for?

An example would be an NPC node. It has an “approach” action, which the NPC fires when the player gets within a certain distance, and a “death” action, which the NPC fires when it dies. Along with NPCs I have QuestUpdate nodes, which when triggered cause changes to the game’s state and can cause things like text pop-ups to display. In my level scene I’d instance NPCs, and for some of the NPCs I’d assign QuestUpdate nodes for either their “approach” or “death” actions.

Another example would be NPC AI. Not all NPCs would need AI, and I may have different AI types for different NPCs. So I’d instance AI nodes assigned to the relevant NPCs.

One idea I had was giving my node special container nodes for each of its actions that get handled by the special child nodes. So an NPC would have an “approach” node, a “death” node, and an “ai” node. In the level scene I’d assign the relevant QustUpdate nodes to an NPC’s “approach” and “death” nodes while assigning the relevant AI node to an NPC’s “ai” node. But this would mean turning on editable children, displaying every other child node the NPC has.

Another idea I had was just requiring these child nodes to have a specific name so that the main node can check for them directly. So a QuestUpdate node meant to handle an NPC’s death action would always be named “on death”. I actually do this now for the AI nodes. Though it requires that I give the node the correct name.

:bust_in_silhouette: Reply From: Zylann

If I understood well, your NPC has signals and child nodes listen to them to perform either AI, approach or death actions?
In this case, you don’t need to give them a specific name, just connect the signals you are interested in in the _ready() callback. I do that too in my game to separate sound, graphics, stats and other logic within my avatar node.

For example, approach nodes would have a script on them that registers for the on_approach signal from the NPC and do things. It’s a bit like a component approach.

Also, you can inherit scenes instead of using the “editable children” feature, but it depends on what you feel is better. I would choose the option for which there is no copy/paste :stuck_out_tongue:

I’d like the signals approach, but then I’d need a way to control which signal a child node listens to.

Using my NPCs and QuestUpdates example, I’d either need to add a property to the QuestUpdates specifying which signal it responds to, or add an intermediate node between NPC and QuestUpdate. Such an intermediate node would be a child of NPC and connect itself to one of the NPCs signals, so I’d have a node type for an NPC’s approach action and a node type for an NPC’s death action. In turn an intermediate node would have a child QuestUpdate.

Another thing is I may want to check if a node has a child associated to one of its action for other things. If I have an NPC node, I may want it to have a “talk” action. And if an NPC has a QuestUpdate associated with its talk action, I may want a check that allows me to display a talk icon when the mouse is over the NPC.

Aaron | 2016-08-25 00:06