This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

Hi,
I need to instance scene through preload command, but its argument must be a variable.
Preload accepts only string constant...
Any suggestion?
Thanks in advance
-j

in Engine by (1,488 points)

1 Answer

+11 votes
Best answer

Preload is executed at script compile-time, there are no way you can use non-constant string, use load instead.

by (179 points)
selected by

What does it mean to be executed at script compile-time? I thought compiling is what creates the final executable.

I don't really know how it's done, but it seems it works that way: when scene is loading, all it's resources being loaded too (stuff like textures, other scenes used in currently loading scene and finally scripts). Godot exactly knows what to load because you created all the scenes and other nodes like Sprite in Godot Editor and Godot can save this data into scene. Finally, when scripts are loaded, they are parsed, and then compiled in byte-code for faster execution. When script parser sees call to 'preload', it can call ResourceLoader to load resource, specified as string in your 'preload' call. In that case, scene starts only when all resources are loaded.

But when you call 'load', you send message to ResourceLoader to load specified resource while your scene is already playing, which means your game will freeze (or maybe just script will freeze) until resource loads completely.

So basically advantage of compile-time resource loading is that you avoid freezes during gameplay, but scene loads for a longer time. When you use dynamic loading of resources, you save some memory and load stuff you need only when you actually need it. You can even fix freezes in scene by using something like ResourceInteractiveLoader, see http://docs.godotengine.org/en/latest/tutorials/engine/background_loading.html

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 Frequently asked questions and 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 [email protected] with your username.