Skip to content

Commit f17a8be

Browse files
committed
SimpleSpawner: Allow spawning nodes as child of scene
In the current implementation, spawned nodes are children of the spawner. In some cases this behaviour is what you want, but it means it's not possible to change the spawn position of future scenes without moving already-spawned scenes. Add a new property to SimpleSpawner which allows choosing a new mode where spawned scenes are made children of the root scene. The change in spawn_start() is necessary because, if the Start Spawning block is used in the On Ready entrypoint, this results in spawn_start() being called from the SimpleSpawner's _ready() function. At this point, the SimpleSpawner itself is ready but the root scene is not. In this case, attempting to add a child to the root scene logs an error, and a later attempt to get the spawned scene's parent to remove it returns undefined.
1 parent d16f29c commit f17a8be

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

addons/block_code/simple_spawner/simple_spawner.gd

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,20 @@ const BlocksCatalog = preload("res://addons/block_code/code_generation/blocks_ca
77
const OptionData = preload("res://addons/block_code/code_generation/option_data.gd")
88
const Types = preload("res://addons/block_code/types/types.gd")
99

10+
enum SpawnParent {
11+
THIS, ## Spawned scenes are children of this node
12+
SCENE, ## Spawned scenes are children of the scene
13+
}
1014
enum LimitBehavior { REPLACE, NO_SPAWN }
1115

1216
## The scenes to spawn. If more than one are provided, they will be picked randomly.
1317
@export var scenes: Array[PackedScene] = []
1418

19+
## The node that the spawned scenes should be a child of. If you want to move
20+
## the SimpleSpawner without moving the scenes it has already spawned, choose
21+
## SCENE.
22+
@export var spawn_parent: SpawnParent
23+
1524
## The period of time in seconds to spawn another component. If zero, they won't spawn
1625
## automatically. Use the "Spawn" block.
1726
@export_range(0.0, 10.0, 0.1, "or_greater") var spawn_frequency: float = 0.0:
@@ -41,7 +50,7 @@ func get_custom_class():
4150
func _remove_oldest_spawned():
4251
var spawned = _spawned_scenes.pop_front()
4352
if is_instance_valid(spawned):
44-
remove_child(spawned)
53+
spawned.get_parent().remove_child(spawned)
4554

4655

4756
func _set_spawn_fraquency(new_frequency: float):
@@ -60,7 +69,7 @@ func spawn_start():
6069
_timer.wait_time = spawn_frequency
6170
_timer.timeout.connect(spawn_once)
6271
_timer.start()
63-
spawn_once()
72+
spawn_once.call_deferred()
6473

6574

6675
func spawn_stop():
@@ -90,7 +99,12 @@ func spawn_once():
9099
var scene: PackedScene = scenes.pick_random()
91100
var spawned = scene.instantiate()
92101
_spawned_scenes.push_back(spawned)
93-
add_child(spawned)
102+
match spawn_parent:
103+
SpawnParent.THIS:
104+
add_child(spawned)
105+
SpawnParent.SCENE:
106+
get_tree().current_scene.add_child(spawned)
107+
spawned.position = global_position
94108

95109

96110
func do_set_spawn_frequency(new_frequency: float):

0 commit comments

Comments
 (0)