Issue with chaching shader at the start of the game

Godot Version

Godot 4.2.1


when exporting my game to HTML5 it runs completely fine until an explosion effect is spawned on screen for the first time when killing a mob, the game freezes for an entire 2-3 seconds, an easy fix is to just spawn it at the startup of the game so that the freeze would happen at the start instead

bandicam 2024-06-19 12-12-56-899

func _ready():
	var SMOKE_SCENE = preload("res://smoke_explosion/smoke_explosion.tscn").instantiate()
	SMOKE_SCENE.position = Vector2(0.0, 0.0)
	print("spawned smoke")

but for some reason this isn’t working and the smoke isn’t being spawned at the start

func _ready():
	var SMOKE_SCENE = preload("res://smoke_explosion/smoke_explosion.tscn")
	var smoke = SMOKE_SCENE.instantiate()
	smoke.position = Vector2(0.0, 0.0)
	print("spawned smoke")

this also didn’t work, the smoke isn’t being spawned at the start


tried getting the tree instead of the parent but it doesn’t help

I tried putting breakpoints in the _ready function to see if the smoke is actually being instantiated at the start and I noticed that it seems to be instantiated and put in the game before any of game graphics actually appear on screen, so it looks like it’s appearing and disappearing before we even see it, so it fixes nothing !

Any help is appreciated !!!

You shouldn’t preload anything in the _ready() method.

Try this:

const SMOKE_SCENE = preload("res://smoke_explosion/smoke_explosion.tscn")

func _ready():
	var smoke_instance = SMOKE_SCENE.instantiate()
	smoke_instance.global_position = Vector2(0.0, 0.0)
	print("spawned smoke")
1 Like

You may want to use threads instead of “caching shaders”.

1 Like

Thanks for the answer, I already tried this but it didn’t solve anything

I’ll look into the thing you mentioned in your other reply

1 Like

Is position (0, 0) visible when the level starts?

1 Like

Yes it is and I just double checked

That would be possible.

If you have this simple scene tree:


Then this is the order:

  1. Parent _enter_tree
  2. Child _enter_tree
  3. Child _ready
  4. Parent _ready

Notice that Parent will wait for all children to run _ready before running _ready.