I have a module with following functions:
function ANIM.create(id, initial_anim, initial_flip)
local instance = {
id = id or go.get_id(),
my_flip = initial_flip or true,
anim = initial_anim or hash("idle")
}
return setmetatable(instance, { __index = ANIM })
end
function ANIM:get()
print("FFF", self.anim, self.my_flip)
end
function ANIM:play(anim)
assert(anim, "You must provide initial anim for animator")
if (not self.anim) or (self.anim ~= anim) then
msg.post("#sprite", "play_animation", { id = anim })
self.anim = anim
end
end
And then I instantiate an animator’s instance and implicitly pass self object to each function called on the instance, like this:
local animator = require "utils.animator"
function init(self)
self.sprite = animator.create()
self.sprite:play(hash("run"))
And now it seems like I can play different animation for each of instances of different game objects, I hope it is a good solution.
Earlier, when I wasn’t returning self from a module I tried to just:
self.sprite = require "utils.animator"
But there were some unexpected bugs so I gave up (strange, because I thought it could work)