Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are
spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the
password reset link.
Due to spam on this forum, all posts now need moderator approval.
Entire forum
➜ MUSHclient
➜ Lua
➜ Accessing serialized tables in MUSHclient variables, through a metatable
Accessing serialized tables in MUSHclient variables, through a metatable
|
It is now over 60 days since the last post. This thread is closed.
Refresh page
Posted by
| Cino
Australia (17 posts) Bio
|
Date
| Mon 14 Aug 2006 07:51 AM (UTC) |
Message
| 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? | Top |
|
Posted by
| Nick Gammon
Australia (23,158 posts) Bio
Forum Administrator |
Date
| Reply #1 on Mon 14 Aug 2006 08:26 AM (UTC) Amended on Mon 14 Aug 2006 08:31 AM (UTC) by Nick Gammon
|
Message
| 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. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).
To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.
11,716 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top