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!

in Engine by (14 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

by (108 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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.