Modifying a scene before loading

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

Hey everyone!
I want to make a battle simulator. In a mani menu I will give the user options to adjust armies size, terrain, etc.

Then I want to launch a fight (in a separate battle scene).
So the battle scene have to be heavily modified before it is loaded.
I found two ways to change scenes, and both have problems.

First one is to use something like

GD.Print(GetTree().ChangeScene("res://BattleScene.tscn"));

However, this way I cannot modify the scene before it is loaded, as it is stored in a file.

Alternatively, I loaded a packed scene and modified it somehow:

PackedScene batllescene_packed = (PackedScene)GD.Load("res://BattleScene.tscn");
battle_scene = (BattleScene)batllescene_packed.Instance();
//modify the scene somehow

Now I have to switch the scenes; I do it with

 GetTree().Root.AddChild(battle_scene);

However, when I do it, the menu scene is still visible underneath the new one!
I guess I have to unload it, but when I do it, the program exits. I tried to unload the menu in two ways:

GetParent().RemoveChild(this);

And, more complicated:

MainMenu main_menu = (MainMenu)GetTree().Root.GetNode("MainMenu");
GetTree().Root.RemoveChild(main_menu);

I have been stuck on this problem for hours; what is the correct way to solve this? Much thx.

:bust_in_silhouette: Reply From: Inces

It is a good habit to organize a project in a way, that won’t force You to swap whole scenes.
I recommend to only use changescene() from title screen to menu to actual game. In game it is better to have some menagement node, that will load and modify levels, adding them as a child one at a time, while never being removed or freed himself. Autoload is very usefull for this.

If You have gone too far with Your project to reorganize it, You could eventually make menu visible to false :slight_smile: