Accessing serialized tables in MUSHclient variables, through a metatable

Posted by Cino on Mon 14 Aug 2006 07:51 AM — 2 posts, 14,349 views.

Australia #0
Essentially to have ease of usability of tables with relatively non-volatile script data.

In plugins I have MUSH variables named after characters, that contain the serialized table of the data this plugin looks after.  The currently used character name is located in another plugin.

Basically, I would like to be able to use something like,

char = GetPluginVariable(active_character_pID,"character")
...
v.char.option.omit.that = true
...
if not v.char.option.omit.that then -- carry on printing
...
v.char.stats.defends.dodge = v.char.stats.defends.dodge + 1


and have the MUSH variable be updated after each operation.  So changes persist reloading of scripting, and script variables do not need to be re-initialised when reloading happens.

Attempting to modify

v={} -- variables table
setmetatable(v,{
	-- called to access an entry
	__index=
		function(t,name)
			-- check to see if MUSH variable contains serialized table
			if string.find(GetVariable(name),"^"..name.." = {}"))
				-- loading into local scope
				assert(loadstring("local "..GetVariable(name)))()
				
				-- return copy of this table, how?
			-- otherwise is a regular MUSH variable
			else
				return GetVariable(name)
			end
		end;
	-- called to change or delete an entry
	__newindex=
		function(t,name,val)
			local result
			if val==nil then
				result=DeleteVariable(name)
			-- elseif type(val)=="table" then
				-- SetVariable(name,serialize(name)) -- this doesn't seem right?
			else
				result=SetVariable(name,tostring(val))
			end
			-- warn if they are using bad variable names
			if result==error_code.eInvalidObjectLabel then
				error("Bad variable name '"..name.."'")
			end
		end;
	})


Prepended 'local' so the table will be discarded after the function returns a copy of it.

Sort of stuck on the syntax to return a reference to this table.  Is it possible? I suspect assert and loadstring are not the way to go...  And also how to serialize back in.  Hmm.

Haven't really tested properly because of this, if there are any glaringly obvious logic errors, please point out.

Any suggestions?
Australia Forum Administrator #1
I think you are better off keeping the variables in Lua variables during the operation of the plugin, serializing in and out when the plugin is loaded, or the state is saved. See this post:

http://www.gammon.com.au/forum/bbshowpost.php?bbsubject_id=4960

The serializing out is in the included exampscript.lua files that ships with the client.

As that post suggests, you can do the serializing into a string in the OnPluginSaveState callback, which ensures the variables are serialized before saving the plugin state.

Then to load from the string into the Lua space (when the plugin loads) you just do:

loadstring (GetVariable ("whatever_you_called_it"))

You might also want to look at the setfenv function documented here:

http://www.gammon.com.au/scripts/doc.php?general=lua_base

The example there shows how when you do the loadstring, it can be placed into a table of your choice.