• Godot Help
  • getting the positions of multiple nodes in a scene

I'm trying to store the positions of a specific set of nodes in a scene (so I can iterate through and check distance). However I haven't been able to find a way to do so.

I'll give an example:

Node 0 needs to store the positions of a specific set of nodes in an array.

Nodes 1-8 have are at varying positions away from node 0.

Node 0 needs to specifically store the positions of Node 2, 3, 6 and 8. What would be best way of doing so?

  • Invalid get index 'position' (on base 'Node')

    Node is the most basic node type, and has no position. Node2D is the most basic 2D node type that has a position property.

    Spatial is the most basic 3D node type that has a position, named translation. (In Godot 4, Spatial has been renamed to Node3D, and its position is named position.)

    If that doesn't confuse you, let me know and I'll try harder. 😎

Is Node 0 the parent of the other nodes? It's best to put the array in a node that's an ancestor of the nodes in the array, or in an autoload.

var nodes: Array

func _ready() -> void:
    nodes = [
        $path/to/node2,
        $path/to/node3,
        ...
    ]

Is that enough detail?

    DaveTheCoder I mean that would work but node 0 is a sibling node to the others, and the nodes to find will also be doing the same thing. each node has an array that needs to be filled with game objects. I'd say I didn't explain it well enough. I could probably figure something out though. I'll need to be able to do this regardless of how many nodes there are to put into the array.

    Using groups might help. Add nodes 1-8 to a group and then when you want to find the positions/distances use get_tree().get_nodes_in_group() to iterate through them.

      soundgnome Then it seems I'm having trouble with the actual iteration. I'm getting a Invalid get index 'position' (on base 'Node') error on this line of code:

      indexglobalposition = targetsfound[i].position

      the surrounding code looks like this

      for i in container.get_child_count():
      		targetsfound.append(container.get_child(i))
      	for i in range(0, targetsfound.size()):
      		indexglobalposition = targetsfound[i].position
      		indexdistance = selfglobalposition.distance_to(indexglobalposition) #need to fix
      		if distancetobeat == null :
      			distancetobeat = indexdistance
      			closesttarget = targetsfound[i]
      		else :
      			if indexdistance < distancetobeat :
      				distancetobeat = indexdistance
      				closesttarget = targetsfound[i]

      Invalid get index 'position' (on base 'Node')

      Node is the most basic node type, and has no position. Node2D is the most basic 2D node type that has a position property.

      Spatial is the most basic 3D node type that has a position, named translation. (In Godot 4, Spatial has been renamed to Node3D, and its position is named position.)

      If that doesn't confuse you, let me know and I'll try harder. 😎