Tip: play sprite-sheet VFX in Godot 4 (2D and 3D) with a tiny SpriteFrames helper

I needed a quick way to play sprite-sheet effects (slashes, heals, explosions) in Godot 4 without making a SpriteFrames resource by hand for every effect. This helper builds one from any grid sheet at runtime:

static func frames_from_sheet(tex: Texture2D, cols: int, rows: int, count: int, fps := 24.0) -> SpriteFrames:
    var sf := SpriteFrames.new()
    sf.set_animation_loop(&"default", false)
    sf.set_animation_speed(&"default", fps)
    var w := tex.get_width() / cols
    var h := tex.get_height() / rows
    var n := 0
    for y in rows:
        for x in cols:
            if n >= count:
                return sf
            var frame := AtlasTexture.new()
            frame.atlas = tex
            frame.region = Rect2(x * w, y * h, w, h)
            sf.add_frame(&"default", frame)
            n += 1
    return sf

2D: spawn, play once, free itself

func spawn_fx(sheet: Texture2D, pos: Vector2) -> void:
    var fx := AnimatedSprite2D.new()
    fx.sprite_frames = frames_from_sheet(sheet, 6, 4, 24)
    fx.global_position = pos
    var mat := CanvasItemMaterial.new()
    mat.blend_mode = CanvasItemMaterial.BLEND_MODE_ADD   # glowy magic looks much better additive
    fx.material = mat
    add_child(fx)
    fx.play()
    fx.animation_finished.connect(fx.queue_free)

3D: same frames on a billboard

var fx := AnimatedSprite3D.new()
fx.sprite_frames = frames_from_sheet(sheet, 6, 4, 24)
fx.billboard = BaseMaterial3D.BILLBOARD_ENABLED
fx.shaded = false
fx.pixel_size = 0.01          # 256 px frame ≈ 2.5 m
fx.no_depth_test = true       # optional: always draw on top of the target

A few things that tripped me up:

  • Set set_animation_loop(..., false) or animation_finished never fires.
  • Keep the texture import filter on Linear for painted/glowy effects and Nearest for pixel art.
  • Spawn the effect at the target’s feet for ground effects (quakes, magic circles) and at chest height for hits, otherwise everything looks like it floats.
  • For hit feedback, an effect plus a 0.1 s white flash on the target (material_overlay in 3D, modulate in 2D) reads much better than the effect alone.

If you want something to test with, there are two free sheets (Slash and Heal) here: Free Spell VFX. Disclosure: those effects are generated procedurally by AI-assisted scripts.