in that case make it both of these code snippets functions. i didnt do that because i just wanted to show how timing works. instead of using globals as i had done, stick all the parameters into a table, and return it from the function. this can be passed to the frame function. you can also include in the table the subsystem handle so you dont have to look it up later.
--takes subystem handle, orientation a, oritentation b, and animation length, returns a table or nil on failure
function initAnimation(subSys, oriA, oriB, animT)
--make sure handle is valid
if not subSys:isValid() then
return nil
end
--create a table
local t = {}
--subsystem handle
t.subsys = subSys
--create start and end orientations
t.orientA = oriA
t.orientB = oriB
--how long the animation will run
t.animTime = animT
--setup timing info
t.startTime = mn.getMissionTime()
t.timeStamp = t.startTime + t.animTime
return t --now contains everything we need to know for our animation
end
--now we just call it with the table, returns true when an animation frame is done, false when the the animation fails or is over
function doAnimation(t)
--first make sure the subsystem is still around (it could have been destroyed since the animation started
if not t.subsys:isValid() then
return false
end
--animate while we still have time left
if mn.getMissionTime() < t.timeStamp then
local animT = (mn.getMissionTime() - t.startTime) / t.animTime
local orientNew = t.orientA:getInterpolated(t.orientB, animT)
--apply to subystem now (note that subsystem needs to be a valid subsystem handle)
t.subsys.Orientation = orientNew
else --timestamp expired
return false
end
return true
end
you just stick that code in $OnGameInit: and you can call it from any other hook (in theory). now there is another problem, and that is keeping track of all the animation tables when you have a dozen animating subsystems running all at the same time. this is lua so we can just stick em all into another table then just iterate over it every frame so that all animations can be processed.
--all animations go here
animationList = {}
--same arguments as initAnimation, it just uses a global table instead
function addAnimation(sub, oa, ob, at)
--create an animation
local t = initAnimation(sub, oa, ob, at)
--t may be nil
if t then
--add it to the animation list
table.insert(animationList, t)
end
end
--no arguments, no return, just call once every frame
function runAnimations()
for i=1, #animationList do
local b = doAnimation(animationList[i])
--remove items from the list when they fail or are completed
if not b then
table.remove(animationList, i)
end
end
end
that can go in $onGameInit as well. then you can call addAnimation() at any time during a mission to create an animation for a subsystem. then call runAnimations() once every frame. these functions automatically maintain the animationList table. yo may need to re-init the animationList table in $OnMissionStart: (animationList = {}).