How to set the position of multiple scene instances?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By marcorexo


I’m new to Godot and am trying to create multiple instances of a scene. The problem I have is that I keep getting the error: nonexistent function ‘set_pos’ in base ‘Spatial’. Here’s part of my script:

extends Spatial   

 var box = preload("res://box_scene.tscn")
    var box_node
    var box_nodes = []
 func _ready():
        for n in range(10):
    	    var position = Vector3(randi()%300, randi()%300, randi()%300)
    		box_node = box.instance()
    		box_node.set_pos(position.x, position.y, position.z)

Since the error message suggests that the variable box_node does inherit form the 3D class ‘Spatial’ then why am I unable to access the set_pos method?


:bust_in_silhouette: Reply From: TheFamousRat

Hello marcorexo,

There is no “set_pos” method in the Spatial class. The function you want to use is “set_translation”. The code you wrote to send the argument is also incorrect : set_translation takes a Vector3 as parameter. You can write it either as :


or, and in my opinion better (because cleaner) :


Because you are new to Godot, I prefer to precise this : the transformation of a Node (translation, rotation and scaling) is dependent on that of it’s parent. “set_translation” will only affect the node’s local position. That is, if say the position of the parent is (0,1,0) and you call “set_translation” on one of its childs, and set said child’s translation at for example (2,0,0), the child’s global translation (so what appears on screen) will be (2,1,0). Same applies for rotation and scaling.

If you ever need to modify directly a node’s global position, you can access its global transformation by calling “get_global_transform()” (a transform is a matrix containing information about the node’s translation, scaling and rotation). You can then modify said global Transform and set it by calling “set_global_transform”.

Hope this helps !

Lovely, Thank you! I appreciate your time in writing this.

marcorexo | 2019-02-03 11:47

No problem ! Good luck with your projects

TheFamousRat | 2019-02-03 15:33