LUA Store and reload MFD

wingnut

Donator
Donator
Joined
May 10, 2013
Messages
129
Reaction score
0
Points
16
I want to store a value in my Lua-based MFD when the user exits Orbiter and restore it when the next Orbiter session is started.

But I don't understand how the MFD callback functions
  • writestatus(scn)
  • readstatus(scn)
are supposed to be used.

Do I have to define both functions in my MFD's .cfg file?
For writestatus(scn) I assume that I simply would define the function and assign the required persistent variables like this:
Code:
function writestatus(scn)
    ostime = os.time()
end

How would I retrieve the saved variable then in readstatus(scn)?
 

meson800

Addon Developer
Addon Developer
Donator
Joined
Aug 6, 2011
Messages
405
Reaction score
2
Points
18
If the Lua callback function is like the c++ one, scn should be a file handle. I don't know Lua, but in C++ you use various functions (oapiWriteScenario_xx and oapiReadScenario_xx) to write and read information from the file handle.
 

wingnut

Donator
Donator
Joined
May 10, 2013
Messages
129
Reaction score
0
Points
16
So I would need to "manually" store the information into the .scn file with the scn handle using the LUA I/O functions?

Update: I tried to do use the LUA I/O functions on the scenario handle in the callback functions but even if I only try to read the scn handle the Current State scenario gets corrupted and ends after the MFD sections:
Code:
BEGIN_DESC
Contains the latest simulation state.
END_DESC

BEGIN_ENVIRONMENT
  System Sol
  Date MJD 60247.5989219534
END_ENVIRONMENT

BEGIN_FOCUS
  Ship CTV-Lunar
END_FOCUS

BEGIN_CAMERA
  TARGET CTV-Lunar
  MODE Cockpit
  FOV 50.00
END_CAMERA

BEGIN_HUD
  TYPE Orbit
  REF AUTO
END_HUD

BEGIN_MFD Right
  TYPE User
  MODE Time
 
Last edited:

BEEP

Addon Developer
Addon Developer
Joined
Apr 5, 2008
Messages
153
Reaction score
15
Points
18
The thing is :

readstatus(scn) is called at the start of Orbiter session for the MFD TYPE contained in the

BEGIN_MFD side
TYPE....
VAR1= numvalue or boolvalue etc
VAR2= numvalue or boolvalue etc
END_MFD side

section from the scenery file being loaded for the session.
It allows the developer to initialize key variables of the MFD according to the scenery file. Therefore readstatus(scn) values prevails over the initial setup() and recallstatus() mainly because it runs after them .

Normaly , whithin readstatus(scn) the developer should employ a routine that takes the given scenery file handler, open it for reading, search for the MFD section above, read each line containing the MFD variable, strip the string from the line, apply the value to the variable in the MFD program, and close the scenery file after the job is done.
IMPORTANT readstatus(scn) will initialize vars only for the MFD TYPE stated in the MFD section of the starting scenery file. All MFD's activated at runtime should be initialized by setup() or recallstatus()

writestatus(scn) does the opposite. At the end of Orbiter section ( you pressed CTRL-Q) or at Quicksave it looks for the presently active MFD and activates the writestatus(scn) function for this MFD. There the developer built a routine that takes the given handler, opens the closing or quicksaving scenery file for editing , and writes the MFD current data in this file in a way that the above readstatus(scn) routine will understand in a future load of a new session. Don't forget to close the file at the end.
IMPORTANT writestatus(scn) will act only on active MFD's at the time of ending session. All unactive MFD conditions are lost.



Rgds

Beep
 
Last edited:

wingnut

Donator
Donator
Joined
May 10, 2013
Messages
129
Reaction score
0
Points
16
I think that my current problem is that I'm not using the scn handle correctly to open the scenario file to which the status should be saved or read from.

For example, I tried this callback function in my MFD .cfg just for testing
Code:
function writestatus(scn)
    io.input(scn)
end
and it results in the content of the (Current State).scn file to be cut as posted above, i. e. there is no content after the MFD section although I did not write anything to the file.

Beep, do you have any LUA example for an implementation of writestatus(scn) and readstatus(scn) MFD callback functions you would like to share with me?
 

meson800

Addon Developer
Addon Developer
Donator
Joined
Aug 6, 2011
Messages
405
Reaction score
2
Points
18
Looking at the Lua 5.1 reference here, it looks like you need an io.close()

Opening a file handle through io.input without closing it properly will result in the corruption of the file.
 

wingnut

Donator
Donator
Joined
May 10, 2013
Messages
129
Reaction score
0
Points
16
Unfortunately, this does not solve the problem. Even if I add io.close() the scenario file gets corrupted.

