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.
- A second tween with the first left running: 0.69 at 0.6 s, 0.20 at 0.8 s, 0.10 at 0.9 s, 0.00 at 1.0 s, and emitting false at the end. The biggest change in one frame was 0.80.
- The script: 0.70 at 0.6 s, 1.00 from 0.8 s on, emitting true. The biggest change in one frame was 0.03.
- The same two tweens left to run on their own in real time: amount_ratio 0.00 and emitting false at the end with create_tween(), 1.00 and emitting true with the script.
- A tween started every frame for 60 frames, for example from
_process(): 60 tweens running at once with create_tween(), 1 with the script.
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
- Two buffs that both control one aura need one timer for the aura, not two: overlapping buff timers.
- Fading an effect out before removing it: fade out looping particles. Moving two things at once with one tween: set_parallel().
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
- A spawned effect that appears exactly where you hit
- Draw the hit effect over what it hit
- Remove a one-shot particle effect when it ends
- Fade out and stop looping particles
- Spawn a hit effect facing the surface
- Hit sparks from a pool of emitters
- Changing one particle effect's color changes all of them
- Particles that play the same way every time
- Freeze particles in mid-air
- Running dust and landing puffs
- Particles left behind by a moving object
- 2D particles follow the player or stay behind
- Footstep particles that match the ground
- A level up effect that plays while paused
- Hide particle effects far from the camera
- Death particles disappear when the enemy is freed
- Remove all particles at once
- Tire smoke that only shows when the car drifts
- Play a 2D sprite sheet effect once and remove it, even if its animation loops
- Sprite sheet frames without the empty cells
- Sprite sheet frames without lines from the next cell