Persistent Variables?

Bounces
Posts: 18
Joined: Sat Dec 26, 2009 5:44 am

Persistent Variables?

Post by Bounces »

I was reading in the blog and saw this paragraph...
Blaine von Roeder has also created a Variable Persistence System – which means you can now easily save your variables across sessions! The manual will be updated shortly on how to make use of this.
Now I have been greatly annoyed that my backeq variable does not save its value when I quit (it is the variable that tells my sipper and eater triggers what to get the food and drink out of). I have to remember to set it whenever I start a session or I might starve to death...or have to pay attention to the notifications...and honestly, who wants to do that.

So when I read this I was excited...but I can't find in the manual how to do it. Can we maybe get a sneak peek?

User avatar
Vadi
Posts: 4852
Joined: Sat Mar 14, 2009 3:13 pm

Re: Persistent Variables?

Post by Vadi »

Sneak peek is in the downloads page atm.

Skriptz
Posts: 45
Joined: Sun Dec 27, 2009 10:48 pm

Re: Persistent Variables?

Post by Skriptz »

Bounces wrote: Now I have been greatly annoyed that my backeq variable does not save its value when I quit (it is the variable that tells my sipper and eater triggers what to get the food and drink out of).
Can't you just get the variable and put it in a script? So when you load Mudlet it will load that variable with the value you have put in it? Unless of course the variable you have changes values consistently or something.

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

Re: Persistent Variables?

Post by Heiko »

The functionality to save variables has been available for a long time. -> table.save() and table.load() in the api docs.
Just put all of the variables that you like to save/load into some table and save this table to disc before closing the profile or load it when you start playing to restore your saved variables. It's really simple.
Blaine has written a set of new wrapper functions on table.save()/table.load() that try to make this process even easier for inexperienced users. These functions are already included in 1.0.5, but they have not documented so far. Look at the Lua source code in LuaGlobal.lua

Bounces
Posts: 18
Joined: Sat Dec 26, 2009 5:44 am

Re: Persistent Variables?

Post by Bounces »

Vadi wrote:Sneak peek is in the downloads page atm.
Went and looked at the downloads page...
Variable Persistence System -- Easily save settings between sessions!
remember( varName ) - flags a variable to be saved. Variables pending saving are stored in the table _saveTable
loadVars() - loads saved variables out of SavedVariables.lua into _G (global namespace).
saveVars() - saves all variables flagged for saving into SavedVariables.lua. Stores their values at time func is called.
Notes: Variables are loaded with the same name they were saved as. Saving global "myVar" will load it load it back in as global "myVar" when loadVars() is called.
I just want to make sure I am understanding all of this correctly because scripting and triggers and stuff is pretty new to me.

In the alias that I use to set the backeq variable I should call the function remember(backeq). That way whenever I set the variable to a new value it will flag it to save over the previous version in the SavedVariables.lua table. I then need to create 2 new triggers. 1)that fires when the session first starts, which is pretty easy because I can trigger it off of the welcome text, that will call loadVars(). and 2) that fires when the session ends, triggering off of the rent out text probably, that calls saveVars().

And doing that will save my backeq variable and take it from session to session...and I can apply that to any other variable that I want to be moveable as well...correct?

Pretty sweet.

User avatar
Vadi
Posts: 4852
Joined: Sat Mar 14, 2009 3:13 pm

Re: Persistent Variables?

Post by Vadi »

Pretty much, it's as simple as that. In the future, when we add snapshots to variable saving (so you could potentially retrieve your data from a week or a month ago), Mudlet will also auto-call the loadVars() when it's starting Lua and saveVars() before closing it down, so even less work for you.

User avatar
Alexander Divine
Posts: 65
Joined: Mon Dec 21, 2009 7:01 pm

Re: Persistent Variables?

Post by Alexander Divine »

I had some questions regarding the saving of variables. I just grabbed Mudlet 1.0.5 but I can't seem to get the saving to work. I can't find the SavedVariables.lua file, and regardless of whether I know where it is or not, saveVars() and loadVars() don't do a whole lot.

To test it, I altered my targetting alias like so:

Code: Select all

Before:
target = matches[2]

After:
target = matches[2]
remember(target)
saveVars()
I then closed my client, opened it, and called an alias that did loadVars(), then attempted to echo the value of "target." I got nada. :(

User avatar
Vadi
Posts: 4852
Joined: Sat Mar 14, 2009 3:13 pm

Re: Persistent Variables?

Post by Vadi »

You only need to make it remember it once. It gets the value when you do saveVars()

for a test, try this script:

Code: Select all

a = "somevalue"
echo (a)
remember(a)
saveVars()
a = "deleted value"
echo(a)
loadVars()
echo(a)

User avatar
Alexander Divine
Posts: 65
Joined: Mon Dec 21, 2009 7:01 pm

Re: Persistent Variables?

Post by Alexander Divine »

Gave that a shot, came up with "somevaluedeletedvaluedeletedvalue," so it seems something's not working with the loading.

On another note, I actually located the SavedVariables.lua file in my /.config/Aetolia and Mapper/ profile. It contains this:

Code: Select all

return {{},
}
Even after extensive attempts to save variables to it. :(

User avatar
Vadi
Posts: 4852
Joined: Sat Mar 14, 2009 3:13 pm

Re: Persistent Variables?

Post by Vadi »

Sorry, the script should have been

Code: Select all

a = "somevalue"
echo (a .. "\n")
remember(a)
saveVars()
a = "deleted value"
echo(a .. "\n")
loadVars()
echo(a .. "\n")
But indeed its not working for some reason.

Post Reply