Advertisement
9_cVv

events

Sep 17th, 2021 (edited)
670
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.33 KB | None | 0 0
  1. local class = {}
  2. local events = {}
  3.  
  4. local mt = {
  5.     __index = function(t, i)
  6.         for name,func in pairs(t) do
  7.             if name:lower() == i:lower() then
  8.                 return func
  9.             end
  10.         end
  11.     end
  12. }
  13.  
  14. function class:GetEvents()
  15.     return events
  16. end
  17. function class:GetEvent(id)
  18.     return self[id] or nil
  19. end
  20. function class:CreateEvent(id)
  21.     local event = {}
  22.     local connections = {}
  23.  
  24.     function event:Connect(func)
  25.         local connection = {}
  26.         connection['func'] = func
  27.         connection['enabled'] = true
  28.         function connection:Disconnect()
  29.             connection['func'] = nil
  30.             connection['enabled'] = false
  31.             connection = nil
  32.         end
  33.         setmetatable(connection, mt)
  34.         table.insert(connections, connection)
  35.         return connection
  36.     end
  37.     function event:DisconnectAll()
  38.         connections = {}
  39.     end
  40.     function event:Fire(...)
  41.         for i,connection in pairs(connections) do
  42.             if connection and table.find(connections, connection) and connection.func and connection.enabled == true then
  43.                 connection.func(...)
  44.             end
  45.         end
  46.     end
  47.  
  48.     setmetatable(event, mt)
  49.     local ID = id or #self
  50.     self[ID] = event
  51.     return event
  52. end
  53. setmetatable(class, mt)
  54.  
  55. return class
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement