Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use preload? #400

Closed
paytonrules opened this issue Jun 8, 2020 · 4 comments
Closed

How to use preload? #400

paytonrules opened this issue Jun 8, 2020 · 4 comments
Labels

Comments

@paytonrules
Copy link
Contributor

Going through my own code, I've been replacing uses of GDScript's preload with a call to the ResourceLoader and using the load function in the _ready hook.

I recently discovered that there is a ResourcePreloader - which is used by the preload function available in godot-rust, but I can't find any examples of it being used. Is this a case where the ResourcePreloader isn't actually useful in GDNative? Or could I actually use it and take advantage of pre-loading?

@ghost ghost added the question label Jun 8, 2020
@ghost
Copy link

ghost commented Jun 8, 2020

Unfortunately, there is no equivalent to preload in languages other than GDScript, because preload is GDScript-specific magic that works at compile time. If you read the official documentation on preload, it says:

Returns a resource from the filesystem that is loaded during script parsing. (emphasis mine)

This is only possible in GDScript because the parser is deeply integrated into the engine.

You can use ResourcePreloader as a separate node in your scene, which will work regardless of whether you're using Rust or GDScript. However, note that if you create a ResourcePreloader in your code, you will still be loading these resources at the time of construction, because there is no way for the engine to know what resources are being added before actually running the code.

I'd suggest that ResourceLoader be used in most cases.

@ghost
Copy link

ghost commented Jun 8, 2020

Also, you can always put a Mutex<HashMap> into a Rust static and load everything you need there during a loading screen.

@paytonrules
Copy link
Contributor Author

Thanks for the detailed answer. If I understand this right it seems like we lose an optimization here. When porting from GDScript to Rust I moved preload calls into ready, and used the global ResourceLoader to load them. It worked fine, but I can imagine in some situations you'd want to use the ResourcePreloader as a node to get the same effect as the preload function.

This might be a good item for issue https://github.com/godot-rust/godot-rust/issues/330

@ghost
Copy link

ghost commented Jun 12, 2020

Added to the tracker. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant