Copying Animations

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Spheres
Prole
Posts: 2
Joined: Mon Nov 17, 2008 8:39 pm
Location: Lübeck, Germany
Contact:

Copying Animations

Post by Spheres »

Hi Guys,

this may be more a LUA-related problem, but whatever... :roll:
LUA seems to work mainly with references, but is it somehow possible to create a copy, for example of an animation object?

If I do something like:

Code: Select all

anim1 = love.graphics.newAnimation(img_explosion, 40, 40, 0.6)
anim2 = anim1
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?
User avatar
Kaze
Party member
Posts: 189
Joined: Sat Jul 19, 2008 4:39 pm
Location: Dublin, Ireland

Re: Copying Animations

Post by Kaze »

Spheres wrote:Hi Guys,

this may be more a LUA-related problem, but whatever... :roll:
LUA seems to work mainly with references, but is it somehow possible to create a copy, for example of an animation object?

If I do something like:

Code: Select all

anim1 = love.graphics.newAnimation(img_explosion, 40, 40, 0.6)
anim2 = anim1
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):

Code: Select all

    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
User avatar
rude
Administrator
Posts: 1052
Joined: Mon Feb 04, 2008 3:58 pm
Location: Oslo, Norway

Re: Copying Animations

Post by rude »

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: Select all

-- (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.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 83 guests