Can't get variable from another note

Godot Version

Godot 4+

Question

Hi there, I’m new to Godot and am having problems trying to get a variable from another node.
The following is my scene structure
image

I’m trying to get a variable I’ve stored in MonsterData in Main.

This is my code in MonsterData

extends Node2D

@export var data = "test"

# Called when the node enters the scene tree for the first time.
func _ready():
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	pass

In Main scene, I’m trying to call the variable “data”

extends Node

signal testingSignal

# Called when the node enters the scene tree for the first time.
func _ready():
	print(get_node("MonsterData").data)
	pass # Replace with function body.
	


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	pass
	

func _on_button_pressed():
	testingSignal.emit()
	pass # Replace with function body.


func _on_testing_signal():
	print("Signalfired")
	pass # Replace with function body.

However, upon doing so I face the following error:
Invalid get index ‘data’ (on base ‘null instance’)

Anyone know how to resolve this? Thanks!

easiest way to get it is by, click drag and drop the MonsterData Node2D from SceneTree to Script code, it should generate you a NodePath like

$MonsterData

add .data to get the data from that

another way is to add @export var monster_data:Node2D in main script
then after clicking the Main Node, go to Inspector dock, click drag and drop the MonsterData Node2D to the Monster Data Property shown in inspector dock

to access the monster data, something like this should work

monster_data.data

These don’t seem to work unfortunately.

can you show your newest code for the main script?

Hi there, it’s already in the top post.

did you do what i just shown? just to see you do it correctly, i want to see your newest “don’t seem to work” code

The existing code already does your first part, where I replaced “getNode” with $MonsterData. It doesn’t work.

you dont get node with $MonsterData inside
just $MonsterData will work

I know that. This doesn’t work at all

extends Node

signal testingSignal


# Called when the node enters the scene tree for the first time.
func _ready():
	print($MonsterData.data)
	pass # Replace with function body.
	


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	pass
	

func _on_button_pressed():
	testingSignal.emit()
	pass # Replace with function body.


func _on_testing_signal():
	print("Signalfired")
	pass # Replace with function body.

this direct reference sometimes doesnt work, probably you got it right now, try the @export method

Like I said, that doesn’t work either

extends Node

signal testingSignal
@export var monster_data:Node2D

# Called when the node enters the scene tree for the first time.
func _ready():
	print(monster_data.data)
	pass # Replace with function body.
	


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	pass
	

func _on_button_pressed():
	testingSignal.emit()
	pass # Replace with function body.


func _on_testing_signal():
	print("Signalfired")
	pass # Replace with function body.

did you assign it in inspector?

Yes, I did

then add this before printing it

await get_tree().process_frame

if this still cant
add

await get_tree().create_timer(1).timeout

if this 1 second wait is still null, then something is wrong with your editor