Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | abelgutierrez99 |
Hi, I’m new at Godot,
I want to instantiate a node at some point in 3d using GDScript. I found examples about how to do that in 2d, but I cannot find a solution for 3d.
I put my code bellow to show what I’m traying to do. At code, Cube
and CubeWater
are StaticBody
:
extends Spatial
var rng = RandomNumberGenerator.new()
# Nature blocks
var Cube = preload("res://0Proves/ProceduralGeneration/Cube.tscn")
var CubeWater = preload("res://0Proves/ProceduralGeneration/CubeWater.tscn")
# Size of the world
var WorldSize : Array = [32, 32]
# Positions of the world, starts fixing x and varying z
var WorldPositions : Array = []
func _ready():
WorldPositions.resize(WorldSize[0]*WorldSize[1])
for i in range(WorldPositions.size()):
WorldPositions[i] = rng.randi_range(0,1)
# default tile: Cube
var nodeToAdd = Cube.instance()
if WorldPositions[i] == 0:
pass
elif WorldPositions[i] == 1:
nodeToAdd = CubeWater.instance()
else:
print("Something went wrong")
#nodeToAdd.position= Vector3(1,0,1) # I tried a random position
add_child(nodeToAdd)
pass