|
snowburn -> [suggestion] Community LUA Library (12/18/2014 8:47:02 PM)
|
Hello!! i've downloaded v1.06 and its a giant step forward, congratulation to all the devs :) i think its time for us (the players) to create a lua library which the most used functions to simplify scenario creation. for example: How to assign a possibly dead unit to a mission. Author: Baloogan quote:
function AddToMission(side_name,unit_name,mission_name) local unit = ScenEdit_GetUnit({side=side_name,name=unit_name}) if unit ~= nil then unit.mission = mission_name end end This is a library for making using GetKeyValue and SetKeyValue easier. It allows you to use persisted values through a simple tablelike interface Author: ckfinite quote:
KeyStore = { keys = { }, demarshallers = { prim_str = function(strval) return strval end, prim_num = function(strval) return tonumber(strval) end, prim_nil = function(strval) return nil end, prim_bool= function(strval) return strval=="true" end, prim_func= function(strval) return nil end, prim_tab = function(strval) return nil end } } --metatable local persister = {} local function contains(t, e) for i = 1,#t do if t == e then return true end end return false end function persister.__index(table, key) local result = ScenEdit_GetKeyValue(key) if result == nil then return nil end if (string.sub(result,1,1) == ":") then --unmarshalled return string.sub(result,2) else local slen,val = string.match(result,"([0-9]+):(.*)") local nlen = tonumber(slen) local demarshal_name = string.sub(val,1,nlen) local demarshaller = table.demarshallers[demarshal_name] return demarshaller(string.sub(val,nlen+1)) end end function persister.__newindex(_table,key,value) function marshal(value) local mt = getmetatable(value) local name,marshalled = nil,nil if mt == nil or (mt ~= nil and mt.__marshal == nil) then if type(value) == "string" then name,marshalled = "prim_str",value elseif type(value) == "number" then name,marshalled = "prim_num",tostring(value) elseif type(value) == "nil" then name,marshalled = "prim_nil","" elseif type(value) == "boolean" then name,marshalled = "prim_bool",tostring(value) elseif type(value) == "function" then name,marshalled = "prim_func","" --TODO elseif type(value) == "table" then name,marshalled = "prim_tab","" --TODO else name,marshalled = "prim_str",tostring(value) --default to str end end if mt ~= nil and mt.__marshal ~= nil then name,marshalled = mt.__marshal(value) name = "mrsh_" .. name end return string.len(name) .. ":" .. name .. marshalled end if contains(_table.keys,key) then ScenEdit_SetKeyValue(key,marshal(value)) else table.insert(_table.keys, key) ScenEdit_SetKeyValue(key,marshal(value)) end end function persister.__call(table, key, demarshaller) table.demarshallers["mrsh_" .. key] = demarshaller end setmetatable(KeyStore, persister)
|
|
|
|