0 votes

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
Godot version 3.2.3
in Engine by (232 points)

2 Answers

+1 vote
Best answer

In 3d it is

nodeToAdd.translation = Vector3(1,0,1)

You can see the real name for coding when you hover over a property in the editor.

by (300 points)
selected by
+1 vote

For 3D the property is called translation
You can use a Position3D node, place it exactly where you want
nodeToAdd.translation = $Position3D.translation

Hope this helps :)

by (106 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.