# \[C++\] How do i set a function to be replicated over the network ?

**URL:** <https://forums.unrealengine.com/t/c-how-do-i-set-a-function-to-be-replicated-over-the-network/19772>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [January 28, 2015, 12:36am UTC](https://forums.unrealengine.com/t/c-how-do-i-set-a-function-to-be-replicated-over-the-network/19772 "2015-01-28T00:36:17Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gunschlinger](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Gunschlinger](https://forums.unrealengine.com/u/Gunschlinger)\
**Post date:** [January 28, 2015, 12:36am UTC](https://forums.unrealengine.com/t/c-how-do-i-set-a-function-to-be-replicated-over-the-network/19772/1 "2015-01-28T00:36:17Z")

</div>

How do i set a function to be replicated over the network ?

---

<div class="post-metadata">

**Author:** ![Fredrum](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Fredrum](https://forums.unrealengine.com/u/Fredrum)\
**Post date:** [January 28, 2015, 4:06am UTC](https://forums.unrealengine.com/t/c-how-do-i-set-a-function-to-be-replicated-over-the-network/19772/2 "2015-01-28T04:06:57Z")

</div>

I had trouble with this but then I’m a total noob.

Check out the Wiki for starters,

> **[A new, community-hosted Unreal Engine Wiki](https://forums.unrealengine.com/t/a-new-community-hosted-unreal-engine-wiki/141494)**
>
> After over a year in maintenance mode, the official Unreal Engine Wiki is now permanently offline. These resources now live on a new community-run Unreal Engine Community Wiki — https://unrealcommunity.wiki/! You will be able to find content from...

and doc’s about ‘RPCs’  
[https://docs.unrealengine.com/latest/INT/Gameplay/Networking/Replication/RPCs/index.html](https://docs.unrealengine.com/latest/INT/Gameplay/Networking/Replication/RPCs/index.html)

---

<div class="post-metadata">

**Author:** ![Gunschlinger](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Gunschlinger](https://forums.unrealengine.com/u/Gunschlinger)\
**Post date:** [January 28, 2015, 1:17pm UTC](https://forums.unrealengine.com/t/c-how-do-i-set-a-function-to-be-replicated-over-the-network/19772/3 "2015-01-28T13:17:42Z")

</div>

Thanks buddy !

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_55d41bed](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_55d41bed](https://forums.unrealengine.com/u/anonymous_user_55d41bed)\
**Post date:** [January 28, 2015, 2:26pm UTC](https://forums.unrealengine.com/t/c-how-do-i-set-a-function-to-be-replicated-over-the-network/19772/4 "2015-01-28T14:26:54Z")

</div>

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](https://docs.unrealengine.com/latest/INT/Resources/SampleGames/ShooterGame/index.html))

---

<div class="post-metadata">

**Author:** ![Gunschlinger](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Gunschlinger](https://forums.unrealengine.com/u/Gunschlinger)\
**Post date:** [January 28, 2015, 3:12pm UTC](https://forums.unrealengine.com/t/c-how-do-i-set-a-function-to-be-replicated-over-the-network/19772/5 "2015-01-28T15:12:21Z")

</div>

i will ! thanks for the great support guys

---

<div class="post-metadata">

**Author:** ![Janyx](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Janyx](https://forums.unrealengine.com/u/Janyx)\
**Post date:** [July 13, 2015, 12:45am UTC](https://forums.unrealengine.com/t/c-how-do-i-set-a-function-to-be-replicated-over-the-network/19772/6 "2015-07-13T00:45:04Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jjwisniewka](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@jjwisniewka](https://forums.unrealengine.com/u/jjwisniewka)\
**Post date:** [January 29, 2018, 10:56am UTC](https://forums.unrealengine.com/t/c-how-do-i-set-a-function-to-be-replicated-over-the-network/19772/7 "2018-01-29T10:56:42Z")

</div>

> [@anonymous\_user\_55d41bed](#):
>
> 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 ([https://docs.unrealengine.com/latest…ame/index.html](https://docs.unrealengine.com/latest/INT/Resources/SampleGames/ShooterGame/index.html))

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](https://wiki.unrealengine.com/Networking/Replication) -\> has something changed in new engine releases?

EDIT:

I have added

```auto

        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

---

<div class="post-metadata">

**Author:** ![jjwisniewka](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@jjwisniewka](https://forums.unrealengine.com/u/jjwisniewka)\
**Post date:** [January 29, 2018, 2:59pm UTC](https://forums.unrealengine.com/t/c-how-do-i-set-a-function-to-be-replicated-over-the-network/19772/8 "2018-01-29T14:59:21Z")

</div>

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?
