I'm trying to instance my player scene into whatever map I want. When I call the spawn_player function in the ready function the player spawns just fine, however once I use the warp function to load a new map and instance the player again it doesn't work. There are no errors, the map scene loads just fine, but there's no player visible on screen or the remote node tree. I set something to print in the ready function of the player and that does get printed, but the player is nowhere to be found. What's the problem? Sorry if it's something obvious but I've rewrote this multiple times and can't find what's wrong. Here's my code:
1. extends Node
2.
3. var player = ("res://Scenes/Player.tscn")
4. onready var main = get_node("/root/Main")
5.
6.
7. # Setup signal & spawn player
8. func _ready():
9. GameHandler.connect("warp", self, "warp")
10. spawn_player(0, 0)
11.
12.
13. # Warp to a new map
14. func warp(scene, x, y):
15. GameHandler.emit_signal("fade_out")
16. load_new_map(scene)
17. spawn_player(x, y)
18. GameHandler.emit_signal("fade_in")
19.
20. # Load a new map
21. func load_new_map(scene):
22. var current_map = main.get_node("Map")
23. var newScene = load(scene).instance()
24. current_map.queue_free()
25. main.call_deferred("add_child", newScene)
26.
27. # Spawn player at coords specified
28. func spawn_player(x, y):
29. var ysort = main.get_node("Map/YSort")
30. var newPlayer = load(player).instance()
31. newPlayer.position.x = x
32. newPlayer.position.y = y
33. print(str(ysort))
34. print(str(newPlayer))
35. ysort.call_deferred("add_child", newPlayer)
36. print("after ", str(ysort.get_node("Player")))