Skip to content
GodotComponents
← Back to components
A bullet travels across the screen and disappears after a fixed time.

Lifetime Timer

Type: General

Frees its parent after a set number of seconds. Drop it on bullets, effects and pickups so they clean up after themselves.

Setup

  1. Save the script as lifetime_timer.gd.
  2. Add a Node as a child of the scene that should disappear and attach the script.
  3. Set Seconds in the Inspector.

Code

GDScript
class_name LifetimeTimer
extends Node
## Frees its parent after a set time. Useful for bullets, effects and pickups.

@export var seconds: float = 2.0

var _time_left: float


func _ready() -> void:
	_time_left = seconds


func _process(delta: float) -> void:
	_time_left -= delta
	if _time_left <= 0.0:
		get_parent().queue_free()