saving variables

tarrant
Posts: 49
Joined: Thu Apr 15, 2010 10:36 pm

saving variables

Post by tarrant »

I made an alias to
Code: [show] | [select all] lua
local path = getMudletHomeDir() .. "/myVariables.save"
table.save( path, myVariableTable )
An in a script, i have
Code: [show] | [select all] lua
local path = getMudletHomeDir() .. "/myVariables.save"
table.load( path, myVariableTable )
But once i load my variables, i get a few errors.
Lua syntax error:[string "--Globals..."]:54: attempt to call method 'move' (a nil value)
ERROR: Lua compile error. compiling script of script:chatStartup
Lua syntax error:mudlet-lua/lua/GUIUtils.lua:813: attempt to call field 'Process' (a nil value)
ERROR: Lua compile error. compiling script of script:Console
Anyone know what's wrong? Or how to fix this?

Denarii
Posts: 111
Joined: Thu Dec 03, 2009 10:54 pm

Re: Weird errors

Post by Denarii »

Process is a helper function for c/h/decho. It's contained in a table called Echos. Have you created your own Echos table?

tarrant
Posts: 49
Joined: Thu Apr 15, 2010 10:36 pm

Re: Weird errors

Post by tarrant »

Not that i know of. Oddly enough, the problem went away. Don't know what happened, can't seem to re-create it.
So its all good for now. thanks.

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

Re: Weird errors

Post by Omit »

I have noticed that if you try to call a function before it is defined it will fail in this way....hmm... not sure I am explaining it well....

Example: this will fail with this kind of error
Code: [show] | [select all] lua
SomeFunction()

function SomeFunction()
local x = 1
end
Example: this will not
Code: [show] | [select all] lua
function SomeFunction()
local x = 1
end
SomeFunction()
... but it will only fail the first time.. I suspect that you may end up seeing this again when you restart the client.

tarrant
Posts: 49
Joined: Thu Apr 15, 2010 10:36 pm

Re: Weird errors

Post by tarrant »

Ok, the problem is back. It didn't really go away. i just forgot i disabled the loadvars part.

i put both in aliases loadvars and savevars
Code: [show] | [select all] lua
local path = getMudletHomeDir() .. "/myVariables.save"
table.save( path, myVariableTable );
display( myVariableTable )
Code: [show] | [select all] lua
local path = getMudletHomeDir() .. "/myVariables.save"
table.load( path, myVariableTable );
display( myVariableTable )
Both just display nil.
Help! This is so frustrating...

Or what other ways do people use to save their variables between session.

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

Re: Weird errors

Post by Heiko »

Well, if the table that you want to save isn't defined or doesn't contain any content when you save it, it cannot possibly contain anything when you load it again.
The recommended way to save your user data is to use table.save() and table.load() directly. There is no known bugs with those functions and they have been used successfully for a long time.
1. Make sure that your file path is correct. (e.g. your above code won't work on windows)
2. Make sure that the table that you want to save contains the correct content before you call table.save().

For debug purposes you can open the saved data file in a text editor and verify its content.

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

Re: Weird errors

Post by Heiko »

Loading and saving user variables should generally be done by event handlers that handle profile load and exit events.

tarrant
Posts: 49
Joined: Thu Apr 15, 2010 10:36 pm

Re: Weird errors

Post by tarrant »

Ok, i made 2 scripts.
1. Load Vars
registered event handler: sysLoadEvent
in the script box:
Code: [show] | [select all] lua
local path = getMudletHomeDir() .. "/myVariables.save"
table.load( path )
2. Save Vars
registered event handler: sysExitEvent
in the script box:
Code: [show] | [select all] lua
local path = getMudletHomeDir() .. "/myVariables.save"
table.save( path )
It seems to be loading fine, but not too sure why it doesn't save. I'm using mudlet on a mac. What's the right way to quit such that it saves?

Also,
Code: [show] | [select all] lua
WindowWidth=0;
WindowHeight=0;
WindowWidth, WindowHeight = getMainWindowSize();
createLabel( "ammoTypeConsole", WindowWidth - 300, 0, 300, 300, 0 )
setBackgroundColor( "ammoTypeConsole", 0, 0, 0, 0);
resizeWindow( "ammoTypeConsole", 105, 20 )
moveWindow( "ammoTypeConsole", 910, WindowHeight-21 )

clearUserWindow("ammoTypeConsole")
cecho("ammoTypeConsole", [[<p style="font-size:12pt"><b><center><font color="white">Ammo: ]].. ammoTypeCur .. [[</font></b></p></center>]])
This script is giving rise to this error
Lua syntax error:mudlet-lua/lua/GUIUtils.lua:813: attempt to call field 'Process' (a nil value)
ERROR: Lua compile error. compiling script of script:AmmoTypeConsole
Looks like a bug to me since i don't have any Process in my script. I'm probably mistaken because i don't know anything about it. Can anyone spot anything that'll give rise to such a problem? Or what other info is needed to deduce this?

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

Re: Weird errors

Post by Heiko »

You forgot to specify which table you want to save. The syntax is: table.save( path, myTableThatIwantToSerialize )
The same applies to table.load(). Those are general functions that can be used to serialize any Lua table or restore the data from a file to any given table. When restoring a table from file you need to tell the function which table you want to hold the content as the table name is not part of the file.

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

Re: Weird errors

Post by Heiko »

Denarii wrote:Process is a helper function for c/h/decho. It's contained in a table called Echos. Have you created your own Echos table?
We should probably rename the Echos table to something that is less likely to be chosen for a user variable.

Post Reply