1
\$\begingroup\$

Using 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

GIF SHOWING THE BUG IN ACTION

func _ready():
    var SMOKE_SCENE = preload("res://smoke_explosion/smoke_explosion.tscn").instantiate()
    get_parent().add_child(SMOKE_SCENE)
    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()
    get_parent().add_child(smoke)
    smoke.position = Vector2(0.0, 0.0)
    print("spawned smoke")

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

get_tree().add_child(smoke)

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

EDIT 1: here is the code of the explosion scene

extends Node2D


func _ready():
    %Smoke.material.set_shader_parameter("texture_offset", Vector2(randfn(0.0, 1.0), randfn(0.0, 1.0)))
    %AnimationPlayer.play("explosion")
    await %AnimationPlayer.animation_finished
    queue_free()

The scene tree

\$\endgroup\$
5
  • 1
    \$\begingroup\$ Which node is executing the pre-spawn code? And when? (I.e. in the menus, at the start of the level...) \$\endgroup\$
    – liggiorgio
    Commented Jun 19 at 10:56
  • 1
    \$\begingroup\$ OTOH, the stutter may be caused by either loading graphics or a computationally expensive setup for your explosion. Do you mind sharing the explosion script/setup and info about its sprite/animation? \$\endgroup\$
    – liggiorgio
    Commented Jun 19 at 10:58
  • \$\begingroup\$ @liggiorgio The game manager scene is executing the code, it should execute right when the level starts \$\endgroup\$ Commented Jun 19 at 14:47
  • \$\begingroup\$ After Edit 1: you are using a Node2D with a Control child, which I assume you are animating, and also setting shader parameters every time an explosion is instantiated. This looks expensive, given the platform you're targeting (Web), however, this can only be confirmed by the debugger: open the Debug > Monitor tab when running the game from the editor, and look for any odd spikes when an explosion appears on the screen. \$\endgroup\$
    – liggiorgio
    Commented Jun 20 at 8:51
  • \$\begingroup\$ If needed, consider an alternative using a CPUParticle2D Node for your explosion: it exposes many parameters for randomising position and animation of particles in your games. \$\endgroup\$
    – liggiorgio
    Commented Jun 20 at 8:54

0

You must log in to answer this question.

Browse other questions tagged .