Event Systems in Roblox Lua (Part 4 – Full Solution)

Click here for part 3 of this tutorial series, where we used a BindableEvent to take all the work out of creating our own event system. Ok, maybe not all the work, but still…The limitations are real with BindableEvents. Namely the arguments from firing them can’t include special tables, and that’s a problem when we’re working with our custom objects with metatables. Let’s fix it with a little bit of Lua magic.

First off, we need to save the original values when the event is fired. We don’t know what variables are sent to the fire  method, but we are able to save them with a table using the   operator. If you want to know the nitty-gritty, definitely read section 5.2 on variable arguments in Programming in Lua. Let’s add an argument table and an argument count fields in the event itself:

function Event.new()
	local self = setmetatable({
		bindableEvent = Instance.new("BindableEvent");
		argData = nil;
		argCount = nil;
	}, Event)
	return self
end

Note that we don’t have to add these here, by the way. Initializing them to nil does do anything except let us, the programmers, know that these are accessible fields that we’ll use in the future. It’s good practice, so you should get in the habit of showing these fields here.

Next, let’s save the arguments and argument count in these fields by revisiting our fire function:

function Event:fire(...)
	self.argData = {...}
	self.argCount = select("#", ...)
	self.bindableEvent:Fire()
end

Notice how we use   within a table to capture the values into argData . We can now use this table to inspect the possibly many arguments passed. Also, an important detail is the use of the select  function: if you give “#”  as the first argument, it returns the number of arguments passed after it. We just use    again here to do that. Finally, we aren’t even sending anything to the BindableEvent anymore since we’re handling that ourselves. It’s best to remove it for efficiency’s sake.

Okay, now we have all the information saved! How do we use it? We have to get a bit creative with our connect function. Instead of connecting the function directly to the BindableEvent’s Event, we need to connect our own anonymous function. Inside, we’ll unpack  the arguments we saved while firing the event, and use that to call our function.

function Event:connect(func)
	if not func then error("connect(nil)", 2) end
	return self.bindableEvent.Event:connect(function ()
		func(unpack(self.argData, 1, self.argCount))
	end)
end

We use unpack  here in order to return each of the saved arguments from 1 to the number we saved. I’ll save the detailed explanation of unpack for section 5.1 in Programming in Lua. Just know that it’s like returning multiple values from a function, except for unpack  it’s the multiple values are all the indices of the table we give it.

And that should solve our problems with the BindableEvent! We get all the nice threading that Roblox provides internally while working around the table limitations by saving and unpacking them ourselves. Try out the final product with the test code from part 3 and you’ll see the improvement.

And there you have it – just a few ways you can create and use your own custom events within your Lua code on Roblox. You can download the final Event class code here. I had to whittle down my examples to just these 3, so if there’s interest I’ll post others. If you have questions, send me a direct message on Twitter or a message on Roblox. Thanks!

Author: Ozzypig

Roblox developer and computer scientist. I love all things coding and game design!