How custom connections work in OOP

Let’s say we want to make connection using pure lua, no bindable events and no RBXconnections, how am I supposed to do that? i’m curious

3 Likes

you can use tables and metatables to simulate event handling, just:

  1. define event table (a table to store event listeners.)
  2. connect function (a function to register listeners.)
  3. Fire Function (a function to trigger all listeners.)

Here is an example for u:

local Event = {}
Event.__index = Event

function Event.new()
    return setmetatable({listeners = {}}, Event)
end

function Event:Connect(listener)
    table.insert(self.listeners, listener)
end

function Event:Fire(...)
    for _, listener in ipairs(self.listeners) do
        listener(...)
    end
end

-- usage
local idkEvent = Event.new()
idkEvent:Connect(function(msg) print(msg) end) -- this will print "yo"
idkEvent:Fire("yo")
4 Likes

i’ll try it tomorrow as i have to go rn, but thx for help i’ll definetively look at this, also for others who read this, you can write your ideas still

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.