Saving node position when pressing a button

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: 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’

Here's how it's organized

It’s organized like this

eduo.abreu | 2018-07-10 16:49

:bust_in_silhouette: Reply From: happycamper

I wouldn’t put a body as a child of a button. If you move objeto up a layer and have the call function in the buttons script you would need get-node (“…/objeto”) the …/ traverses back up a directory. You might also consider using a signal to do this