Hello programmers new and old, it is I… pumpkin pie. Okay, enough with the cringe, today we’re going to be looking at how to make a custom events for your game. These could come in handy for handling, for example, placement systems, control events, etc.
Here is the link to the module I use for this tutorial.
https://www.roblox.com/library/4232267804/Event-Module
Step 1: Setting Up
For this tutorial, we’re going to be using:
- A module made by @Crazyman32 for his AeroGameFramework, that I slightly edited
- A
Script
inServerScriptService
called controller.
To install the module, just drag it into your workspace from the “Your Items” section in the Toolbox, then drag it into the ServerScriptService
folder in your Explorer.
Step 2: Using the Module
So, we need to load up the module in to the script you made, so on the first line, write this:
local Event = require(script.Parent.Event)
What the require
method does is it loads the module and returns it to the variable you defined it to.
Step 3: Making an Event
There’s an inbuilt function in the module that creates a new function known as .new
. It can be used like so:
local Event = require(script.Parent.Event)
local myEvent = Event.new() -- Creates our event.
Now the myEvent
variable is an event we can use later on. Let’s start by connecting it to a function.
Step 4: Connecting the Event to a Function
With our myEvent
event, we can use it to connect a function to the event with the :Connect
function. The only argument it requires is a callback function
that will be called when the event is fired.
local Event = require(script.Parent.Event)
local myEvent = Event.new() -- Creates our event.
-- Let's connect the event to a function :D
myEvent:Connect(function(arg1, arg2)
-- Print out the arguments
print(arg1, arg2)
end)
Step 5: Firing the Event
We’ve connected it to a function, now lets fire it! (with the :Fire
method)
local Event = require(script.Parent.Event)
local myEvent = Event.new() -- Creates our event.
-- Let's connect the event to a function :D
myEvent:Connect(function(arg1, arg2)
-- Print out the arguments
print(arg1, arg2)
end)
-- Fire event with any amount of arguments
myEvent:Fire(1, 2)
Now when you run the game, it should log the following to the console.
> 1 2
FINISHED CODE
local Event = require(script.Parent.Event)
local myEvent = Event.new() -- Creates our event.
-- Let's connect the event to a function :D
myEvent:Connect(function(arg1, arg2)
-- Print out the arguments
print(arg1, arg2)
end)
-- Fire event with any amount of arguments
myEvent:Fire(1, 2)
Documentation
event = Event.new()
event:Fire(...)
event:Wait()
event:Connect(functionHandler)
event:DisconnectAll()
event:Destroy()
Using ‘Connect’:
connection = event:Connect(func)
connection.Connected
connection:Disconnect()
NOTE ON MEMORY LEAK PREVENTION:
If an event is no longer being used, be sure to invoke the ‘Destroy’ method
to ensure that all events are properly released. Failure to do so could
result in memory leaks due to connections still being referenced.
WHY NOT BINDABLE EVENTS:
This module passes by reference, whereas BindableEvents pass by value.
In other words, BindableEvents will create a copy of whatever is passed
rather than the original value itself. This becomes difficult when dealing
with tables, where passing by reference is usually most ideal.
Thanks for reading, I hope this helped all of you programmers out there. Have a nice day!
HOW HELPFUL WAS THIS TUTORIAL? (1 being worst and 5 being best)
- 1
- 2
- 3
- 4
- 5
0 voters