How to preload dynamically?

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

I have assets that I want to be ready for use in my game, but not necessarily a part of any tree yet. I have a problem with load, because it causes a lot of lag when it’s dynamically loading an asset in for the first time.

  1. Is there a way to reference a string constant from another script for preload? When I try it, Godot’s preload doesn’t accept it. I’m assuming because that very action is dynamic in nature.

  2. I need to reference a big library of potential assets depending on the players choices prior to using the script I want to use preload in. I’d like to have that list in a separate script or at least a dictionary to reference. Is this possible?

  3. How can I have the assets I need preload in without necessarily using them right away?

This loading lag during the gameplay on the android is killer.

:bust_in_silhouette: Reply From: klaas

preload doesnt work that way. preload happens on compile time, therefor it only accepts constants.

question 1
… no

question 2:
you cannot use preload for this. You dont have to have a list in another script. Just write youself a function to initialize this class. Then load your assets before the “action” starts.

question 3:
you dont have to use them right away … have a look

extends BaseClass

var my_asset
var my_other_asset

func load_assets():
	#just load here
	my_asset = load("res://thing to load.tscn")
	my_other_asset = load("res://other thing to load.tscn")

func use_asset():
	#instance it here
	var thing = my_asset.instance()

Thanks for replying.

I’ve done exactly the type of loading situation you’re showing in your example. The problem I’m having is on the mobile version of the game is; I get a lot of lag every time the instance loads up the first time during the gameplay because it’s loading in that asset dynamically for the first time at the moment it’s called and added to the scene.

I want it to be preloaded at compile time for this particular scene so that it doesn’t need to load it during the gameplay

I would like to mitigate that lag entirely using preload. Essentially having the instances loaded at compile time.

Any pointers on achieving this?

Dumuz | 2021-08-13 08:05

You cant!

It doesnt work that way!

Maybe this can help you
Background loading — Godot Engine (stable) documentation in English

But i think you have to rethink your program flow.

klaas | 2021-08-13 08:21

This background loading looks like what I have to dive into, thanks for the info.

Dumuz | 2021-08-13 08:27