How to get a variable from another script

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By TheSlimeDev

in my game i want a Remote transform node to move my camera boundaries when i enter an area2d but i keep getting "Invalid get index ‘Isinmap’ (on base: ‘null instance’) and when i run it it shows a blank scene.

parent script :

extends Area2D

onready var Isinmap = false

func _on_00_body_entered(_body):
Isinmap = true

func _on_00_body_exited(_body):
Isinmap = false

child script :

extends RemoteTransform2D
var Parent = get_parent()
var isinmap = Parent.Isinmap
func _process(_delta):
if isinmap != true :
set_remote_node(“/root/Camera2D/limits/cambottomright”)

else:
	set_remote_node("/Parent/bottomright/")

in a backup i deleted all the area2ds and scripts and the game worked fine and i have been working for hours

maybe shortcutting get_parent() ruins it, try to do just var isinmap = get_parent().Isinmap

vania23 | 2022-12-10 18:53

:bust_in_silhouette: Reply From: aXu_AP

As the error says, you are trying to get Isinmap from null. The question is, why Parent is null?

Reason lies in following line:

var Parent = get_parent()

Variables are by default set in object initialization. That is, the moment the node is created. However, at that point the node hasn’t yet been added to the scene tree, so it doesn’t have a parent, so get_parent() returns null.

This can be easily fixed, by telling Godot that we want to set this variable only after the node has been fully initialized and added to the scene:

onready var Parent = get_parent()

thank you
this helped a lot

TheSlimeDev | 2022-12-10 19:36