0 votes

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

Thanks in advance!

asked 6 days ago in Engine by eduo.abreu (12 points)

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".

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

1 Answer

0 votes

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

answered 3 days ago by happycamper (97 points)
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.