Hi everyone.
I am making a script for a challenge and i want to be able to show on screen, how much time has passed since the engines of the spacecraft were ignited, in a similar way that you can see how much fuel you have spent, in the 2010 edition/Challenges scenarios.
Here is what i have so far:
I'm able to get the Mission Elapsed Time when i dock with the ISS, but am not so sure as to what i have to add, in order to get the MET while i'm flying.
Any help is much appreciated.

I am making a script for a challenge and i want to be able to show on screen, how much time has passed since the engines of the spacecraft were ignited, in a similar way that you can see how much fuel you have spent, in the 2010 edition/Challenges scenarios.
Here is what i have so far:
Code:
-- read high score list from file
function hscore_read (file,n)
local slist={}
local f = io.open(file,"r")
if f ~= nil then
for i=1,n do
local t = f:read()
if t == nil then break end
local name,time
_,_,name,time = string.find (t, "(.-):(.+)")
slist[i]={name,tonumber(time)}
end
f:close()
end
return slist
end
-- write high score list to file
function hscore_write (file,hscore)
local f = io.open(file,"w")
for i=1,#hscore do
f:write(hscore[i][1]..':'..hscore[i][2]..'\n')
end
f:close()
end
-- convert high score list to a string
function hscore2str (hscore,mark)
local str
if #hscore > 0 then
str = 'High score list:\n\n'..string.format('%s %s', 'Time', 'Name')..'\n----------------------------------'
for i=1,#hscore do
str = str..'\n'..string.format('%08.2f %s', hscore[i][2], hscore[i][1])
if mark==i then str = str..' <====' end
end
else
str = 'No high scores yet!'
end
return str
end
data_path = 'Script/XR2 Challenges/Challenge5.dat' -- high score file
max_score = 10 -- max high score entries
intro = 'Flight challenge:\n\
Launch the XR2 Ravenstar into orbit and dock\
with the International Space Station\
as fast as you can.\
Time will begin counting when you ignite\
your engines. Good luck!\n'
note = oapi.create_annotation()
note:set_pos (0.3,0.05,0.9,0.95)
note:set_colour ({r=1,g=0.6,b=0.2})
note:set_text (intro)
-- read highscore list
slist = hscore_read (data_path, max_score)
-- sanity checks
v = vessel.get_interface('XR2-01')
if v == nil then
term.out ('Could not find XR2-01. Aborting')
return
elseif v:get_classname() ~= 'XR2Ravenstar' then
term.out ('Wrong vessel class. Aborting')
return
end
hd = v:get_dockhandle (0)
hp0 = v:get_propellanthandle(0) -- main tank
hp1 = v:get_propellanthandle(1) -- RCS tank
m1 = v:get_propellantmass(hp0) + v:get_propellantmass(hp1)
-- wait for engines
while v:get_propellantmass (hp0) + v:get_propellantmass(hp1) == m1 do
proc.skip()
end
t0 = oapi.get_simtime()
-- wait for docking event
mate = v:get_dockstatus (hd)
while mate==nil do
proc.skip()
mate = v:get_dockstatus (hd)
if mate ~= nil then -- make sure we are docked to ISS
v2 = vessel.get_interface(mate)
if v2:get_name()~='ISS' then mate = nil end
end
end
t1 = oapi.get_simtime()
-- write out the results
dt = t1-t0
ptext = "Challenge completed successfully!\n"
ptext = ptext..string.format ("Mission Elapsed Time: %.2f seconds", dt)
note:set_pos (0.3,0.15,0.9,0.95)
note:set_size (1)
note:set_text (ptext)
-- enter high score list!
if #slist<max_score or dt < slist[#slist][2] then
ptext = ptext.."\n\nYou made it into the high score!"
note:set_text (ptext)
name = proc.wait_input ('High score: Enter your name:')
idx = #slist+1
for i = 1,#slist do
if slist[i][2] > dt then idx=i; break end
end
table.insert(slist,idx,{name,dt})
if #slist > max_score then table.remove(slist) end
hscore_write (data_path, slist)
end
ptext = ptext..'\n\n'..hscore2str(slist,idx)
note:set_text (ptext)
proc.wait_sysdt(30)
oapi.del_annotation (note)
I'm able to get the Mission Elapsed Time when i dock with the ISS, but am not so sure as to what i have to add, in order to get the MET while i'm flying.
Any help is much appreciated.

Last edited: