Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- MetaRPG: sv.network.lua
- ver 1.0.0000 ~ 12/Feb/2010
- by Xalphox
- --]]
- MetaRPG = MetaRPG or {}
- MetaRPG.Network = {}
- -- MetaRPG.Network.ClientObj
- -- This object is a simplified model for server-client interaction, allowing a scripter
- -- or module maker to simply write NETOBJ(ElementID):FuncName(Param) rather than having
- -- to establish an event and et cetera.
- --
- -- One limitation it has is it cannot recieve returns yet.
- __ClientObj = {}
- -- MetaRPG.Network.GetObject(Element) || NETOBJ(Element)
- -- Gets a networked object of said element, which is an empty table
- -- with it's metatable set to __ClientObj.
- function MetaRPG.Network.GetObject(Element)
- local Obj = {}
- local __Obj = table.copy(__ClientObj)
- __Obj.TargetElement = Element
- setmetatable(Obj, __Obj)
- return Obj
- end
- NETOBJ = MetaRPG.Network.GetObject
- -- __ClientObj:__index(t, k, v)
- -- Emulates a function, fetching the function name and a list of
- -- parameters.
- function __ClientObj:__index(t, k, v)
- local function F(...)
- local FuncData = {}
- FuncData.Name = t
- FuncData.Param = {}
- FuncData.ParamType = {}
- for k, v in pairs(arg) do
- if k ~= "n" then
- FuncData.Param[k] = v
- FuncData.ParamType[k] = type(v)
- end
- end
- return self:__SendCommand(FuncData)
- end
- return pcall(rawget, t, k, v) or F -- : This is incase a method actually exists inside the table.
- end
- -- __ClientObj:__SendCommand(FuncData)
- -- Sends the function and parameters, as parsed by __ClientObj:__index, to
- -- the client; replacing the unsendable data types (functions & userdata) with
- -- string representations.
- function __ClientObj:__SendCommand(FuncData)
- if not self.TargetElement then
- error("You can't directly access the ClientObj object!")
- end
- for k, v in pairs(FuncData.Param) do
- if ParamType[k] == "function" then
- FuncData.Param[k] = "__FUNCTION__"
- end
- if ParamType[k] == "userdata" then
- FuncData.Param[k] = "__USERDATA__"
- end
- end
- return triggerClientEvent(self.TargetElement, "MT.NET.SendCommand", getRootElement(), FuncData.Param)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement