How to call method only once while keeping the animation on loop

Godot Version

<stable4.2>

Question

<how do you call method only once in animation player, while keeping the animation on loop?>

If only the AnimationPlayer will call the method, the simplest solution would be something like this:

var already_called: bool = false

func my_func():
	if already_called:
		return

	print("Hello!")
	already_called = true

Of course, that doesn’t work if you need to call the method from somewhere else. In that case you could have a parameter for the caller’s path. Give it an empty default value so you can call the method without it as well when the caller is not the AnimationPlayer. Example:

@onready var anim_player = $AnimationPlayer

var anim_player_already_called: bool = false

func my_func(caller_path: NodePath = ""):
	if get_node_or_null(caller_path) == anim_player:
		if anim_player_already_called:
			return
		
		anim_player_already_called = true
	
	print("Hello!")

In your AnimationPlayer node, select the key that calls the method and define your argument(s). Then drag the AnimationPlayer node to the value field of the NodePath argument.