I tried using get node and get node by group but I can't get it to work. Can anyone help me out.
How do I access the position of another node through code
What does your scene look like? What code have you tried? Which node is the script attached to and which node are you trying to get the position of? Without knowing more, it is hard to help find a solution.
If you have a scene like the following:
- Root_Node
- Child_One
- Child_Two
- Child_Two_Child
And you want to get the position of the Child_Two_Child
node in a script attached to Child_One
, then the following should work:
extends Node2D
func _ready():
var root_node = get_parent()
var child_two = root_node.get_node("Child_Two")
var child_two_child = child_two.get_node("Child_Two_Child")
var child_two_child_position = child_two_child.global_position
print ("The child node of Child_Two is at position: " + String(child_two_child_position))
You can also use an exported variable to get the node if that makes it easier. Then you can just select the node you want through the Godot editor by selecting the node and assigning the node path in the inspector:
extends Node2D
export (NodePath) var target_node_nodepath = null
var target_node
func _ready():
# Note: you will need to set target_node_nodepath in the editor!
target_node = get_node(target_node_nodepath)
print ("Position of target node: " + String(target_node.global_position))
Hopefully this helps!
@TwistedTwigleg said: What does your scene look like? What code have you tried? Which node is the script attached to and which node are you trying to get the position of? Without knowing more, it is hard to help find a solution.
If you have a scene like the following:
- Root_Node
- Child_One
- Child_Two
- Child_Two_Child
And you want to get the position of the
Child_Two_Child
node in a script attached toChild_One
, then the following should work:extends Node2D func _ready(): var root_node = get_parent() var child_two = root_node.get_node("Child_Two") var child_two_child = child_two.get_node("Child_Two_Child") var child_two_child_position = child_two_child.global_position print ("The child node of Child_Two is at position: " + String(child_two_child_position))
You can also use an exported variable to get the node if that makes it easier. Then you can just select the node you want through the Godot editor by selecting the node and assigning the node path in the inspector:
extends Node2D export (NodePath) var target_node_nodepath = null var target_node func _ready(): # Note: you will need to set target_node_nodepath in the editor! target_node = get_node(target_node_nodepath) print ("Position of target node: " + String(target_node.global_position))
Hopefully this helps!
Hey, this works for me. But can I ask another question. How to I set the position of a node while instancing? Is there a way to addChild() with global position.
@Anderoo99 said: Hey, this works for me. But can I ask another question. How to I set the position of a node while instancing? Is there a way to addChild() with global position.
To set the position, you just need to change global_position
to whatever position you want. Sometimes I have had issues with changing the position of a node before calling add_child
, so I generally change the position after add_child
just to be on the safe side.
Something like this should work though:
var clone = scene_to_spawn.instance()
add_child(clone)
clone.global_position = Vector2(100, 200)
# If you want to set the position relative to the parent node:
# clone.position = Vector2(100, 200)