How to make it so that the tween waits for an animation to play before continuing?

Godot Version

Godot 4

Question

So here is how its supposed to work. We have the enemy’s position aka enemy.slot and the player position which is just slot.
So the game is turned based and I want the player to walk up to the enemy and attack before going back to where they originally were. So I’m using tweens for that bit.

Here is where the problem begins. The player’s normal attack animation plays before it walks up to the enemy and I don’t know why. Here is my code:

func _on_nrml_atk_pressed(): #detects when a button is pressed
	if GlobalSignals.isplayerturn == true and self == GlobalSignals.activeplayercard:
		var tween : Tween = get_tree().create_tween() # Creates tween
		_move_to(tween, target_enemy.slot)
		tween.connect("finished", Callable(self, "_nrml_atk")) #Calls nrml atk function
		representitive_node.get_node("Animations").play("NrmlAtk") # Plays animation
		representitive_node.get_node("Animations").queue("Idle") # Returns to default animation after finishing.
		_move_to(tween, slot) # Returns to original position
		#Returns to enemy's turn
		await get_tree().create_timer(1.0).timeout #if there is a way to make it return to the enemy's turn when the tween is finished let me know :)
		GlobalSignals.isplayerturn = false

You can call functions with tweens like:
tween.tween_callback(Callable(self, "_nrml_atk"))
tween.tween_callback(Callable(representitive_node.get_node("Animations"), "play").bind("NrmlAtk"))
tween.tween_callback(Callable(representitive_node.get_node("Animations"), "queue").bind("Idle"))
This way they will be called one after another, after the tween is done moving.

(edit: though I would probably just change the animations from the _nrml_atk function)

Then generally you can use the animation_player’s animation_finished signal to wait for an animation to finish.
I think you’d need change things a little to make it work, put things after the animations into a separate function that would be called by the signal.

You can change a bool using tweens, like:
tween.tween_property( GlobalSignals, "isplayerturn", false, 0.0)
so it will be changed to false when the tween is done with the moving.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.