The same happens when I tried other methods described in The Complete I/O Model
 

BEEP

Addon Developer
Addon Developer
Joined
Apr 5, 2008
Messages
153
Reaction score
15
Points
18
I dont have a good example for readstatus(scn) or writestatus(scn). Im trying different
things with these functions...
I'll check but meanwhile try to refit the way you deal with files

try (even at the terminal to see how things work):

filename = 'Config/MFD/MyFile.txt'
bbsfile = io.open(filename,"w")
bbsfile:write('This is my line.')
bbsfile:close()


Maybe scn is not a good handle for io.open() or maybe it's the handle already, the file is already open and just:

function writestatus(scn)

scn:write('MyVariable = 20')
scn:write('My Second Variable = true')

end

will do the job. In this case let Orbiter close the file.
 

meson800

Addon Developer
Addon Developer
Donator
Joined
Aug 6, 2011
Messages
405
Reaction score
2
Points
18
Hmmm. Maybe the file handle is already opened when it is passed to your MFD. Have you tried just reading/writing directly with the filehandle, without opening/closing it?

---------- Post added at 04:16 PM ---------- Previous post was at 04:15 PM ----------

Beep is a :ninja:

Maybe scn is not a good handle for io.open() or maybe it's the handle already, the file is already open and just:

function writestatus(scn)

scn:write('MyVariable = 20')
scn:write('My Second Variable = true')

end

will do the job. In this case let Orbiter close the file.
 

wingnut

Donator
Donator
Joined
May 10, 2013
Messages
129
Reaction score
0
Points
16
I'll check but meanwhile try to refit the way you deal with files

try (even at the terminal to see how things work):

filename = 'Config/MFD/MyFile.txt'
bbsfile = io.open(filename,"w")
bbsfile:write('This is my line.')
bbsfile:close()


Maybe scn is not a good handle for io.open() or maybe it's the handle already, the file is already open and just:
Thanks Beep and meson, this works alright on my console. I was not able to write directly to the scn handle though.
What worked better is to open the (Current state).scn file and ignoring the scn handle:
Code:
function writestatus(scn)
    filename = 'Scenarios/(Current state).scn'
    fhandle = io.open(filename,"w")
    fhandle:write('My line.')
    fhandle:close()
end
which produces this file:

MrJkzsz.jpg


Now let's try if this method will result in a proper scenario file when I read the file's content first and insert the bits from my MFD into the correct slot instead of writing something into it directly...

More unexpected things happened: I can open the (Current state).scn file fine in the LUA console in Orbiter and append new strings to it or overwrite it but in the callback function appending does not work at all and opening the .scn file in 'w' mode overwrites the first few lines in the .scn file while the remaining lines remain in the file like posted in the screenshot above.
 
Last edited:

BEEP

Addon Developer
Addon Developer
Joined
Apr 5, 2008
Messages
153
Reaction score
15
Points
18
Dear Wingnut,

After some testing, it seems to me that we are stuck with the problem. The scn handle is useless probably specific for the C++ OAPI functions such as oapiWriteScenario(FILEHANDLE scn,char *item, int i) which makes wonders there but is not available to us poor Lua coders. < sound of a desperate howling through the moonlight night>

The scripting system brings some of these surprises. :( It's still new.

Hope you can now at least find a circunvention to solve your problem some other way.



Regards,

Beep
 

wingnut

Donator
Donator
Joined
May 10, 2013
Messages
129
Reaction score
0
Points
16
Yes, it seems like I cannot work with the scenario file without breaking it.

I store my variables in a separate file now which works without further complications until now.

Thank you Beep and meson for your support and advice.
 

Thunder Chicken

Fine Threads since 2008
Donator
Joined
Mar 22, 2008
Messages
4,375
Reaction score
3,307
Points
138
Location
Massachusetts
Apologies for this necropost (10 years), but for those struggling with getting MFD properties in and out of the scenario file using Lua and found this thread through the forum search function, it seems that the situation isn't improved with Orbiter 2016. Here are the results of my experiments:

Code:
function readstatus(scn)

    if scn then -- scn is true

        --message = type(scn) --returns type 'userdata'
        --message = type(getmetatable(scn)) --returns 'table'
        --message = table.concat(getmetatable(scn)) --empty table
        --scn_table = getmetatable(scn)
        --message = tostring(#scn_table) --0 elements in table
        --message = tostring(scn) --returns 'userdata: 00A7EB84'

    end

end

Still not clear whether this function is usable at all. Any effort to read anything from scn fails. :(
 
Last edited:
Top