Can't acess Node variables

Godot Version

4.21

Question

I don’t get what is wrong, as I’ve done similar things in the past and it worked.
In this code I’m initializing a player node, this player node gets a variable that stores the initial location he starts on the map, this location is in the scene tree, aswell as the player:


extends Node2D

@export var current_location : Node2D

func _ready():
	self.current_location = $"../Locations/Location_Abisia"
	print(current_location)
	self.position = current_location.position
	print(position)

I print both current_location and current_location.position to check that its working, and it is, output is:
Location_Abisia:<Node2D#30115103956>
(220, 210)

So far the player has indeed those variables assigned to it. Now I have an UI and this UI is going to check where the player is, and will display the appropriate buttons:
(Note: This UI is a child of the main game scene tree, just like the player, the UI comes after(below) the player Node)


extends CanvasLayer

@onready var player := preload("res://scenes/Player.tscn")

func _ready():
	$Control/MC/HBC/VBC/Button_Enter_Settlement.hide()

func _process(delta):
        print(player)
	if player.current_location.location_type == "Settlement":
		$Control/MC/HBC/VBC/Button_Enter_Settlement.show()

Basically it turns off the button by default, then in _process() it checks if the player current location variable is a Node which has a variable string, and if it does, then it shows a button.

I print the player to see if its indeed possible to access it in the _process(), and it is, the output:
<PackedScene#-9223372007310097184>

But when it tries to access the variables within it, gives this error:
Invalid get index ‘current_location’ (on base: ‘PackedScene’).

I’ve dones this kind of things several times and I’ve tried lots of things and for game sake nothing seems to work…

You’ve preloaded but not instanced/added the player node to the scene.

1 Like

That was one of my many attempts to solve it, but your point made me realize that all I had to do was referencing the player scene using the $ sign, so its solved. thanks!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.