How do i set a function to be replicated over the network ?
I had trouble with this but then I’m a total noob.
Check out the Wiki for starters,
and doc’s about ‘RPCs’
https://docs.unrealengine.com/latest/INT/Gameplay/Networking/Replication/RPCs/index.html
Thanks buddy !
The documentation Fredrum pointed out is a great place to start. Also, I wanted to post a few things that gave me issues when I first started with replication.
- Make sure all actors are spawned from the Authority (server), otherwise the actor in the clients will think they’re the authority, the server will then replicate them down to the clients
- Check the Role property of your actors to determine if the code is running on the Server or client
- A good pattern to use is to do the work on the client AND the server so you don’t have to wait for lag, so for example if you have a DoSomething method:
void AMyActor::DoSomething()
{
if (Role < ROLE_Authority) {
Server_DoSomething();
}
// Do something, on the client AND eventually on the server too
}
bool AMyActor::Server_DoSomething_Validate()
{
return true;
}
void AMyActor::Server_DoSomething_Implementation()
{
DoSomething();
}
- Look at the Shooter Game for some great examples on how to handle replication (Sample Game Projects for Unreal Engine | Unreal Engine 5.1 Documentation)
i will ! thanks for the great support guys
I am having issues with the first thing you pointed out. I spawned an actor without authority and now it thinks it’s the server.
However in my component I have no idea how to check Role, the component doesn’t seem to have the Role variable. How did you handle this?
I used exact same pattern to replicate character rotation and unfortunately it does not replicate properly (Server pawn is updated on Client but Client is not updated on server). I followed A new, community-hosted Unreal Engine Wiki - Announcements - Epic Developer Community Forums -> has something changed in new engine releases?
EDIT:
I have added
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, HasAuthority() ? TEXT("Attack Server!") : TEXT("Attack Client!"));
AddActorWorldOffset(GetActorForwardVector() * 100.0f);
to test why it won’t replicate properly and the texts on the screan are correct. Adding offset on server pawn works but on client pawn results in moving it but then moving it back to previous location. To be honest I don’t understand why
Ok I did manage to solve this but I wonder if there is more optimal way.
Right know I have multiple functions total ->
- Server replicated (with validation)
- Multicast replicated
then I check for authority and either run server or multicast function. I wonder if there is easier way to accopmlish it using UFunction headers?