Network Modules
NetworkModule is a base class in the PurrNet networking solution that allows you to create modular, network-aware components that are not MonoBehaviours. These modules can be attached to any NetworkIdentity, enabling code reuse and clean separation of concerns in your networked game.
Key Features
Modularity: Encapsulate specific functionality into reusable modules.
Network Integration: Access networking properties like isServer, isClient, and send RPCs.
Lifecycle Hooks: Override OnSpawn and OnDespawned for initialization and cleanup and many others.
Example: HealthModule with State Synchronization
Here's a concise example of a HealthModule that synchronizes a health value across the network, ensuring proper state synchronization even if clients reconnect.
Explanation
Buffered RPC: The RpcUpdateHealth method uses bufferLast: true to buffer the last health value so new or reconnecting clients receive the correct state.
Server Authority: Only the server can modify the Health property to maintain authoritative game state.
Event Handling: Clients can subscribe to OnHealthChanged to react to health updates.
Attaching HealthModule to a NetworkIdentity
Last updated