Playing an animation when a signal is emitted

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By PurpleSkull

So i was wandering if it was possible to play an animation when a signal is emitted, just so i dont have to create a script for each time i want to do simple behaviour such as that. I would have to create a different script each time I wanted to connect a signal to do anything. Or maybe i can create a single script i can reuse to connect a signal to call any function. I already tried to use the advanced option in the connect section and to connect the signal to the play method in the animation player, giving the “StringName” argument, but sometimes it fails, it doesnt seem to work everytime though, it sends an error saying that he didn’t find the animation, but the name of the animation in the error is blank, so its like it doesn’t receive the argument. I don’t know, any suggestions?

:bust_in_silhouette: Reply From: tuon

You can create a new script that inherits from Node and then add some exports to it to point to the node that emits the signal, the name of that signal, the animation node, and the name of the animation. Then in the script’s _ready() method connect the signal to some generic handler method. You’ll want to add a good number of parameters that are all defaulted to null in that method. I’ve used up to 9 such parameters. This way Godot won’t give you an error because the number of parameters are different. Then in the handler method call the animation node’s play method with the given animation name.

*note: When I say “exports that point to node” I mean a NodePath.