ClientNamingSystem - jimdroberts/FishMMO GitHub Wiki
The ClientNamingSystem
class is responsible for managing the mapping between character and object IDs and their corresponding names in the FishMMO game client. It handles the registration and unregistration of naming broadcasts, as well as the storage and retrieval of name-ID mappings.
-
internal static Client Client
Reference to the client instance for network operations.
-
private static Dictionary<NamingSystemType, Dictionary<long, string>> idToName
Maps naming system type and ID to name. Used for fast lookup and caching.
-
private static Dictionary<string, long> nameToID
Maps character names to their unique IDs. Assumes character names are unique.
-
private static Dictionary<NamingSystemType, Dictionary<long, Action>> pendingNameRequests
Tracks pending name requests by type and ID, with callbacks to invoke when names are received.
-
private static Dictionary<NamingSystemType, Dictionary<string, Action>> pendingIdRequests
Tracks pending ID requests by type and name, with callbacks to invoke when IDs are received.
-
public static void Initialize(Client client)
Initializes the naming system, registers broadcast handlers, and loads cached names from disk (outside Unity Editor). Parameters: - Client client: The client instance to use for network operations.
-
public static void Destroy()
Cleans up the naming system, unregisters broadcast handlers, and saves cached names to disk (outside Unity Editor).
-
public static void SetName(NamingSystemType type, long id, Action action)
Checks if the name matching the ID and type is known. If not, requests it from the server and invokes the callback when available. Values learned this way are saved to disk when the game closes and loaded when the game loads. Parameters: - NamingSystemType type: The naming system type. - long id: The unique ID to resolve to a name. - Action action: Callback to invoke with the resolved name.
-
public static void GetCharacterID(string name, Action action)
Gets the character ID for a given name. If not cached, requests it from the server and invokes the callback when available. Parameters: - string name: The character name to resolve to an ID. - Action action: Callback to invoke with the resolved ID.
- Call
ClientNamingSystem.Initialize(client)
after the client is ready and network systems are initialized. - Use
SetName
to resolve IDs to names, andGetCharacterID
to resolve names to IDs. - Call
ClientNamingSystem.Destroy()
on shutdown to save cached names and unregister handlers.
// Example 1: Resolve a character name from an ID
ClientNamingSystem.SetName(NamingSystemType.CharacterName, characterId, (name) => {
Debug.Log($"Character name: {name}");
});
// Example 2: Resolve a character ID from a name
ClientNamingSystem.GetCharacterID("PlayerName", (id) => {
Debug.Log($"Character ID: {id}");
});
- Always initialize the naming system after the client and network manager are ready.
- Use callbacks to handle asynchronous name/ID resolution.
- Call
Destroy
to persist name mappings and unregister handlers when the client shuts down. - Avoid duplicate requests for the same name or ID; the system batches callbacks for efficiency.