Game.Simulation.IXPMessageHandler
Assembly: Assembly-CSharp
Namespace: Game.Simulation
Type: interface
Base: N/A
Summary:
Interface for a message handler that accepts XPMessage instances from the simulation. Implementers receive XPMessage objects (representing experience/skill/XP-related events or updates) and are responsible for enqueuing, processing, forwarding, or otherwise handling those messages in a manner appropriate for the simulation or mod. Implementations should be mindful of thread/context the message is delivered on (simulation thread vs main thread) and avoid long-running work directly inside AddMessage — prefer queuing for later processing in the appropriate context.
Fields
None
No fields are declared by the interface. Implementations will typically have their own internal queues or state to track incoming messages.
Properties
None
No properties are required by this interface. Implementers may expose properties as needed for their own logic.
Constructors
None
Interfaces do not define constructors. Concrete implementations should provide their own constructors as needed.
Methods
void AddMessage(XPMessage message)
Adds an XPMessage to the handler for processing. Typical responsibilities include validating the message, enqueueing it for later processing, or immediately forwarding it to the appropriate processing system. Avoid heavy synchronous work here; instead, push the message into a thread-safe queue or schedule processing on the simulation/main update loop.
Notes: - XPMessage type is not defined here; check the game's codebase for its structure (likely contains IDs, XP amounts, source/target info). - Consider thread-safety: if AddMessage can be called from worker threads, use lock-free/thread-safe queues or appropriate synchronization. - If your mod needs to act on XPMessage content in the Unity main thread (for example, to update UI), ensure you marshal processing back to the main thread.
Usage Example
using System.Collections.Concurrent;
using Game.Simulation;
public struct XPMessage
{
public int EntityId;
public int Amount;
// other fields...
}
public class MyXPMessageHandler : IXPMessageHandler
{
// Thread-safe queue to collect incoming messages
private readonly ConcurrentQueue<XPMessage> _queue = new ConcurrentQueue<XPMessage>();
// Called by the simulation to add a message (may be from worker threads)
public void AddMessage(XPMessage message)
{
// Quick enqueue, avoid heavy processing here
_queue.Enqueue(message);
}
// Called from the main/mod update loop to process queued messages
public void ProcessQueuedMessages()
{
while (_queue.TryDequeue(out var msg))
{
// Handle message (apply XP, update UI, etc.)
// Ensure UI updates are performed on the main Unity thread
}
}
}
Additional tips: - If the game exposes registration APIs for message handlers, register your implementation at the appropriate initialization point. - If you need to persist or serialize XP-related changes, do so in your processing step rather than inside AddMessage. - Test with a high message rate to ensure your queueing/processing approach handles load without causing dropped messages or contention.