saving variables to hard disk and restoring them

Post Reply
aloc_acoc
Posts: 12
Joined: Mon Oct 19, 2009 11:12 pm

saving variables to hard disk and restoring them

Post by aloc_acoc » Thu Oct 22, 2009 11:08 pm

I'm having trouble saving variables between sessions. I saw in another similar post that Heiko had suggested taking a look at table.save() and table.load(), but I don't understand how to make use of those functions. Right now I have this going:

In Scripts:

Code: Select all

createMiniConsole("reagents",0,2,150,215)
setBackgroundColor("reagents",0,0,0,100);
setMiniConsoleFontSize("reagents", 10)
setWindowWrap("reagents", 85);
setTextFormat("reagents",55,0,0,0,0,255,0,0,0);

moveWindow( "reagents" , 1095 , 320 )
hideWindow( "reagents" )
showWindow( "reagents" )
In Aliases:
Pattern: ^rupdate

Code: Select all

clearUserWindow("reagents")
setFgColor( "reagents", 102, 255, 0 )
setBgColor( "reagents", 0, 0, 0 )
echoUserWindow("reagents", "\n")
echoUserWindow("reagents", "\n")
echoUserWindow("reagents", "\n")
echoUserWindow("reagents", "\nFire Reagents " .. firereagent)
echoUserWindow("reagents", "\nWater Reagents " .. waterreagent)
echoUserWindow("reagents", "\nEarth Reagents " .. earthreagent)
echoUserWindow("reagents", "\nAir Reagents " .. airreagent)
echoUserWindow("reagents", "\nGem Reagents " .. gemreagent)
echoUserWindow("reagents", "\nEthereal Reagents " .. etherealreagent)
Pattern: ^sfire (\w+)

Code: Select all

firereagent = matches[2]
expandAlias ( "rupdate" )
Similar alias for water, earth, air, etc. reagents

In Triggers:
Pattern: ^A (\w.*) flares brightly and vanishes\! [Perl Regex]

Code: Select all

if matches[2] == "pinch of sulfur" then 
firereagent = firereagent - 1
end
if matches [2] == "browning mandrake leaf" then 
earthreagent = earthreagent - 1
end
if matches [2] == "feather from a roc" then 
airreagent = airreagent - 1
end
if matches [2] == "shimmering white fish scale" then 
waterreagent = waterreagent - 1
end
if matches [2] == "sunstone" then 
etherealreagent = etherealreagent - 1
end
expandAlias ( "rupdate" )
Now I'm not even sure this is best way of doing what I did, but through all of this, I've got a mini console that captures a display on the side of my screen that says:

Fire Reagents #
Water Reagents #
Earth Reagents #
etc.

My problem is that I don't know how to utilize the table.save() and table.load() so that my reagent numbers carry over each time I exit and relog. Do I put table.save()/table.load() in a new script file, or in the original script file that contains the miniconsole information? Or does it belong in an alias that I have to type each time in order to load it up? Sorry if this is basic, but it's been proving to be a rough transition from mud coding to real coding. To summarize this huge post.. Could you explain exactly how I make my variables save?

Thank you!

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: Trouble saving variables

Post by Heiko » Fri Oct 23, 2009 12:37 am

You usually declare your global variables in script items. All activated script items are compiled when the profile is being loaded or if you click on a script item and either save it manually or click on another script item which invokes the autosave feature.

The table.save() function saves a table to hard disc - or if no table is specified it'll save all global variables.

For example you declare the following variables in a script:

Code: Select all

myVariableTable = {}
myVariableTable.fireagent = 0
myVariableTable.earthagent = 27
myVariableTable.wateragent = 376
myVariableTable.enemies = {"tom","jerry","peter","mike"}

display(myVariableTable)
Then you add an alias ^exit that runs following variable save script:

Code: Select all

local path = getMudletHomeDir() .. "\myVariables.save" -- on Windows
-- on Linux it would be: local path = getMudletHomeDir() .. "/myVariables.save"
table.save( path, myVariableTable );
To load your saved variables you could use a trigger on your login screen or some alias that runs following code:

Code: Select all

local path = getMudletHomeDir() .. "\myVariables.save" -- on windows
-- on Linux: local path = getMudletHomeDir() .. "/myVariables.save"

table.load( path, myVariableTable );
display( myVariableTable )
That's all.

table.save( path ) saves the entire global variables of your current session
table.load( path ) will restore the entire variables of your saved session

This may be even more convenient for some people.

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: Trouble saving variables

Post by Heiko » Fri Oct 23, 2009 11:37 am

I've edited the above table load script to restore the saved table to its state of the time of the last table save call.
The old version of the script loaded the stored table into a table with a different name. This might have been confusing.

Post Reply

Who is online

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