Maybe not. Maybe yes. Here's a cleaner way to make entirely seperate per-event onEvent branches anyway.
In every case of the different examples, all that needs to be done is to create a new function and reference it in the table with the key as the name of the event that should call it. Multiple events can, of course, call to the same function, by binding the function to 2 event names seperately.
It comes in 3 flavours:
OnEvent and I don't care about which event was fired:
Code: Select all
--write your code for onEvent into functions like this
local function myFirstEventFunc()
-- do stuff
end
--reference functions to have them be called at the right time
local eventFuncs = {
["Level-Start"] = myFirstEventFunc,
["mycustomevent"] = myFirstEventFunc
}
--onEvent doesn't need to be touched
function onEvent(eventname)
if eventFuncs[eventname] then eventFuncs[eventname]()
end
OnEvent and I do care about what was fired:
Code: Select all
--write your code for onEvent into functions like this
local function myFirstEventFunc(eventname)
-- do stuff
end
--reference functions to have them be called at the right time
local eventFuncs = {
["Level-Start"] = myFirstEventFunc,
["mycustomevent"] = myFirstEventFunc
}
--onEvent doesn't need to be touched
function onEvent(eventname)
if eventFuncs[eventname] then eventFuncs[eventname](eventname)
end
Code: Select all
--write your code for onEvent into functions like this
local function myFirstEventFunc(eventobj, eventname)
-- do stuff
end
--reference functions to have them be called at the right time
local eventFuncs = {
["Level-Start"] = myFirstEventFunc,
["mycustomevent"] = myFirstEventFunc
}
--onEvent doesn't need to be touched
function onEventDirect(eventobj, eventname)
if eventFuncs[eventname] then eventFuncs[eventname](eventobj, eventname)
end