← Pixel Dreamer Studio

Death particles disappear when the enemy is freed

queue_free() frees the children too. Put the burst in the level, where the enemy's death cannot take it along.

queue_free() frees a node and all its children. If the sparks are a child of the enemy, they go with it: in our Godot 4.7 test, a burst started under the enemy just before queue_free() showed 0 lit points. Add the effect to the level instead, at the enemy's position, then free the enemy. With the script below, 423 to 446 lit points showed 0.2 seconds later, and the burst removed itself when it finished.

Left: two red boxes standing on grey circles. Right: the same circles with no boxes, and orange sparks only over the right one
Godot 4.7 with the Compatibility renderer. Left: two enemies before they die. Right: 0.25 seconds after. The left enemy had its sparks as a child, so nothing is left. The right one used the script below.

The code

Attach this to the enemy and call die() when its health reaches 0. The burst is built in code so the example is complete. With your own effect scene, instantiate it and add it to the level the same way.

extends Node3D
## Put this on an enemy. die() leaves a burst of sparks behind and removes the enemy.
## The sparks go into the level, not under the enemy, so queue_free() does not take them along.


func die() -> void:
	var fx := GPUParticles3D.new()
	fx.amount = 40
	fx.lifetime = 0.8
	fx.one_shot = true
	fx.explosiveness = 1.0
	var m := ParticleProcessMaterial.new()
	m.direction = Vector3.UP
	m.spread = 70.0
	m.initial_velocity_min = 3.0
	m.initial_velocity_max = 5.0
	m.gravity = Vector3(0, -9.8, 0)
	fx.process_material = m
	var mat := StandardMaterial3D.new()
	mat.shading_mode = BaseMaterial3D.SHADING_MODE_UNSHADED
	mat.billboard_mode = BaseMaterial3D.BILLBOARD_PARTICLES
	mat.albedo_color = Color(1, 0.55, 0.2)
	var q := QuadMesh.new()
	q.size = Vector2(0.12, 0.12)
	q.material = mat
	fx.draw_pass_1 = q
	fx.finished.connect(fx.queue_free)  # the burst removes itself when its last spark is gone
	get_tree().current_scene.add_child(fx)  # the fix: in the level, not under this enemy
	fx.global_position = global_position
	fx.restart()  # after add_child, so finished fires
	queue_free()

What we measured

An enemy at a height of 1 m, killed 0.2 seconds after it was added. We counted lit points 0.2 seconds after the kill, and the emitters left in the level 1.5 seconds after it. Two runs of each.

If the effect is already in the enemy scene

fx.reparent(get_tree().current_scene) moves it into the level and keeps it where it is. Connect its finished signal to queue_free too, or the used emitter stays in the level.

What to change

Why restart() comes after add_child(): remove one-shot particles when finished. Many hits a second: reuse a pool of emitters.

Tested in Godot 4.7 with the Compatibility renderer.

More how-tos: Play, stop and remove effects