Project version 0.1 (no autoload script)
structure:
Root Node → Level1 Node
level1 script
func set_player_position(_new_pos: Vector2) -> void:
if player == null:
player = PLAYER.instantiate()
var level = get_tree().root.get_node("Level1")
level.call_deferred("add_child", player)
level.call_deferred("move_child", player, 0)
player.global_position = _new_pos
This spawns the player on the spawnpoint, however, other objects looking for “Player” node are now stating the following error.
E 0:00:00:0990 doorsstairs.gd:6 @ _ready(): Node not found: “…/…/Player” (relative to “/root/Level1/Door Container/DoorStairs_a1”).
<C++ Error> Method/function failed. Returning: nullptr
<C++ Source> scene/main/node.cpp:1651 @ get_node()
doorsstairs.gd:6 @ _ready()
When running the game the Player is there and the path as far as i can tell is …/…/Player. I check this via the remote node view.
So this method here does not use autoload script to spawn the player but when i click start game it switches scene to Level1 and then begins to load its nodes/objects.
Project version 0.2 (autoload script)
structure:
Root Node → Menu Node → main_game node
Menu Scene
Two buttons Play and Quit. Press Play and load the main_game node
Global_game_manager script
extends Node
const PLAYER = preload("res://Scenes/Player/Player.tscn")
var player: Player
var player_spawned: bool = false
func _ready():
add_player_instance()
func add_player_instance() -> void:
player = PLAYER.instantiate()
var main_game = get_tree().root.get_node("MainGame")
main_game.add_child(player)
func set_player_position(_new_pos: Vector2) -> void:
player.global_position = _new_pos
When I run the project i get an error,
Attempt to call function ‘add_child’ in base ‘null instance’ on a null instance.
Now I am guessing this has something to do with the autoload script trying to do what I am telling it to do while trying to load the first scene which is the Menu scene. Menu scene does not load or anything else.
Is a signal bets used to delay the autoload script or use the following to wait?
func _ready() -> void: await self.ready
Or am i looking to do something completely different all together?