+3 votes

Hi,

I'm preloading some elements on the scene with preload() function. It should make creation of an instance of preloaded scene instantly available, but it doesn't seem to work entirely as intended. Each time a resource or scene, which was preloaded, is spawned for the first time, there is a small freeze of the game and some frames are skipped.

For example, first enemy I spawn cause little freeze, but each next of the same type is loaded instantly without freezes. If I want to spawn other type of enemy (different scene), again, first spawn causes mini freeze, and next is OK.

Am I missing something important here? I want to have all the required resources preloaded at the beginning of the level, so they are available instantly later on.

How can I do this?

in Engine by (137 points)

It's not only in the characters. It's on everything I load this way. I've got the BadGuys, but also for example a scene containing a single arrow, that I spawn, when BadGuy shoots his crossbow. Also an explosion, that have no mesh at all, only particle effects, omni light, sound and area to detect who is affected by the explosion.

There are some textures on every scene, but it cannot be, that every texture I use for every item I spawn is somehow broken.

No texture is broken :) There is a bug in the engine, that causes the lag and it's triggered when specific object appears. In my case it's when units shoot, and I preload the muzzle, I think it's due to the emission setting on the material. There must be some common lag trigger for your scene, you just have to find out what is it. I would guess that it has to do with lighting, because that is also in my case.

Well... I do have many light sources with dynamic shadows everywhere, because light and shadow plays important role on my game. Also, I have the same lag when I enable shadows on Directional Light node, so... you might be onto something.

I am trying to find a solution to this as well. I found something, but it's extremely hacky and not sure if I'm going to use it yet, but figured I'd post it here.

In your main UI canvaslayer, add a Node2D called smooth_preloads. Then, create var smooth_preloads = [] and a func called smooth_preload. Now, append the paths of your scenes you want to be smoothly loaded.

func smooth_preload():
    for path in smooth_preloads:
        var t = load(path).instance()
        smooth_preloads_node.add_child(t)
        yield(get_tree(), "idle_frame")

    for node in smooth_preloads_node.get_children():
        #node.queue_free()
        pass

There are some caveats with this:
- The scene must be VISIBLE in your player's viewport.
- You cannot make it invisible or hide it.
- If you run queue_free after they are loaded, it won't work.

I initially was going to use this solution because I figured I could set the opacity to 0 and then maybe receive a signal once the preload is called, and queue_free it from smooth_preloads_node. However, it seems like even if the opacity of the node is set to 0, the hiccup still happens. I'm stumped.

My solution does work, but the preloaded scene has to be visible on the player's viewport first. Tested on exported builds as well.

Still not sure how to go about this... but it's essential. For example, if a player joins a game and uses a skill. There should be no hiccup at all if we are using preload

:( No idea what to do as there is no way I'm going to have all the skills/scenes visible on the screen lol.

I am seriously contemplating on switching game engines. However, it's been nearly half a decade there is no turning back. But good lord, it's been hell. So many bugs and issues, when does it stop! :X

edit: I am going to stay optimistic though and hope it can be fixed

~~Hmm. It seems like this is happening if you are calling duplicate() on a material (however, it only hiccups once then it's smooth as butter). I'm going to do some more testing.~~

edit: Nevermind. Nope it doesn't. I'm totally stumped lmao.

Please log in or register to answer this question.

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.