I'd probably set up a dedicated scene explicitly for showing this animated damage value and then just instantiate it, position it, and start it wherever it's needed. Further, I'd just have the scene destroy itself when the animation finishes.
For example, your scene could be setup like this (Damage.tscn
)
Node2D
Label
AnimationPlayer
And, the Node2D could have the following script:
extends Node2D
func show_damage(damage_val):
$Label.text = "Damage = %s" % damage_val
$AnimationPlayer.play("damage")
yield($AnimationPlayer, "animation_finished")
queue_free()
Note, that yield()
followed by queue_free()
will cause the scene to destroy itself when the damage animation finishes playing.
Then, just instantiate the damage scene, position it as needed, and call the show_damage()
. For example, this places a new instance at each mouse-click position and passes in a random damage value.
extends Node2D
var damage_scene = preload("res://Damage.tscn")
func _input(event):
if event.is_action_pressed("click"):
var pos = get_global_mouse_position()
var d = damage_scene.instance()
get_parent().add_child(d)
d.position = pos
d.show_damage(randi()%100+1)
Then, just season the underlying animation to taste. You could animate visibility, scale, rotation, position, color, ... Whatever you need.