Is using script-fired events a viable script structure?

I am finding ways on how to structure my game, and it seems like an event-based system like this one seems to be working, where:

  • a central ModuleScript that holds events using Signals are used
  • different kinds of ModuleScripts use this script to fire or connect events when conditions are met.

It helps with each script being individually working and without circulation of scripts, but the problem is that it can’t be chronologically ran (thus having some scripts running first which they don’t want to).

I did solve that problem for now by creating events firing after the needed script first is done, and it could be chained together (this will be messy to do, though).
I am concerned that there will be flexibility and performance issues on using events in this way. Therefore, is this a viable script structure or should I change it?

These are the events list script and a sample script, so that it’s more clear on what I mean:

local Signal = require(game:GetService("ReplicatedStorage").Libraries.Signal)

type Signal<T...> = Signal.Signal<T...>

local events = {
    -- this one returns a Signal in order for the original Firer, for a "loading screen"
    -- to confirm that it was finished
    worldInitiating = Signal.new() :: Signal<Signal<string>>,
    worldInitiated = Signal.new() :: Signal<nil>,
    gameStarting = Signal.new() :: Signal<nil>,
    gameEnding = Signal.new() :: Signal<nil>,
    nextActiveFrame = Signal.new() :: Signal<number, number>,
    snakeMoved = Signal.new() :: Signal<nil>,
}

return events```

(shortened for easier readability)
```lua
events.worldInitiating:Connect(function(returnSignal)
    returnSignal:Fire(script.Name)
end)

events.gameStarting:Connect(function()
    -- assume that this sets up snakeController and snake
end)

events.nextActiveFrame:Connect(function(deltaTime, gameTime)
    local snakeController = sessionData.snakeController
    local snake = sessionData.snake
    snake:MoveByTime(gameTime)
    snakeController:FollowCameraToSnake(workspace.Camera)
    events.snakeMoved:Fire()
end)

I made a pretty good script framework that is popular in a lot of games. It’s a modular framework, this will also work with your idea as you can add a signal module to it, then you can easily communicate between them, with good performance included.

1 Like