stick-the-quick/levels/DeferredSpawnGroup.gd

32 lines
684 B
GDScript

class_name DeferredSpawnGroup extends Node
signal spawned(what: Node)
signal despawned(what: Node)
var root: Node
func _ready() -> void:
get_tree().current_scene.tree_exiting.connect(root.queue_free)
func spawn() -> void:
if root and not root.is_inside_tree():
if root.is_node_ready():
root.request_ready()
add_child(root)
spawned.emit(root)
func despawn() -> void:
if root and root.is_inside_tree():
remove_child(root)
despawned.emit(root)
func dissolve() -> void:
if root:
root.reparent(get_parent())
get_tree().current_scene.tree_exiting.disconnect(root.queue_free)
root = null
queue_free()
func is_spawned():
return root and root.is_inside_tree()