this may be more a LUA-related problem, but whatever...
LUA seems to work mainly with references, but is it somehow possible to create a copy, for example of an animation object?
I can't manage the states of those animation seperatly, because all I got is a reference to "anim1", but I don't want to call "newAnimation()" with the same parameters everytime I need a new instance of that animation...
this may be more a LUA-related problem, but whatever...
LUA seems to work mainly with references, but is it somehow possible to create a copy, for example of an animation object?
I can't manage the states of those animation seperatly, because all I got is a reference to "anim1", but I don't want to call "newAnimation()" with the same parameters everytime I need a new instance of that animation...
Or did I just miss something?
It's likely that newAnimation returns a table;
Tables in Lua are pointers; For example (Taken from lua.org/pil):
a = {}
a["x"] = 10
b = a -- `b' refers to the same table as `a'
print(b["x"]) --> 10
b["x"] = 20
print(a["x"]) --> 20
a = nil -- now only `b' still refers to the table
b = nil -- now there are no references left to the table
There's no way to copy an animation. If you need to create a lot equal animations frequently, I suggest that you put free animations in a buffer, or even preallocate them:
-- (Code is untested)
active_anims = {}
free_anims = {}
-- Preallocate 100 animations.
for i = 1,100 do
table.insert(free_anims, love.graphics.newAnimation( ... ))
end
-- Reuse an existing animation, or create a new one.
function get_animation()
local a = table.remove(free_anims)
if a = nil then return love.graphics.newAnimation( ... ) end
a:reset()
return a
end
-- Make an animation available for reuse.
function free_animation(a)
table.insert(free_animations, a)
end
Animations are not really that heavy to allocate, so I'm not sure you should bother doing this. This is a very neat trick for ParticleSystems, though.