SHARE
TWEET

Untitled

a guest Nov 19th, 2019 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local Emitter = {}
  2.  
  3. function Emitter.new()
  4.     local emitobj = {}
  5.     emitobj.Listeners = {}
  6.     return setmetatable(emitobj, {__index = Emitter}) --Metatable __index just means when you try to access a nil index, itll try calling a func in Emitter table
  7. end
  8.  
  9. function Emitter:Connect(Func)
  10.     table.insert(self.Listeners, Func)
  11. end
  12.  
  13. function Emitter:Emit(...) -- ... is variadic for unknown params
  14.     for Index, Func in pairs(self.Listeners) do
  15.         Func(...)
  16.     end
  17. end
  18.  
  19. return Emitter
  20.  
  21.  
  22. --example usage:
  23. local Emitter = require("Emitter")
  24.  
  25. local OnMessageEmitter = Emitter.new()
  26.  
  27. function OnMessageCallback(Message)
  28.     print(Message)
  29. end
  30.  
  31. OnMessageEmitter:Connect(OnMessageCallback)
  32.  
  33. --You can then keep track of the emitter and later do:
  34. Emitter:Emit("This is a message")
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top