๐งฐ GetModule
GetModule is Shard's centralized module registry. It allows you to access any initialized service, controller, or utility module from anywhere in your game after it's been registered by the framework bootstrapper.
It supports:
- Retrieving Shard-managed modules (e.g. controllers/services)
- Fallback resolution from
ReplicatedStorage
orServerStorage
if not found - Optional debug logging for fallback visibility
๐ฆ When Should You Use GetModule?โ
- To access a service from another service (
RoundService โ DataService
) - To access a controller from another controller (
UIController โ PromptController
) - To share utility modules between environments (e.g. a type module or config table)
- When doing manual dependency resolution across folders
๐งช Basic Usageโ
local GetModule = require(ReplicatedStorage.Packages.GetModule)
local SpawnService = GetModule("SpawnService")
local RoundService = GetModule("RoundService")
SpawnService:SpawnPlayerAtRandomSpawn(player)
task.wait(5)
RoundService:StartNextRound()
๐งฌ How It Worksโ
During startup, every module that is loaded through the bootstrapper is added to the registry:
GetModule:Add(required, moduleName)
Once registered, you can retrieve it with:
local MyService = GetModule("MyService")
If the module isn't found in the registry, GetModule will search:
ReplicatedStorage
ServerStorage
(on the server)StarterPlayerScripts
(on the client)
If a fallback is found and required successfully, it will also be cached for future use.
๐ง Tips & Best Practicesโ
- Always call
GetModule()
insidestart()
methods, never insideinitialize()
, to ensure all dependencies are already registered. - Avoid circular dependencies. If
ServiceA
usesServiceB
and vice versa, extract shared logic into a third module. - Use
GetModule.Debug = true
to enable warnings when fallback modules are required.
๐ Example: Optional Fallbackโ
-- SharedTypes may not exist, but if it does, this will find it
local SharedTypes = GetModule("SharedTypes")
if SharedTypes then
print(SharedTypes.ItemType)
end
๐ฆ APIโ
Method | Description |
---|---|
GetModule(name) | Retrieve a module by name. If not found, it will search ReplicatedStorage and ServerStorage. |
GetModule:Add(module, name) | Register a module in the GetModule registry. This is done automatically during the bootstrap process. |
GetModule:Remove(name) | Remove a module from the registry. This is not commonly used. |
GetModule.Debug | Enable debug logging for fallback resolution. Useful for development. By default, this is tied directly to ReplicatedStorage.Configs.Framework , GET_MODULE_DEBUG |
GetModule:Resolve(name) | Resolve a module by name, returning the module or nil if not found. If found it is cached. This is an internal method. |