I was really curious on how Roblox implements these objects in their game. The interface just feels so smooth.

For you guys that don't know what I'm talking about, a RBXScriptSignal is an "Event" in roblox -- it fires a set of functions when something happens. And to track how something happens, well, that's not this object's job: essentially Roblox's client tells the object when something happens and will likely fire a private method similar to:fire(). When you want to add a function to the the event, you use the:connect()method. When you want to remove that function from the event, you use :disconnect() on the RBXScriptConnection object returned from :connect() They do a really good job of explaining it here

Anyways, here is my basic mock of it.** I want to know how to do what I'm missing from this interpretation**. Right now, it seems to do everything that a basic Event does, except :connectFirst()and :connectLast()and **especially **:wait()

Here is the ScriptSignal Class:

local ScriptSignal = {}
local ScriptConnection = require(game.ServerScriptService.ScriptConnection)

local meta = {
	__index = ScriptSignal	
	};

function ScriptSignal.new() 
	local newInst = {}
	newInst.functions = {};
	setmetatable(newInst, meta);
	return newInst;
end

function ScriptSignal:connect(listener)
	if type(listener) == "function" then
		local connection = ScriptConnection.new(self)
		self.functions[connection] = listener;
		return connection;
	end
end

function ScriptSignal:fire(...)
	for _, v in pairs(self.functions) do
		v(...);
	end
end

return ScriptSignal

And here is the Script Connection class

local ScriptConnection = {}
local meta = {
	__index = ScriptConnection
};

ScriptConnection.connected = true;

function ScriptConnection.new(scriptSignal) 
	local newInst = {}
	newInst.__scriptSignal = scriptSignal;
	setmetatable(newInst, meta);
	return newInst;
end

function ScriptConnection:disconnect()
	self.__scriptSignal.functions[self] = nil;
	self.connected = false;
end

return ScriptConnection

Edit: forgot the simple parameter for :Fire(), "..."

1 comment

0

I'm probably not tasking these functions right on the thread scheduler thingy :P