Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local wep3 = {} -- our "Entity", imagine userdata here
- -- helper, ignore
- local function set_lookup_fallback(what,towhere)
- local m = getmetatable(what)
- if not m then
- m = {}
- setmetatable(what,m)
- end
- m.__index = towhere
- end
- -- Weapon and Entity metatable functions here like normally
- local WeaponFuncs={}
- local EntityFuncs={}
- -- examples
- function WeaponFuncs:IsValid() print"COMMON (WEP)" assert(self==wep3) return true end
- function EntityFuncs:IsValid() print"COMMON (ENT)" assert(self==wep3) return true end
- function EntityFuncs:ent () print"ENT (ENT)" end
- function WeaponFuncs:wep () print"WEP (WEP)" end
- -- weapon looks into entity metatable if it cant find something
- set_lookup_fallback(WeaponFuncs,EntityFuncs)
- -- single entity specific
- -- these tables would need to be created for each entity
- local wep3_data = {} -- this saves entity specific data
- set_lookup_fallback(wep3_data,WeaponFuncs) -- not found, lookup Weapon -> Entity
- local wep3_meta = {
- __newindex=wep3_data,
- __index=wep3_data,
- __tostring=function() return "Weapon[3]" end,
- --etc
- }
- setmetatable(wep3,wep3_meta)
- -- testing
- wep3.asd = 123
- assert(wep3_data.asd==wep3.asd and wep3.asd==123,"data vanished")
- assert(isfunction(wep3.IsValid),"wep3.IsValid not a function")
- print("IsValid",wep3:IsValid())
- wep3:wep()
- wep3:ent()
- print("tostring",tostring(wep3))
Advertisement
Add Comment
Please, Sign In to add comment