Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Lua properties
--N1ghtSiren -- local function MakeProxy(object, getters, setters) local proxy = {} proxy.__index = function(self, key) --debug message to indicate its work print("\tgetter: " .. tostring(self) .. "["..tostring(key).."]") --try to get getter on same key local func = getters and getters[key] if func then print("custom getter found on key " .. tostring(key)) --calling getter (so we can just self.x = 0 later) return func(getmetatable(self)) else --try to return new proxy value |or| old object value return rawget(getmetatable(self), key) or object[key] end end proxy.__newindex = function(self, key, value) --debug message to indicate its work print("\tsetter: " .. tostring(self) .. "[" .. tostring(key) .. "] = " .. tostring(value)) --try to get setter on same key local func = setters and setters[key] if func then print("custom setter found on key " .. tostring(key)) --call setter (so we can just self.x = 0 later) func(getmetatable(self), value) else --if setter not exists, just set directly to object rawset(getmetatable(self), key, value) end end return setmetatable({}, proxy) end --some constants local KEY_X = "x" local KEY_ID = "unitid" --some constructor function NewUnit(unitid) local obj = {} obj[KEY_ID] = unitid local proxy = MakeProxy( --object obj, --getters { [KEY_X] = function(self) --print("getter") return self[KEY_X] end, }, --setters { [KEY_X] = function(self, value) --basically you can limit types there, so it can be only number if(type(value) == "number")then self[KEY_X] = value else print("error: x must be 'number' type ") end end } ) return proxy end local u = NewUnit("Hpal") u.x = 22 u.y = -10 print(u.x) -- 22 --value without defined setter - works as usual print(u.y) -- -10 --old values before setting up proxy print(u[KEY_ID]) -- Hpal --changing old values u[KEY_ID] = "HvsH" print(u[KEY_ID]) -- HvsH --some weird interactions u.x = function() end -- kinda ok, but value not updates print(u.x) print("") u.x = nil --same print(u.x) print("") --null values print(u.posx) --nil
run
|
edit
|
history
|
help
0
Random numbers in Lua. Range: 1-49. Sets of numbers without duplicates
1
Lua stuff
Monty Hall
test123
finding volume of pyramids (for me, but feel free to use)
sh
Goalless DRAWS
quick sort
test3