+1 vote

Every time i switch scenes or play my project some of the animations and stuff are already beeing executed in the background before the whole scene loads.

This makes the game start running before the player can even see whats happening... i wanna make godot wait for the whole scene to load before running anything, is there a simple way to do this? maybe some onload() method of something :S

in Engine by (68 points)

2 Answers

+1 vote
Best answer

Load the worlds first, by that I mean first load all the scenes and all the infos and then start the scene you want to start. Have a function start() or open() in your scenes, you can also add stop(), close(), delete(), etc if you want. When you load, just run the loading functions, when loading is finished, call start(). (You may use the property "visible" to open or hide a scene)

by (805 points)
selected by
0 votes

I've encountered a similar problem and have done lots of testing to figure out how this happens. So far my lead is that once certain nodes are both "ready" and set visible (in my case, a 3d scene), the entire game hangs for a second or two before displaying it.

I got around this by using yield funtions and timers between loading certain parts of the scene and before playing animations (I'm using C# so the example code will be using async methods and the await ToSignal() function instead).
The code should look something like this

public override void _Ready()
{
    InitializeMap();
}
async void InitializeMap() 
{
    PackedScene startupScene = 
    (PackedScene)ResourceLoader.Load(sceneDir);
    Spatial newMap = (Spatial)startupScene.Instance();
    //loading the map file
    await ToSignal(GetTree().CreateTimer(0.01f), "timeout"); 
    //first timer
    AddChild(newMap);//then add map to scene tree
    await ToSignal(GetTree().CreateTimer(0.001f), "timeout");
    //second timer
    AnimationPlayer.Play("startup"); //then startup animation plays
}
by (58 points)
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.