0 votes

Hi, I trying spawn...load player node by main script(autoload) with Globals
.....location for the player works for me but player do'nt load to the level.
Thanks for help

main script (autoload) :

extends Node

var player_location = Vector3(0, -14, -82)
var player_spawn = load("res://scenes/player.scn")


func _ready():
    set_process(true)
    set_process_input(true)
    player_spawn = player_spawn.instance()
    Globals.set("player_location", player_location)
    Globals.set("player_spawn", player_spawn)

and level(Node) script to load-spawn player :

extends Node

var player_spawn



func _ready():
    set_process(true)
    Globals.get("player_spawn")
    print(Globals.player_spawn)
    get_tree().get_root().add_child(player_spawn)

this is from output :

**Debug Process Started**
[RigidBody:701]
**Debug Process Stopped**
in Engine by (454 points)

1 Answer

+1 vote
Best answer
Globals.get("player_spawn")

This line doesn't do anything, you didn't store player_spawn anywhere, so in your level node, player_spawn will be null and spawn nothing.

I suggest you do this instead:

extends Node

var player_spawn

func _ready():
    set_process(true)
    player_spawn = Globals.get("player_spawn")
    get_tree().get_root().add_child(player_spawn)

Or, simply:

extends Node

func _ready():
    set_process(true)
    get_tree().get_root().add_child(Globals.get("player_spawn"))
by (29,090 points)
selected by

Thanks very much,
first try didn't work with both code sample.
I get error :

Parent node is busy setting up children, addnode() failed. Consider
using call
deferred ("addchild",child) instead.
so I add call
deferred and this works....great but I get error

 Parameter 'p_child' is null.... in this line : `get_tree().get_root().add_child(player_spawn)`

This works :

extends Node

var player_spawn

func _ready():
    set_process(true)
    player_spawn = Globals.get("player_spawn")
    player_spawn = call_deferred("add_child",player_spawn)
    get_tree().get_root().add_child(player_spawn)

Ah, true... not simple having an instance in an auto-load script tbh^^
Then you can also remove get_tree().get_root().add_child(player_spawn) now, and also storing a second time player_spawn is not required:

extends Node

func _ready():
    set_process(true)
    call_deferred("add_child",Globals.get("player_spawn"))

Yes, that's it.....totally works!
Thanks very much,Zylann

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.