CharacterSystem - jimdroberts/FishMMO GitHub Wiki
The CharacterSystem
is a core server-side system in FishMMO responsible for managing all aspects of player character lifecycle, including authentication, loading, spawning, despawning, saving, teleportation, and event handling. It maintains mappings between connections, character IDs, names, and world servers, and coordinates the flow of character data between the server, database, and client. The system also handles periodic character saves, out-of-bounds checks, and broadcasts all relevant character data (abilities, inventory, guild, party, friends, etc.) to the client.
-
private SceneServerAuthenticator loginAuthenticator
Authenticator for login and character loading.
-
private LocalConnectionState serverState
Current connection state of the server.
-
public float SaveRate
Interval in seconds between periodic character saves.
-
private float nextSave
Time remaining until the next character save.
-
public float OutOfBoundsCheckRate
Interval in seconds between out-of-bounds checks for characters.
-
private float nextOutOfBoundsCheck
Time remaining until the next out-of-bounds check.
-
public event Action<NetworkConnection, long> OnBeforeLoadCharacter
Triggered before a character is loaded from the database.
-
public event Action<NetworkConnection, IPlayerCharacter> OnAfterLoadCharacter
Triggered after a character is loaded from the database.
-
public event Action<NetworkConnection, IPlayerCharacter> OnConnect
Triggered immediately after a character is added to their respective cache.
-
public event Action<NetworkConnection, IPlayerCharacter> OnDisconnect
Triggered immediately after a character is removed from their respective cache.
-
public event Action<NetworkConnection, IPlayerCharacter, Scene> OnSpawnCharacter
Triggered immediately after a character is spawned in the scene.
-
public event Action<NetworkConnection, IPlayerCharacter> OnDespawnCharacter
Triggered immediately after a character is despawned from the scene.
-
public event Action<NetworkConnection, IPlayerCharacter> OnPetKilled
Triggered immediately after a pet is killed.
-
public Dictionary<long, IPlayerCharacter> CharactersByID
Maps character IDs to player character instances.
-
public Dictionary<string, IPlayerCharacter> CharactersByLowerCaseName
Maps lowercase character names to player character instances.
-
public Dictionary<long, Dictionary<long, IPlayerCharacter>> CharactersByWorld
Maps world server IDs to dictionaries of character IDs and player character instances.
-
public Dictionary<NetworkConnection, IPlayerCharacter> ConnectionCharacters
Maps network connections to player character instances.
-
public Dictionary<NetworkConnection, IPlayerCharacter> WaitingSceneLoadCharacters
Maps network connections to player characters waiting for scene load.
-
public override void InitializeOnce()
Initializes the character system, registers event handlers, and sets up character authentication and broadcast handling.
-
public override void Destroying()
Cleans up the character system, unregisters event handlers, and saves all characters to the database before shutdown.
-
void LateUpdate()
Unity LateUpdate callback. Periodically checks for out-of-bounds characters and saves character data.
-
public void SendAllCharacterData(IPlayerCharacter character)
Sends all server-side character data (abilities, achievements, guild, party, friends, inventory, bank, hotkeys) to the owner. character (IPlayerCharacter): Player character to send data for.
-
public bool SendBroadcastToCharacter(string characterName, T msg) where T : struct, IBroadcast
Allows sending a broadcast to a specific character by their character name. characterName (string): Name of the character to send to. msg (T): Broadcast message to send. Returns: bool - True if the broadcast was sent successfully, false otherwise.
-
public bool SendBroadcastToCharacter(long characterID, T msg) where T : struct, IBroadcast
Allows sending a broadcast to a specific character by their character ID. characterID (long): ID of the character to send to. msg (T): Broadcast message to send. Returns: bool - True if the broadcast was sent successfully, false otherwise.
-
public void IPlayerCharacter_OnTeleport(IPlayerCharacter character)
Handles character teleport events, validates teleporter and scene, updates position, and saves state. character (IPlayerCharacter): Player character to teleport.
- Requires the server to be running and the character system to be initialized as part of the server startup.
- SceneServerAuthenticator must be present in the scene for authentication and character loading.
- All event handlers and broadcast registrations are set up automatically by the system.
- No manual registration is required unless extending or customizing character logic.
// Example 1: Sending a broadcast to a character by name
characterSystem.SendBroadcastToCharacter("playername", new CustomBroadcast { ... });
// Example 2: Handling character teleportation (server-side)
characterSystem.IPlayerCharacter_OnTeleport(character);
- Always validate all character and scene data before loading, spawning, or teleporting characters.
- Use the provided event hooks to extend or customize character logic as needed.
- Periodically save all character data to prevent data loss on server shutdown or crash.
- Avoid direct manipulation of character mappings outside of the provided system methods.
- Ensure all scene and teleporter references are valid before performing scene transitions or teleports.