← Pixel Dreamer Studio

An aura that comes back when the buff comes back

A new tween does not stop the old one. Keep the tween and kill it before starting the next.

create_tween() starts a new tween next to any that are already running, and the old one keeps writing the same property and still runs its callbacks at the end. In our Godot 4.7 test an aura fading out over 1 s, then switched off, was faded back in at 0.5 s. The new tween won for 0.3 s, then the old one took over again, dropped the aura from 1.00 to 0.20 in one frame, faded it to 0 and switched it off, with the buff active. Killing the old tween before starting the new one kept the aura at 1.00 and on.

The code

Save it as one_tween.gd, load it with const OneTween := preload("res://one_tween.gd") and use var t := OneTween.start(aura, "fade") wherever you wrote var t := aura.create_tween(). Starting a new "fade" on the same node kills the old one, callbacks and all. Give tweens that should run side by side different names, like "fade" and "pulse".

extends RefCounted
## One tween per job on a node: starting a new one stops the old one first.
## Two tweens on the same property both keep writing it every frame, and the
## old one's callbacks (hide, queue_free, emitting = false) still run at its end.
##   var t := OneTween.start(aura, "fade")
##   t.tween_property(aura, "amount_ratio", 0.0, 1.0)
##   t.tween_callback(func(): aura.emitting = false)
## Use a different job name for tweens that should run side by side
## ("fade" and "pulse"), the same name for tweens that replace each other.


static func start(node: Node, job := "main") -> Tween:
	stop(node, job)
	var t := node.create_tween()
	node.set_meta(_key(job), t)
	return t


static func stop(node: Node, job := "main") -> void:
	var key := _key(job)
	if node.has_meta(key):
		var old = node.get_meta(key)
		if old is Tween and (old as Tween).is_valid():
			(old as Tween).kill()  # stops it and its callbacks
		node.remove_meta(key)


static func _key(job: String) -> StringName:
	return StringName("_one_tween_" + job)

What we measured

A GPUParticles3D aura at amount_ratio 1. Buff ends: tween it to 0 over 1 s, then set emitting = false. At 0.5 s the buff is picked up again: set emitting back on and tween to 1 over 0.3 s. Both tweens were stepped by hand 60 times a second, oldest first. Two runs gave the same numbers.

Why it happens

Each tween is its own object, and every live tween sets the property to its own value each frame, so the last one to run that frame wins. In our test the newer fade-in ran last while it lasted. When it finishes at 0.8 s, the fade-out is still in its first second, so its value shows again, all at once. Its tween_callback() is part of the same tween, so it runs at 1.0 s even though nothing wants the aura off any more. kill() stops the tween and everything still queued in it.

What to change

Auras that follow the body: the Character Aura Kit for Godot 4 emits auras from a rigged character's own skin and follows the animation: power-up, fire, ice, poison, lightning, dissolve.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: Character Aura Kit for Godot 4 emits auras from a rigged character's own skin and follows the animation: power-up, fire, ice, poison, lightning, dissolve.

More how-tos for Character Aura Kit for Godot 4: Magic aura particles that circle a character · Keep a buff aura on until the last buff really runs out

More how-tos: Play, stop and remove effects