Two variants of the same solution:
If you have a flag which is true
while you should be flickering, you can do this:
func _process(delta):
if is_damage_state:
# Halve opacity every uneven frame counts
self.modulate.a = 0.5 if Engine.get_frames_drawn() % 2 == 0 else 1.0
else:
# But beware... if the last damage frame is not even,
# you risk to leave your character half transparent!
# Preferably do this when you set your flag back to false
self.modulate.a = 1.0
If you have a signal emitted only once you take damage:
func on_take_damage():
# Flicker 4 times
for i in 4:
self_modulate.a = 0.5
yield(get_tree(), "idle_frame")
self_modulate.a = 1.0
yield(get_tree(), "idle_frame")
You can also do this without code, by adding another AnimationPlayer
which can run independently from your main animations.