ServerBehaviour - jimdroberts/FishMMO GitHub Wiki
Base class for all server-side behaviours in the FishMMO server architecture. Provides registration, initialization, and lifecycle management for server behaviours, enabling global access and coordinated startup/shutdown of server systems.
-
private static Dictionary<Type, ServerBehaviour> behaviours
Static dictionary mapping behaviour types to their instances for global access and management.
-
public bool Initialized { get; private set; }
Indicates whether this behaviour has been initialized.
-
public Server Server { get; private set; }
Reference to the server instance associated with this behaviour.
-
public ServerManager ServerManager { get; private set; }
Reference to the server manager instance associated with this behaviour.
-
internal static void Register(T behaviour) where T : ServerBehaviour
Registers a server behaviour instance for global access. behaviour: T - The behaviour instance to register.
-
internal static void Unregister(T behaviour) where T : ServerBehaviour
Unregisters a server behaviour instance from global access. behaviour: T - The behaviour instance to unregister.
-
public static bool TryGet(out T control) where T : ServerBehaviour
Attempts to get a registered server behaviour of type T. control: out T - The output behaviour instance if found. Returns: bool - True if found, false otherwise.
-
public static T Get() where T : ServerBehaviour
Gets a registered server behaviour of type T, or null if not found. Returns: T - The behaviour instance if found, otherwise null.
-
public static void InitializeOnceInternal(Server server, ServerManager serverManager)
Initializes all registered server behaviours with the provided server and server manager. server: Server - The server instance. serverManager: ServerManager - The server manager instance.
-
internal void InternalInitializeOnce(Server server, ServerManager serverManager)
Internal initialization logic for this behaviour. Sets server and manager references and calls InitializeOnce. server: Server - The server instance. serverManager: ServerManager - The server manager instance.
-
public abstract void InitializeOnce()
Called once to initialize the behaviour. Must be implemented by derived classes.
-
public abstract void Destroying()
Called when the behaviour is being destroyed. Must be implemented by derived classes.
-
private void Awake()
Unity Awake callback. Registers this behaviour instance.
-
private void OnDestroy()
Unity OnDestroy callback. Calls Destroying and unregisters this behaviour instance.
-
private void OnApplicationQuit()
Unity OnApplicationQuit callback. Calls Destroying and unregisters this behaviour instance.
- Inherit from
ServerBehaviour
for any server-side system that requires coordinated initialization and shutdown. - Implement
InitializeOnce
andDestroying
in derived classes to handle setup and cleanup logic. - Attach derived behaviours to server GameObjects as needed.
- The system will automatically register, initialize, and manage the lifecycle of all server behaviours.
// Example 1: Creating a custom server behaviour
public class MyServerSystem : ServerBehaviour
{
public override void InitializeOnce()
{
// Custom initialization logic
}
public override void Destroying()
{
// Custom cleanup logic
}
}
// Example 2: Accessing a registered server behaviour
if (ServerBehaviour.TryGet<MyServerSystem>(out var mySystem))
{
// Use mySystem as needed
}
- Always inherit from
ServerBehaviour
for server-side systems that require global access or coordinated lifecycle management. - Use
TryGet
andGet
to safely access registered behaviours from anywhere in the server codebase. - Implement proper cleanup in
Destroying
to avoid resource leaks and dangling references. - Avoid direct manipulation of the
behaviours
dictionary; use provided registration methods. - Ensure all server behaviours are attached to active GameObjects in the server scene.