0 votes

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.

Godot version v3.2.3-stable_mono_win64
in Engine by (17 points)

1 Answer

+1 vote
Best answer

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 :)

by (1,110 points)
selected by
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 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 webmaster@godotengine.org with your username.