Module:UtilsVar: Difference between revisions
Jump to navigation
Jump to search
PhantomCaleb (talk | contribs) (Created page with "local p = {} local binser = require("binser") function p.set(name, val) local var = binser.serialize(val) mw.getCurrentFrame():callParserFunction("#vardefine", name, var)...") |
PhantomCaleb (talk | contribs) (RIP, was worth a shot.) |
||
Line 10: | Line 10: | ||
function p.get(name) | function p.get(name) | ||
local var = mw.getCurrentFrame():callParserFunction("#var", name) | local var = mw.getCurrentFrame():callParserFunction("#var", name) | ||
local val = binser.deserialize(var)[1] | local val = binser.deserialize(var)[1] | ||
return val | return val | ||
Line 30: | Line 24: | ||
value = function() | value = function() | ||
return p.get(name) or 0 | return p.get(name) or 0 | ||
end | end | ||
} | } |
Revision as of 16:09, 16 May 2020
This module allows for data to be shared between invocations of a module on a given page, using Extension:VariablesLua. Use cases for this include counters and previewing Cargo storage.
Not to be confused with Module:UtilsArg.
local p = {}
local binser = require("binser")
function p.set(name, val)
local var = binser.serialize(val)
mw.getCurrentFrame():callParserFunction("#vardefine", name, var)
end
function p.get(name)
local var = mw.getCurrentFrame():callParserFunction("#var", name)
local val = binser.deserialize(var)[1]
return val
end
function p.counter(name)
name = name or ""
return {
increment = function()
local val = p.get(name) or 0
val = val + 1
p.set(name, val)
end,
value = function()
return p.get(name) or 0
end
}
end
return p