1. Network Architecture
2.Replication
Information is synchronized from the server to the customer service (one-way), only Actor and its derived classes have the ability of Replication
type
- Actor Replication(类)
- Property Replication
- Compoenent Replication (component)
Two meanings of Actor Replication
1. The server is generated, and the client is also generated (a replication object is generated on the server)
2. All attributes of the current Actor are copied, components are copied, and the master switch of RPC
Enable Actor Replication
Blueprint: Check "Replication"
C++:bReplication = true;
In the constructor in the cpp file
Example demo
Switch Has Authority (Authority: equivalent to the server, Remote: equivalent to the client)
Test the setup while connected to the Internet
unchecked
After checking
Property Replication (setting method for attribute replication)
Blueprint: Replication of the property is set to Replicated
Set on the server side, if the server-side Replication is not set to Replicated, the client cannot see the attribute
Before not set
after setting
C++:
- Add UPROPERTY(Replication) before the attribute
- In the .cpp file, add in the GetLifetimeReplicatedProps function: DOREPLIFETIME (class name, variable name) does not need to declare this function
Add attribute
VisibleAnywhere: also visible in the blueprint
add header file
RepNotift (replication notification)
If a variable is set to Rep_Notify, both the server and the client receiving the value can call a custom function when the variable is replicated
C++ only calls functions on the client side
Blueprint: Replication of the attribute is set to RepNotify, which is automatically generated
When the attribute Replication is set to RepNotify, an ONRep function will be automatically generated
The set variable will change after setting
Both the server and the client in the blueprint can call
c++:UPROPERTY(ReolicatedUsing=xxx)
make variable copyable
3.Ownership
As many players join as there are several Connections
The role of ownership
- RPC needs to determine which client will execute the RPC running on the client
- Actor replication and connection dependencies
- Actor property copy condition when owner is involved
Set/Change/Get Ownership
- SpawnActor (there is Owner in SpawnParanmeters)
- SetOwner
- Possess(OnPossess->PosssesssedBy->SetOwner),UnPossess
4.Actor Role
Authority: have control
Simulated Proxy: simulated proxy
Autonomous Proxy: Autonomous Proxy
Server: server, Client: client. big smiley face
5.RPC
Similar to a function call, but not necessarily executed locally
can be realised:
- The client calls the server to execute
- The server calls the client to execute
Can not have a return value, the default is "unreliable" (can be set to reliable: Reliable)
RPC settings
blueprint:
The Replicates option of the CustomEvent custom event is set to
- Run On Server (server execution)
- Run On Owning Client (client execution)
- Net MultiCast (all endpoints do)
C++
To declare a function as RPC, simply put
- Server
- Client
- NetMulticast
keyword added to the UFUNCTION declaration
example
First check Replication to make it replicable
concrete blueprint
detailed blueprint
Authorrity: on behalf of the server, Remote: on behalf of the client
Is Locally Controlled: Whether it is a local controller, and whether it is a big smiley face
Get Local Role: get the local role, and the smiling face (judging whether it is a big smiling face)
The client call event is generated on the server side,
Server-side implementation only (server generates one, client copies one)
Client-only implementation (two clients spawned by the server, replicating itself)
all achieved
Test Multicast
Only press the right mouse button on the server to print, and both the server and the client will print
Test Server
Only server side printing
Test Client
Create a GameMode first (because GameMode only exists on the server side)
Blueprints in GameMode
Event onPostLogin: Called when a player connects
Get Remote Role: Get the remote role
Get Local Role: Get the local role
Has Authority: Whether it is Authority (equivalent to whether it is a server)
final blueprint
GameMode
Call Test Client
Call Test Multicast
Test Server