Here’s how you can spawn a bunch of one scene (that you can select in the editor) inside a 3D cube with a size of 4 units (untested code, but it should work):
extends Spatial
export (string, FILE) var scene_to_spawn_path
var scene_to_spawn
“You could also use the code below if you know the resource path”
“var scene_to_spawn = preload(“path_to_scene_here”)”
var spawned_scenes = []
const CUBE_SIZE = 4
const SCENE_SIZE = 0.8
func _ready():
“If you are setting scene_to_spawn using preload, then delete”
“the line of code below”
scene_to_spawn = preload(scene_to_spawn_path)
populate_cube()
func populate_cube():
“If we have already populated the cube, then erase all of the”
“previously created scenes”
if spawned_scenes.size() > 0:
for scene in spawned_scenes:
scene.queue_free()
spawned_scenes.empty() “I think that’s what the function is called...”
“Figure out how many scenes we are going to spawn”
Var scenes_to_spawn = rand_range(6, 28)
For i in range(0, scenes_to_spawn):
var clone = scene_to_spawn.instance()
“If you need to set variables before the scene’s”
“_ready function, then do it here!”
add_child(clone)
“you can set variables just like you normally would.”
“Some variables like position can only be set after”
“the clone has been added to the scene”
var random_position = Vector3(1,1,1)
random_position.x = rand_range(-CUBE_SIZE, CUBE_SIZE)
random_position.y = rand_range(-CUBE_SIZE, CUBE_SIZE)
random_position.z = rand_range(-CUBE_SIZE, CUBE_SIZE)
Clone.transform.origin = random_position
“You can set other variables/values too!”
“you can set custom added variables too!”
clone.scale = Vector3(SCENE_SIZE, SCENE_SIZE, SCENE_SIZE)
clone.name = “Clone number:“ + str(i)
“To keep track of the scenes this node has spawned”
“we add them to spawned_scenes”
spawned_scenes.append(clone)
As I said above, I have not tested the code. It probably works, but I cannot say for sure it will.