Calling a Function on an Entity Using the String Name
0 replies, posted
Trying to add an event system for my entity, when events happen on the server they get dispatched to the client
-- On the server...
function ENT:DispatchEvent(event, ...)
local players = {}
for k, v in pairs(self.Players or {}) do
if not IsValid(v.Ent) then continue end
table.insert(players, v.Ent)
end
net.Start("SWVREvent")
net.WriteString(event)
net.WriteEntity(self)
for _, v in ipairs({...}) do
local t = type(v):gsub("^%l", string.upper)
t = isnumber(v) and "Float" or isentity(v) and "Entity" or isbool(v) and "Bool" or t
net["Write" .. t](v) -- This is some ghetto hack...
end
net.Send(players)
end
-- On the client...
function ENT:OnCritcal()
print(net.ReadEntity()) -- Just as an example
end
function ENT:DispatchEvent(event)
self[event](self)
end
net.Receive("SWVREvent", function()
print("EVENT RECEIVED")
local event = net.ReadString()
local ent = net.ReadEntity()
ent:DispatchEvent(event)
end)
The issue is, calling self[event](self) does not work. Trying to access such a function always returns nil. I looked up various ways of calling a function like this but I'm pretty sure I'm doing it right. The only reason I potentially see this failing is if custom entity functions aren't stored in the metatable.
I've already confirmed everything else about the code works (including my really weird way of writing networked data).
I printed the metatable of my entity and none of my custom functions were there, so I guess there must be some other way to access them?
Sorry, you need to Log In to post a reply to this thread.