0 votes

Hi

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)
            box_nodes.append(box_node)
            self.add_child(box_nodes[n]) 

Since the error message suggests that the variable boxnode does inherit form the 3D class 'Spatial' then why am I unable to access the setpos method?

Thanks,

asked Feb 2, 2019 in Engine by marcorexo (85 points)

1 Answer

0 votes
Best answer

Hello marcorexo,

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

box_node.set_translation(Vector3(position.x,position.y,position.z))

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

box_node.set_translation(position)

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. "settranslation" will only affect the node's local position. That is, if say the position of the parent is (0,1,0) and you call "settranslation" 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 "getglobaltransform()" (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 "setglobaltransform".

Hope this helps !

answered Feb 3, 2019 by TheFamousRat (305 points)
selected Feb 3, 2019 by marcorexo

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

No problem ! Good luck with your projects

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.