Module:UtilsVar: Difference between revisions

From Zelda Wiki, the Zelda encyclopedia
Jump to navigation Jump to search
(RIP, was worth a shot.)
No edit summary
Line 1: Line 1:
local p = {}
local p = {}


local binser = require("binser")
VariablesLua = mw.ext.VariablesLua


function p.set(name, val)
function p.set(name, val)
local var = binser.serialize(val)
local json = mw.text.jsonEncode(val)
mw.getCurrentFrame():callParserFunction("#vardefine", name, var)
VariablesLua.vardefine(name, json)
end
end


function p.get(name)
function p.get(name)
local var = mw.getCurrentFrame():callParserFunction("#var", name)
local json = VariablesLua.var(name)
local val = binser.deserialize(var)[1]
if json == "" then
return nil
end
local val = mw.text.jsonDecode(json)
return val
return val
end
end

Revision as of 19:51, 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 = {}

VariablesLua = mw.ext.VariablesLua

function p.set(name, val)
	local json = mw.text.jsonEncode(val)
	VariablesLua.vardefine(name, json)
end

function p.get(name)
	local json = VariablesLua.var(name)
	if json == "" then
		return nil
	end
	local val = mw.text.jsonDecode(json)
	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