Reading time: ~35 minutes
Table of Contents
- NetworkObject
- NetworkBehavior
- Networked Variables
- Remote Procedure Calls (RPCs)
- Network Ownership
- Spawning and Despawning
- Best Practices
NetworkObject
NetworkObject is a special component that makes a GameObject replicate across the network. Any GameObject that needs to exist on both server and clients must have a NetworkObject component.
Adding a NetworkObject
Note: When usingaddComponent(), the NetworkObject is automatically spawned and replicated. You only need to manually callspawn()when using the low-leveladdComponentNoInit()method, which is primarily for internal use.
NetworkObject Features
- Automatic Replication: The GameObject and its transform are replicated to all clients
- Ownership: Can be owned by a specific player or the server
- Hierarchy Support: Child NetworkObjects are automatically managed
- Network Events: Subscribe to ownership and state changes
NetworkObject Events
Getting Network Information
NetworkBehavior
NetworkBehavior is a specialized component that enables network replication of component state. It extends Behavior and requires a NetworkObject on the same GameObject.
Creating a NetworkBehavior
NetworkBehavior Features
- Automatic Spawning: Replicated to clients when the NetworkObject spawns
- Network Variables: Synchronize state using NetworkedVariables, including primitives, tables, and ScriptableObjects
- RPCs: Call methods across the network
- References: Can be referenced in NetworkedVariables
- sendBehavior: Manually trigger replication of the behavior to specific clients
Getting the NetworkObject
Networked Variables
NetworkedVariable allows you to synchronize data between server and clients automatically. When the value changes on the server, it’s automatically replicated to all clients.
Basic Usage
Supported Types
NetworkedVariables support the following types:Replication Behavior
Networked Collections
CORP also provides networked collections for more complex data structures:NetworkedList
NetworkedMap
Note: NetworkedMap only supports primitive keys (string, number, boolean). Complex types like Vector3 or tables cannot be used as keys.
NetworkedSet
Remote Procedure Calls (RPCs)
RPCs allow you to call methods across the network boundary. CORP provides a powerful decorator-based RPC system with extensive configuration options.Basic RPC
RPC Configuration
Endpoints
Define where the RPC can be called from and to:Access Policy
Control who can invoke the RPC:Return Mode
Specify if the RPC returns a value:Reliability
Control transmission reliability:Complete RPC Example
Network Ownership
NetworkObjects can be owned by specific players or the server. Ownership affects:- Who can modify NetworkedVariables
- RPC access policies
- Network authority
Changing Ownership (Server Only)
Checking Ownership
Spawning and Despawning
Spawning NetworkObjects
NetworkObjects are automatically spawned when you useaddComponent():
Note: You only need to manually callspawn()when usingaddComponentNoInit(), which is a low-level method primarily for internal use.
Despawning NetworkObjects
Simply destroy the GameObject:Dynamic Spawning Example
Best Practices
1. Always Use NetworkObject with NetworkBehavior
2. Server Authority
Let the server make authoritative decisions:3. Use Batched Replication When Possible
4. Validate RPC Inputs
5. Clean Up Connections
Next Steps
- Scene Management: Learn about organizing networked scenes
- Advanced Topics: Explore spawn management and observables
- Examples: See complete networked game implementations
Master networking to create seamless multiplayer experiences with CORP! 🌐