Effect_MaxChargeSprite.lua init() is never called – module instantiation issue?

Hi everyone,

I’m developing a 2D action game using the Playdate SDK, and my project is organized into multiple modules. One of my modules, Effect_MaxChargeSprite.lua, is responsible for handling a special charge-effect animation (for a “drill attack” at maximum charge). However, I’m encountering a problem where the init() function in this module is never being called—even though I can see that the file is being loaded correctly.

Problem Details:

  • When the project loads, I see the message "Effect_MaxChargeSprite.lua loaded!" in the console, which confirms that the file is indeed being imported.
  • However, none of the debug prints I’ve placed at the beginning of the init() function (for example, "Effect_MaxChargeSprite:init() START" or a print showing the image table length) ever appear in the log.
  • As a result, the effect instance is never properly created, and its animation never runs.
  • Other modules (like Player, PlayerEffect, Weapon_Spear01, etc.) seem to be working as intended. I’m also ensuring that the effect instance is being preserved (I store it in a global variable _G.debugChargeEffect and in the effect manager) so that it's not garbage-collected.
  • I’ve double-checked the call stack from the module that instantiates the effect (in PlayerAttack:showMaxChargeEffect()), and I adjusted the parameter order (e.g., ensuring fixed coordinates are defined before they are passed to the constructor). Still, the init() method in Effect_MaxChargeSprite.lua is never executed.

Environment:

  • Playdate SDK (latest version)
  • Using Lua with a standard class system (e.g., class('Effect_MaxChargeSprite').extends(gfx.sprite))
  • Running on Windows

What I’ve Tried:

  1. File Load Confirmation: I added a debug print at the very top of Effect_MaxChargeSprite.lua:

lua

print("Effect_MaxChargeSprite.lua loaded!")

This prints correctly when I load the game.
2. Debug Prints in init(): I placed prints at the very start and end of the init() function:

lua

function Effect_MaxChargeSprite:init(initialImage, imageTable, x, y, frameDelay, loop, duration)
    print("Effect_MaxChargeSprite:init() START")
    assert(imageTable, "Effect_MaxChargeSprite: imageTable is nil!")
    ...
    print("Effect_MaxChargeSprite:init - ImageTable length:", self.imageTable:getLength())
    ...
    print("Effect_MaxChargeSprite:init() END")
end

None of these messages appear in the log.
3. Reference Preservation: In PlayerAttack:showMaxChargeEffect(), I assign the created effect instance to both a field in my PlayerEffect module and a global variable (_G.debugChargeEffect) to prevent it from being garbage-collected:

lua

local fixedX = 50
local fixedY = 210

self.player.effectManager.chargeEffectSprite = Effect_MaxChargeSprite:new(
    initialImage,
    weapon.maxChargeEffectImageTable,
    fixedX,
    fixedY,
    100,
    false,
    500
)

_G.debugChargeEffect = self.player.effectManager.chargeEffectSprite
print("Global debugChargeEffect:", _G.debugChargeEffect)
print("Effect created at fixed position:", fixedX, fixedY)

Later logs show that _G.debugChargeEffect is a valid sprite instance with a proper parent (getParent exists). But still, the init() debug prints are never seen.
4. Global Update Loop: I verified that playdate.update() calls playdate.graphics.sprite.update() properly, so all sprite update and draw methods should be executed normally.

My Question: Why is the init() method of Effect_MaxChargeSprite.lua never being called? Could this be due to Lua module caching, garbage collection, or some other instantiation timing issue? Has anyone encountered a similar problem with Playdate’s module instantiation?

Any insights or suggestions would be greatly appreciated!

Thank you in advance for your help!

To construct an instance of a class in Playdate's object system, you just call the class as if it were a function. In your case, you'd write Effect_MaxChargeSprite() instead of Effect_MaxChargeSprite:new().

3 Likes

Subject: Re: Module Init() Method Not Called - Issue Solved

Hi scratchminer,

I wanted to update you on the issue I was facing with the Effect_MaxChargeSprite:init() method not being called. Thanks to your suggestion, I was able to solve the problem!

The main issue was with how I was creating instances of the Effect_MaxChargeSprite class. Initially, I was using the new method, but as you pointed out, in Playdate's object system, I needed to call the class itself as a function.

Here's the code snippet I was using before:

lua

self.player.effectManager.chargeEffectSprite = Effect_MaxChargeSprite:new(
    initialImage,
    weapon.maxChargeEffectImageTable,
    fixedX,
    fixedY,
    100,
    false,
    500
)

And this is the corrected version:

lua

self.player.effectManager.chargeEffectSprite = Effect_MaxChargeSprite(
    initialImage,
    weapon.maxChargeEffectImageTable,
    fixedX,
    fixedY,
    100,
    false,
    500
)

Additionally, I added an alias for the new method to handle both calling conventions, just to be safe:

lua

function Effect_MaxChargeSprite.new(self, ...)
    return self(...)
end

Thanks to your help, the init() method is now being called correctly, and the special charge-effect animation is working as expected!

I really appreciate your quick response and insightful suggestion. It made a huge difference. If you or anyone else runs into a similar issue, hopefully, this solution helps.

Thanks again!

Best regards, [Jbuta]