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
➜ Clarification of usage of the user define tvar.lua and tables
|
Clarification of usage of the user define tvar.lua and tables
|
It is now over 60 days since the last post. This thread is closed.
Refresh page
| Posted by
| Dakine
(22 posts) Bio
|
| Date
| Wed 22 Apr 2015 08:27 PM (UTC) Amended on Wed 22 Apr 2015 10:41 PM (UTC) by Dakine
|
| Message
| Hi,
I came across this thread and in particular reply 5
http://www.gammon.com.au/forum/?id=10980&reply=5#reply5
I thought this a neat solution for what I wanted based on some of the questions I had raised previously. I admit it might be slightly more complicated solution than what I needed it for right now, however, I like to dive in feet first. Also it will make the code easier to upgrade later.
In my plugin i didn't like that I was repeatedly calling GetVariable and SetVariable. The code works as intended.
Have successfully tested some examples using Nicks advise http://www.gammon.com.au/forum/?id=12844&reply=5#reply5 also http://www.gammon.com.au/forum/?id=6030&reply=11#reply11
I then went ahead and implemented the above tvar.lua.
I've hit a wall. I can create the table, and checking the state file and see the table/variable is serialized etc. I can print the table and it's elements.
However, once it's initially set up, I cannot reassign new values to the "elements". I know it's most likely down to my not fully understanding how tables work. I thought I had resolved this last night before going to bed but by the next morning the solution escaped me!
In terms of the plugin setup everything is working. All requires etc as it should be.
snippet code, run when plugin loads or settings intialised:
function initPots()
var.potVars = var.potVars or {}
local next = next
initVars = {}
var.initVars = {heal = "martini", healnum = 5, threshold = 5}
if next (var.potVars) ~= nil then
if clrNum == "true" then -- code to determine whether to just initalise healnum
for k,v in pairs (var.potVars) do
if string.sub(string.lower(k),5,7) == "num" then
v = 0 -- this worked but doing var.potVars.k = 0 didn't.
print(k,v)
--print ("i ran")
end --if
end --for
end --if
else
var.potVars = var.initVars
end --if
var.potVars.heal = "test" -- this does not assign a new value.
print(var.potVars.heal) --shows default value.
end --initPots
As extra info I use this for onpluginsave. Is this the correct method?
function savePots()
var.potVars = var.potVars
end --savePots
I also thought this would work to clear the table but it doesn't
Dak | | Top |
|
| Posted by
| Nick Gammon
Australia (23,166 posts) Bio
Forum Administrator |
| Date
| Reply #1 on Wed 22 Apr 2015 09:48 PM (UTC) |
| Message
|
function savePots()
var.potVars = var.potVars
end --savePots
I don't see what that would do. Variables are saved automatically anyway.
Quote:
I also thought this would work to clear the table but it doesn't
The table "var" does not contain any data. It is just there for its metatable properties. All assignments to it, or read access from it, redirect to the MUSHclient variables.
So, after calling your function initPots, if you look at the MUSHclient variable potVars, you will see it contains:
{
healnum = 5,
heal = "martini",
threshold = 5,
}
And if you tprint var.potVars you see this:
"healnum"=5
"heal"="martini"
"threshold"=5
Quote:
I also thought this would work to clear the table but it doesn't
Actually it worked when I tried it.
The version I tested against was:
-- tvar.lua
-- ----------------------------------------------------------
-- Accessing MUSHclient variables through the 'var' table.
-- See forum thread:
-- http://www.gammon.com.au/forum/?id=4904
-- Improvements suggested:
-- http://www.gammon.com.au/forum/?id=10980
-- Date: 6th Marh 2011
--[[
* Set a variable by assigning something to it.
* Delete a variable by assigning nil to it.
* Get a variable by retrieving its value, will return nil if the variable does not exist.
Improvements in this version:
* Automatically turns things that look like numbers into numbers
* Automatically turns "true" and "false" into booleans
* Saves amd restores tables by using serialize
Examples:
require "tvar"
var.target = "kobold" -- set MUSHclient variable 'target' to kobold
print (var.target) -- print contents of MUSHclient variable
-- table
var.t1 = { "fish", "chips", "steak", { "subtable", "foo" }, drink = "beer" }
Limitations:
1. Things that look like numbers will become numbers, perhaps unintentionally
2. Things that look like tables will become tables, perhaps unintentionally
3. The words "true" and "false" will become booleans even if they were not before
4. Slightly slower than the "var" module because of the extra tests
--]]
-- ----------------------------------------------------------
require "serialize"
var = {} -- variables table
setmetatable (var,
{
-- called to access an entry
__index =
function (t, key)
local v = GetVariable (key) -- find the variable in MUSHclient variable space
-- no such variable returns nil
if v == nil then
return nil
end -- if
-- convert true and false back into booleans
if v == "true" then
return true
end -- if true
if v == "false" then
return false
end -- if false
-- if can be converted into a number return that number
local n = tonumber (v)
if n then
return n
end -- if a number
-- if variable is in the form {xxx} treat as a table
if string.sub (v, 1, 1) == "{" and string.sub (v, -1, -1) == "}" then
local t = {} -- local environment
local f = loadstring ("ret = " .. v)
-- if loadstring fails, just return as a string
if f then
setfenv (f, t) -- local environment
-- if pcall fails, just return as a string
if pcall (f) then
return t.ret
end -- if executed ok
end -- if loadstring worked
end -- if looks like a table
return v -- return "normal" variable
end, -- function __index
-- called to change or delete an entry
__newindex =
function (t, key, value)
local result -- of SetVariable
if value == nil then -- nil deletes it
result = DeleteVariable (key)
elseif type (value) == 'table' then
result = SetVariable (key, serialize.save_simple (value))
else
result = SetVariable (key, tostring (value))
end -- if
-- warn if they are using bad variable keys
if result == error_code.eInvalidObjectLabel then
error ("Bad variable key '" .. key .. "'", 2)
end -- bad variable
end -- function __newindex
}) -- end metatable
return var
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | | Top |
|
| Posted by
| Dakine
(22 posts) Bio
|
| Date
| Reply #2 on Wed 22 Apr 2015 10:45 PM (UTC) |
| Message
| Thanks Nick for clarification.
It's clear i need to take a few steps back and work on simpler examples until satisfied.
Dak | | Top |
|
| Posted by
| Dakine
(22 posts) Bio
|
| Date
| Reply #3 on Thu 23 Apr 2015 07:09 PM (UTC) |
| Message
| I figured what I was doing wrong. Walking away and coming back to look with fresh eyes helped.
My problem was that I didn't fully understand what the state file was showing and it's relationship with the tables I was creating, thus my logic was always going to be awry.
The solution was quite simple once the logic was there. Based on what I wanted to achieve, I used an 'intermediary' table for changes which didn't require accessing the mush variable(s). This way I minimised GetVariable and SetVariable.
Thanks again for tips/solutions and suggestions.
Dak | | 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.
17,134 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top