How do you structure your code for better scalability and expansion?

Code scalability and structuring is a topic I’ve been struggling on for years. I’m really confused as I’ve used both multi-script and single-script architecture. I’ve stuck to SSA but there isn’t enough resources that talk in depth on how to set things up.

Context:

In Leif’s video he talks about how you should haves services that control the games logic. Each one of these services also has their own methods that do stuff.

Questions:

  1. If I have one centralized script that starts up these services, either on server or client, who will use the methods that are in the services?

    a) If the methods are used in the start up script, wouldn’t that ruin the whole point of having all the logic in services?

    b) If other services on the same level hierarchy require each other and use the methods, wouldn’t that lead to messy code and a lot of dependencies?

  1. If the init() should never contain any yielding code then how should I handle :WaitForChild() instances? Lets say I have a lobby service on the client that controls all the animations like NPCs or environmental models. Don’t I need to wait for them to load before I can start their animations? But that would cause init() to yield?

    a) If I instead put the yielding code in another start function that is put in a task.spawn() when loaded wouldn’t that break the whole point of having a module loader? Like most of the client services have :WaitForChild() instances so like half of the services would only have a start function and no init()

I do have more questions regarding to other creators code set ups but I can’t think of any more right now

In general with this architecture there are systems that are bootstrapped by a loader (which picks load order) and the systems are mostly self contained. Logic that these systems want to share goes into shared services and libraries which are also started up on require and can be used. Those should go in a specific spot though if they are intended to be used by multiple things. If they are meant to be used by a specific subsystem only, best to put it under that subsystem module head.

No, the start up script is just for requiring the modules so they can set themselves up. They can’t run without being required at least once, so you have to do so at some point. You can lazy load it though if you don’t need the loader to start it up but a subservice might.

If they are on the same level they should not be requiring each other. Generally if you are requiring anything like this it implies you have some logical circle in your code that can be removed by reorganization.

Not letting init yield is mostly about preventing one thing you load from yielding everything you load. Any logic that would yield should be task.spawned() or otherwise work.

The services themselves are allowed to yield. They are often self contained. You just don’t want their setup to block the setup of the other items. They fundamentally may need to wait on some things to load, and that’s ok. Each can startup on their own schedule. The module loader is purely so you can make sure they load in the correct order in case you have dependencies on some specific behaviors. You also could yield if you are ok with one script blocking the others from starting up until it’s done. This may be necessary sometimes.

In general with this architecture there are systems that are bootstrapped by a loader (which picks load order) and the systems are mostly self contained. Logic that these systems want to share goes into shared services and libraries which are also started up on require and can be used. Those should go in a specific spot though if they are intended to be used by multiple things. If they are meant to be used by a specific subsystem only, best to put it under that subsystem module head.

So in this video he has this code for one of his services: 2:45

function BuildServiceClient:init()
--Do stuff here
end

function BuildServiceClient:build()
--Do stuff here
end

I’m confused on who will use build service client’s build method. I know that the init will be ran by the module loader but where will the build method be used? Is it going to be used within the service or will some other script use it?

The way I’ve organized my code, I didn’t really follow a specific, set in stone, method to do so. But I have developed the habit of separating things into modules, mainly to keep the code readable.

When code spans more than a thousand lines, it starts getting annoying to navigate through it, and that is when I start considering to split it into different scripts. Above 2 thousand lines, I very very very dislike

But that is for the organization of modules themselves. For organizing the code of all the modules, I build these modules as independent structures (that still end up using 5 other modules as dependencies lol). Then, core parts of the game use these and, do stuff

For example, there is a RoundScript script that communicates with a GameState module, which keeps track of the state of the game. There are also localized gamemode modules (stored under RoundScript, which contain information and code for the different gamemodes in the game, and is used exclusively by RoundScript

The modules themselves vary wildly in what they do, some are very generalized like a SignalModule, which can be used in any game. Some like the DatastoreModule is built for the game, but can very easily be adapted to another game, and GameState is purpose built for that game. But most modules are generalized enough that they aren’t strictly built for that game in particular

I don’t restrict myself to a single script architecture, but things do tend to a very small amount of scripts, and a lot of module scripts

The most annoying part of this structure is, when you have multiple games that share modules, keeping modules updated between games is a bit annoying, and dealing with missing dependencies. I tried making myself a simple package manager type thing, but it didn’t end up being very good. I also dislike how the software world is an overcomplicated mess, and I don’t want an overcomplicated package manager

Little pictures

(Using RunContext to make scripts run from inside ReplicatedStorage. This allows a system split between the client and server to still be fully contained into a single folder)

It’s not perfect, but it definitely does the work quite well