Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | eduo.abreu |
Hello everyone, i’m new to gdscript and i’m having some problems when trying to access another node, i’m trying to save the kinematicbody2d position when i press a button, the node is named “Objeto” and when i use get_node("Objeto) it returns null for some reason.
var coordenadax = get_node("Objeto").get("coordenadax")
var coordenaday = get_node("Objeto").get("coordenadax")
I’m trying to get the coordinates of the node into the button script so i can save it into a .data file. In my KinematicBody2D node, i have this to update the position all he time:
func _process(delta):
var pos = get_position()
coordenadax = pos.x
coordenaday = pos.y
How can i save the node’s position and access it from another node?
In the beginning of the script i have the two variables initialized as:
var coordenadax =0
var coordenaday =0
I tried putting the node Objeto as child of the button node but it gives me
“Attempt to call function ‘get’ in base ‘null instance’ on a null
instance”
Thanks in advance!
What’s the relationship between the node with the script and “Objeto”?
You probably want to use:
onready var objeto_position = get_tree().get_node("Objeto").position
.
onready: This will have the assignment wait until the entire scene is ready.
get_tree(): This will get the entire scene tree.
get_node("Objeto"): This searches the tree for a node named "Objeto".
voxelv | 2018-07-10 04:58
Hey, thanks for the reply! I’m getting
Invalid Call. Nonexistent function ‘get_node’ in base ‘SceneTree’
It’s organized like this
eduo.abreu | 2018-07-10 16:49