Storing Information

Post Reply
solarkactus
Posts: 20
Joined: Sat Jan 29, 2011 12:05 am

Storing Information

Post by solarkactus »

I want to be able to store information so that it will be accessible again after I've restarted Mudlet.
(It's going to be simple information, so I'm not looking to make a whole database or anything.
Sorry in advance if this is already in the manual. Please point me in the right direction.)

Say I have something like this:
if currenthealth/maxhealth < sippingpoint then sip(Health) end

And when I log in, I've already decided I want to sip at 50% health:
sippingpoint = 0.5

I can change sippingpoint to 10% or 75% or whatever while I'm playing, but it will eventually revert back to 50% next time.
How can I keep a variable/setting ...thing?

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

Re: Storing Information

Post by Heiko »

Check out table.save(...) and table.load(...) in the api documentation. In a nutshell, you put your variables into a table and write this table to a file and load it in a new session.

solarkactus
Posts: 20
Joined: Sat Jan 29, 2011 12:05 am

Re: Storing Information

Post by solarkactus »

Hi I looked at table.save() and table.load() in the mudlet manual, but wasn't able to figure it out.

I created a table:
settings = {"on","off","off"}

Double checked to see that it was there; It was:
table {
1: 'on'
2: 'off'
3: 'off'
}

Then I tried:
table.save(getMudletHomeDir().."\\myfile","settings")

I closed Mudlet, saved my profile, reopened Mudlet and tried:
table.load(getMudletHomeDir().."\\myfile","settings")

To no avail. No file was created in that folder either.
Can you tell me what I've done wrong?

User avatar
Omit
Posts: 190
Joined: Sun Aug 01, 2010 10:54 pm
Location: Middle Earth
Contact:

Re: Storing Information

Post by Omit »

I was giving this a little thought and threw this together for you....
It is a very simple database to store numeric variables in....

Paste the following into a script window and save it
Code: [show] | [select all] lua
local variables = db:create("storedata", {vars = {k="",v=0,_unique = {"k"},_violations = "REPLACE"}   })

function OvarSave(key,value)
    db:add(variables.vars, {k=key,v=value})
echo("Saved var:"..key.."="..value)
end

function OvarGet(key)
local  results = db:fetch(variables.vars,db:eq(variables.vars.k, key))
local value = results[1]["v"]
echo("Found var:"..key.."="..value)
return value
end
this will give you 2 new functions to use....

OvarSave(key,value)
Where 'key' is any text value(name of your variable) and 'value' is any numeric value (will replace an existing value if any)
and
result = OvarGet(key)

This will save the variable in a database to be recovered whenever (even after restarting mudlet)

solarkactus
Posts: 20
Joined: Sat Jan 29, 2011 12:05 am

Re: Storing Information

Post by solarkactus »

Thanks for taking the time to put the script together. It works great, even with some short strings I tried.

In the end, I was able to get table.load() and table.save() working afterall - I just needed to omit the quotes around the name of the table. ^^; a stupid mistake on my part.

so thank you both!

Filion
Posts: 93
Joined: Sat Mar 26, 2011 4:21 pm

Re: Storing Information

Post by Filion »

Omit, what happens if i try to load a variable that wasnt' saved?

User avatar
Omit
Posts: 190
Joined: Sun Aug 01, 2010 10:54 pm
Location: Middle Earth
Contact:

Re: Storing Information

Post by Omit »

Likely it will throw an error (I have not used the db functions in over a year and not at home to try it myself).
In order to "fix" that you would only need to check if results[1]==nil in the OvarGet function and return that case in whatever format you desire.

If that doesn't work for you, let me know and I will throw together anouther example. (I should have some time in the next few days.)

User avatar
tsuujin
Posts: 695
Joined: Fri Feb 26, 2010 12:59 am
Location: California
Contact:

Re: Storing Information

Post by tsuujin »

I'm a fan of database storage myself, though you do have to take care to regulate your commits. Too many and your system slows down. Too few and you risk data loss.

Post Reply