Problem changing scenes manually
Asked Answered
V

3

0

Hi! I'm trying to build a level manager for my game to shift around scenes. I tried to follow this guide exactly, but I must be doing something wrong because it's not quite working. Here's the relevant portions of my code:

func _on_level_1_change_level(target):
match target:
"level_2":
new_scene = preload("res://Levels/level_2.tscn").instantiate()
position = Vector2($Hero.position.x, 1036)
change_to()

func change_to():
get_tree().root.call_deferred("add_child", new_scene)
get_node("Level1").call_deferred("free")
$Hero.position = position

It does swap out the level and reposition the hero as intended, but while it loads Level2, it doesn't add it as a child of my level manager, so I can't connect to it and start monitoring for level swaps again. If I try to set up a connect function for it, it gives me a null-instance error, and when I ask it to print all the node's children to double-check, Level2 isn't on there. I feel like I'm doing all the things the guide said to (Technically I'm using a call_deferred when it didn't, but I also tried without that and get the same result) so I'm not sure what's going wrong?

EDIT: As a note, I also checked if it was a child of the main Game Manager node that parents my Level Manager, and it wasn't on that either. I have no idea where it's going, but the level itself does load.

Vday answered 18/2 at 16:2 Comment(0)
H
0

get_tree().root.call_deferred("add_child", new_scene)

This is literally calling add_child on the root node, right?

Perhaps you mean to call add_child on the level node.

Has answered 18/2 at 19:40 Comment(0)
V
0

Wait, I just realized I could solve this with a Signal Bus, so I don't actually need to connect to it for a signal. I do still need my Level Manager to be able to unload the level, though, so I still need to figure out why it's not showing up as a child.

Vday answered 18/2 at 16:30 Comment(0)
H
0

get_tree().root.call_deferred("add_child", new_scene)

This is literally calling add_child on the root node, right?

Perhaps you mean to call add_child on the level node.

Has answered 18/2 at 19:40 Comment(0)
V
0

Has Yep, that did it, thanks!

Vday answered 18/2 at 20:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.