This is the testing Godot forums! All forum posts unique to this forum will be deleted! Please use the main forums here for any posts you want to keep. All forum rules still apply.

How to use a for loop to spawn multiple mesh instances in a 3d scene?

winniethewindwinniethewind Posts: 9Member
edited July 2018 in 3D

Hey there, I've been wanting to spawn some 3d objects in the 3d scene? Like a field of stars or spheres across the screen. I've managed to do this in Java using for loops but I dont quite understand the method in GDScript.

I posted a question in the past asking how to do this, and a response was to use a node with a script that acts as an item generator. This makes sense but I don't know where to start. I once saw a Godot video tutorial that explained and demonstrated this but I am unable to find it at this time.

Eventually, I'd like to experiment with spawning multiple physics objects that move with acceleration and deacceleration and such. Like spawning multiple kinematic bodies with all the same properties.

Anyone that could give me a lead I would very much appreciate it. Thanks


Tags :

Answers

  • TwistedTwiglegTwistedTwigleg Posts: 2,561Admin

    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.

Leave a Comment

Rich Text Editor. To edit a paragraph's style, hit tab to get to the paragraph menu. From there you will be able to pick one style. Nothing defaults to paragraph. An inline formatting menu will show up when you select text. Hit tab to get into that menu. Some elements, such as rich link embeds, images, loading indicators, and error messages may get inserted into the editor. You may navigate to these using the arrow keys inside of the editor and delete them with the delete or backspace key.