purpose
I found that in the process of playing turn based games, whenever I want to write a new skill, I may need to create a new trigger event, and then there is a lot of difficult to maintain code. So I looked up some information and found a good event system. I hope someone can give some improvement suggestions based on this
GameBaseEvent
local module = {}
export type GameBaseEvent = {
Name:string,
Enabled:boolean,
EventType:number,
IsGroup:boolean,
Notice:()->()
}
export type GameEvent = {
Name:string,
Enabled:boolean,
CheckFunc:BindableFunction,
ResponseFunc:{BindableFunction},
IsGroup:boolean,
Notice:()->(),
}
export type GameEventGroup = {
Name:string,
Enabled:boolean,
EventList:{GameBaseEvent},
AddEvent:(GameBaseEvent)->(boolean),
GetEvent:(string)->(GameEvent|GameBaseEvent),
EnableAllEvent:(string,boolean)->(),
RemoveEvent:(string)->(),
IsGroup:boolean,
Notice:()->()
}
export type GameEventManager = {
RootEventGroup:GameEventGroup,
CreateEventGroup:(string)->(),
RegisterEvent:(string,string,BindableFunction)->(),
SubscribeEvent:(string,BindableFunction)->(),
Notice:()->()
}
GameEvent
local GameBaseEvent = require(script.Parent.GameBaseEvent)
local GameEvent = {}
GameEvent.__index = GameEvent
function GameEvent.new(_Name:string)
local self = setmetatable({}::GameBaseEvent.GameEvent,GameEvent)
self.Name = _Name
self.Enabled = true
self.IsGroup = false
self.CheckFunc = nil
self.ResponseFunc = {}
return self
end
function GameEvent.Notice(self:GameBaseEvent.GameEvent)
if self.CheckFunc and self.CheckFunc:Invoke() then
for i = 1,#self.ResponseFunc do
self.ResponseFunc[i]:Invoke()
end
end
end
GameEventGroup
local GameBaseEvent = require(script.Parent.GameBaseEvent)
local GameEvent = {}
GameEvent.__index = GameEvent
function GameEvent.new(_Name:string)
local self = setmetatable({}::GameBaseEvent.GameEvent,GameEvent)
self.Name = _Name
self.Enabled = true
self.IsGroup = false
self.CheckFunc = nil
self.ResponseFunc = {}
return self
end
function GameEvent.Notice(self:GameBaseEvent.GameEvent)
if self.CheckFunc and self.CheckFunc:Invoke() then
for i = 1,#self.ResponseFunc do
self.ResponseFunc[i]:Invoke()
end
end
end
GameEventManager
local GameEventGroup = require(script.Parent.GameEventGroup)
local GameBaseEvent = require(script.Parent.GameBaseEvent)
local GameEvent = require(script.Parent.GameEvent)
local GameEventManager = {}
GameEventManager.__index = GameEventManager
function GameEventManager.new()
local self = setmetatable({}::GameBaseEvent.GameEventManager,GameEventManager)
self.RootEventGroup = GameEventGroup.new("Root")
return self
end
function GameEventManager.CreateEventGroup(self:GameBaseEvent.GameEventManager,_EventGroupName:string)
local event:GameBaseEvent.GameBaseEvent = self.RootEventGroup:GetEvent(_EventGroupName)
if not event then
event = GameEventGroup.new(_EventGroupName)
self.RootEventGroup:AddEvent(event)
end
end
function GameEventManager.RegisterEvent(self:GameBaseEvent.GameEventManager,_EventGroupName:string,_EventName:string,_CheckFunc:BindableFunction)
local eventGroup:GameBaseEvent.GameBaseEvent = self.RootEventGroup:GetEvent(_EventGroupName)
if eventGroup and eventGroup.IsGroup then
local event:GameBaseEvent.GameEvent = GameEvent.new(_EventName)
event.CheckFunc = _CheckFunc
eventGroup:AddEvent(event)
end
end
function GameEventManager.SubscribeEvent(self:GameBaseEvent.GameEventManager,_EventName:string,_ResponseFunc:BindableFunction)
local event:GameBaseEvent.GameEvent = self.RootEventGroup:GetEvent(_EventName)
if event and not event.IsGroup then
table.insert(event.ResponseFunc,_ResponseFunc)
end
end
function GameEventManager.Notice(self:GameBaseEvent.GameEventManager)
self.RootEventGroup:Notice()
end
end
Because it’s my first time writing, it may seem a bit problematic. I hope someone can tolerate it