Godot Version
4.2
Question
It’s the first time I am working with particles and I came across an issue.
I am currently trying to add a on-death
particle effect to my enemies however it seems like effects are not properly happening
I currently have a GPUParticle2D
scene which I instantiate and add onto my current scene via an autoload function every time it’s requested.
var death_shader_scene: PackedScene = preload("res://scenes/particles/DeathExplosion/DeathExplosion.tscn")
func death(sprite: Sprite2D, spawn: Vector2) -> void:
var new_death: DeathExplosion = death_shader_scene.instantiate()
get_tree().current_scene.add_child(new_death)
new_death.initialize(sprite, spawn)
I want to highlight that the scene itself works perfectly fine and without issue in the editor, and does the particle effect I want. The only things needed to adapt it to another sprite is changing a few shader parameters ( sprite, hframes, vframes etc…)
My GPUParticle2D scene has a script attached to it
class_name DeathExplosion
extends GPUParticles2D
@onready var timer: Timer = %Timer
func _ready() -> void:
timer.timeout.connect(_free)
func initialize(sprite: Sprite2D, spawn: Vector2) -> void:
print("DeathExplosion instance ready and added: ", self.get_name(), " | POS: ", spawn)
var sprite_width: float = sprite.texture.get_size().x / sprite.hframes / 2
var sprite_height: float = sprite.texture.get_size().y / sprite.vframes / 2
(process_material as ShaderMaterial).set_shader_parameter("emission_box_extents", Vector3(sprite_width, sprite_height, 1))
(process_material as ShaderMaterial).set_shader_parameter("sprite", sprite.texture)
(process_material as ShaderMaterial).set_shader_parameter("sprite_hframes", sprite.hframes)
(process_material as ShaderMaterial).set_shader_parameter("sprite_vframes", sprite.vframes)
global_position = spawn
emitting = true
timer.wait_time = lifetime - 0.1
timer.start()
func _free() -> void:
queue_free()
The scene itselfs appears at the correct coordinates ( I added a color rect to check ) however the particle behavior itself seems extremely inconsistent, most of the time only one will be working, sometimes none will be working etc…
And I am not sure why ( no error or warnings etc… )
Does anyone have any idea why it happens?
Screenshots/Video of the issue
Adding the call onto a 2s timer delay