RepNotify Function only executes on Clients not on the Server

Hello Community,

First of all, the upcomming pictures are very big. Open them in a new Window, to scroll horizontally.

this is not my first RepNotify, but the first iam struggling with. What it is about:

I created an AmmoPickUp Actor. When the Player moves over it (enter its collision sphere), the PickUp content should be added to the Players AmmoBackpack (represent by the RepNotify Variable).
The RepNotify Function should update the Players HUD, to see the content of his AmmoBackpack.

As the client, all is working correctly. But as the Server, the RepNotify Function never gets executed. As i am aware that the Function only gets called, if the RepNotify Variable does actually changes, i added a print as a proof for debugging.

The only difference to my other RepNotifies is, that this one is an array.

Any suggestions?

The Video shows, that the Array is changeing (print in green) and that the RepNotify is called only on the Client (print in white)

1 Like

I did some testing with normal variables and they all get the RepNotifyFunction to execute, but only if i am using the “set var” node. The “incrementInt” node leads to the same problem. So i am supposing, that is has something to do with the “set array element” node.

Edit: After some more searching down this forum, i came up with those two postings:
Set Array Elem problem
FEATURE REQUEST - Set Array Elem Repnotify

The first one is open end and has no result, but the second one mentioned to add another var (a boolean), that is altered with any array change and THIS is RepNotifing the function for the array. The array itself will get back to “replicate” only.

I will test it and post a result here.

3 Likes

Hmmm… after some testing i came up to an “yes and no”:

YES: A RepNotify Boolean solved the problem above and the server now works as intended, but…
NO: The Client stops working. The problem is here, that the boolean gets replicated bevor the array. So that my widget got old information (one step behind) to work with.
To solve: I leave both (the array and the boolean) on RepNotify and uses the “switch authority” to save up some Hardware ressources.

Iam not that satisfied with the result, but it works at least. As long as i have much much more work to do, i leave it here with a marking in my code and will go on with other problems :smiley:

What’s the root cause of this?

I don’t know if the same applies for Blueprint, but in C++ RepNotify/ReplicatedUsing-functions only get called on clients on replication. The server, after setting the replicated values, has to call the OnRep-function manually… because the server is responsible for setting the replicated values and does not recieve them as replication, the server has to call the RepNotifies manually.

Try calling the RepNotify-function manually on the server after setting the values on the server. That way it gets called manually on the server after setting the replicated values, and the clients will automatically call it when the values get replicated.

On server:

  1. Set your array to be replicated and have it have some RepNotify function A
  2. Set values in your array
  3. Immediately after setting the values in the array, call A manually on the server

On client:
A should be called on replication.

6 Likes

Here in 2024 this post helped me, thanks!

It feels a little bit hacky to be manually calling the Rep Notify in order for my listen-server player to receive it (pretty much the same use case as OP, trying to broadcast ammo/inventory change events to update player HUD) but it seems like these Unreal networking systems are designed with dedicated server play in mind. I’m trying to support LAN play so I want my server and client players to behave the same.
This solution requires me to call the OnRep function everywhere that my replicated struct changes, which is a bit of pain, but it works!

If I understand correctly RepNotify function defined in C++ will not get called on the Server (Dedicater or Listen) and will only be called on clients.
This is the opposite of BP functionality, in the case of blueprints it gets called on the Server too (judging from my testing).

The only resource I could find on RepNotify is the following:
https://docs.unrealengine.com/4.27/en-US/Resources/ContentExamples/Networking/1_4/

This states the BP functionality, but it is pretty confusing trying to convert BPs to C++. Would be nice if Epic clarified it in the docs or unified it (but that’s unlikely as that could break already existing games).