I’m relatively new to Godot and struggling with something that seems really simple but just doesn’t work. I wanted to call get_node to get a specific object “Player” that is a child of the root, but no matter what I do the reference is always assigned null. Not all of this information is relevant, but for the sake of help here’s the entirety of the code of the calling class before it crashes:
extends Node2D
class_name MainLogic
signal enemy_hit(dmg: int)
signal battle_start
const bullet_factory_prefab = preload("res://Data Based Scripts/BulletFactory.tscn")
const battle_popup_prefab = preload("res://UI/battle_popup.tscn")
const battle_manager_prefab = preload("res://managers/scenes/BattleManager.tscn")
static var current: MainLogic = null # Current instance of MainLogic.
# Prefabs/settables
var player: Player # The player character
# Instances of classes for tracking
var battle_popup_instance: BattleUI = null # The current instance of the battle UI.
var bullet_factory_instance: BulletFactory = null # The current instance of the bullet factory.
var battle_manager_instance: BattleManager = null
# General UI variables
var screen_size: Vector2 # Size of the game window.
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
player = get_node("Player")
print(player.name)
It crashes when trying to print the name of the player node, saying Invalid access to property or key 'name' on a base object of type null instance.
And this is the picture of the scene tree. The above code is being called from the script on the Main_Scene node.
I also tried exporting the Player variable and just dragging it into its spot there, but I think I’m also doing something wrong with export variables because they don’t work at all either. Even when I set them they just clear on play, that’s why those other variables are preloaded.
After this I’ll probably just rework the code so that the Main_Scene instances the Player instead, but I want to know why my current approach isn’t working.