CanvasModulate not visible after loading an instanced scene.(level loading)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By SCV-ready

Hello. i have been trying to set up a level loading system that does not switch scenes.
in each level there is light2D’s and a canvas modulate along other things.

when i instantiate a level and add it to main as a child it loads, but for some reason the CanvasModulate is not doing anything.

here is my loader code:

func load_level(file : String):
var new_level = load(file)
var level = new_level.instance()
level.name = "level"
if $"/root/Main/".has_node("level"):
	var old_level = $"/root/Main/level"
	for child in old_level.get_children():
		child.queue_free()
		
	old_level.replace_by(level)
	old_level.queue_free()
$"/root/Main".add_child(level)

this code runs in a singleton.

any idea how to fix the issue/ how to implement non whole scenetree levels?

For future reference if someone gets this obscure bug:
managed to work by using remove node on the old level and queue_freeing it afterwards instead of replace by.

SCV-ready | 2019-10-17 18:10

:bust_in_silhouette: Reply From: Ancalabro

I just wanted to thank you for sharing this. I have been having a near identical issue. Your solution did not immediately work for me. To fix things I also had to add a one game-loop delay between the old scene being removed and the new scene being created. I assume something about two CanvasModulate2d nodes operating on the same layer causes problems? I was able to solve my version of the issue by deferring the creation of the new scene, whether by using call_deferred() or the briefest of timers.

Again, just adding to this in case someone else comes along. Thanks for sharing your solution and starting me in the right direction!

I am glad my debugging efforts were useful :slight_smile:

SCV-ready | 2021-01-04 17:30

:bust_in_silhouette: Reply From: Pavel Kotlyar

seems like a known bug CanvasModulate does not update after changing scenes · Issue #39182 · godotengine/godot · GitHub and yes, calling remove_child(old_scene) before old_scene.queue_free() a workaround for now.

i think you meant

old_scene.remove_child(old_scene.get_node("CanvasModulate"))

then

old_scene.queue_free()

that’s what works for me

posei | 2022-05-21 23:12