Not sure if anyone else here is like me, and hate the whole “Drag and drop nodes in inspector” I hated that in Unity also, so I made a small extension helper to automatically do it for me, it probably dont work perfectly for multiple Nodes of the same type, since I hate to have to give up string values of the nodes I am attempting to get the name of the current node.
I haven’t tested it extensively yet, but a quick overview it does work.
public static T SetupNode<T>(this Node parent) where T : Node
{
var childNodes = parent.GetChildren();
try
{
if (!NodeTypeCount.ContainsKey(typeof(T)))
{
NodeTypeCount[typeof(T)] = 0;
}
var correctChild = childNodes.Where(child => child is T).ElementAt(NodeTypeCount[typeof(T)]);
NodeTypeCount[typeof(T)]++;
return parent.GetNodeOrNull<T>(new NodePath(correctChild.Name));
}
catch (Exception)
{
var nodeNames = childNodes.Select(s => s.Name);
var message =
$"No nodes of type {typeof(T).Name} with name found in {parent.Name}. The available nodes are: {string.Join(", ", nodeNames)}";
parent.GetTree().Quit();
throw new InvalidOperationException(message);
}
}
Then it can be used like this:
_animPlayerNode = this.SetupNode<AnimationPlayer>();
_animPlayerNode2 = this.SetupNode<AnimationPlayer>();
_sprite = this.SetupNode<Sprite3D>();
_sprite2 = this.SetupNode<Sprite3D>();
_animPlayerNode.RootNode = _sprite.GetPath();
_animPlayerNode2.RootNode = _sprite2.GetPath();
Anyway, done with that rabbithole. Back to the course to learn godot!