Replication Function not triggering (cross class)

Edited the title to help anyone thinking about it in future:


What could cause Server functions not to trigger? I have replication in dozen other classes and it all works fine but here the server ones just don’t trigger. Am a bit tired already today but am I missing something obvious?

  1. Client that is also listen server succesfully destroys the items, and it replicates properly.
  2. On a Client that is not listen server the functions never trigger.
  3. On Clients running on Dedicated server the functions never trigger.

KortiganItem.h

UFUNCTION() void OnPickedUp();
UFUNCTION(reliable, server, WithValidation) void ServerOnPickedUp();

KortiganItem.cpp

Constructor has:

bReplicates = true;
bReplicateMovement = true;
PrimaryActorTick.bCanEverTick = true;

Above Functions:

void  AKortiganItem::OnPickedUp()
{
	K2_DestroyActor();

	if (Role < ROLE_Authority)
	{
		ServerOnPickedUp(); //Breakpoint gets to here
	}
}

bool AKortiganItem::ServerOnPickedUp_Validate()
{
	return true; //never happens
}

void AKortiganItem::ServerOnPickedUp_Implementation()
{
	OnPickedUp(); //never happens
}

#Simulated Proxy

Does adding this to your constructor help at all?

SetRemoteRoleForBackwardsCompat(ROLE_SimulatedProxy);

Didn’t help sadly. It seems to be trace related issue that I couldn’t trace (pun not intended) down yet. It works fine if I call the same function on overlap in the same class.

Trace Hit (in another class) returns correct item instance, also the debug line drawn by DrawDebugLine is drawn correctly, but calling the function cross-class somehow makes it botch the job with actually executing the function serverside.

“but calling the function cross-class somehow makes it botch the job with actually executing the function serverside.”

Ah there it is then!

If you are going to call a server function cross class you have to first go server side on the class you are currently in!

Here’s the work flow I have experienced to be necessary time after time

again the scenario is calling a server function in Class B from Class A

here is what I do

Class A local (non server) calls
Class A server function to then call
Server function in Class B

I can’t say this is required in every case, in fact I am sure it isnt, but if ever cross class server function is not working, try my above flow

I’ve had to use it many times!

:slight_smile:

Rama

This worked. Thank you!

How would I accept comment as answer?

Hi Rama, Im trying to figurate something out, you said “If you are going to call a server function cross class you have to first go server side on the class you are currently in!” and “Class A local (non server) calls Class A server function to then call Server function in Class B”. This is fine is you want to call just some server side function and thats all.

But, what happen if you dont want to execute server function of other class directly and first you want to execute a client function that returns some parameter to finaly pass it to the server function?. I mean, Class A local (non server) calls Class A server function, then call client function of Class B that returns some parameter to pass it finally to a server side function also in class B. Im not sure if im explaining well. This question is also extended to all calls that can be done after the final server function.

Thanksss for all!

The answer was posted above by Rama:

If you are going to call a server
function cross class you have to first
go server side on the class you are
currently in!

Here’s the work flow I have
experienced to be necessary time after
time

again the scenario is calling a server
function in Class B from Class A

here is what I do

Class A local (non server) calls Class
A server function to then call Server
function in Class B

I can’t say this is required in every
case, in fact I am sure it isnt, but
if ever cross class server function is
not working, try my above flow

I’ve had to use it many times!

:slight_smile:

Rama

Just marking it as answer for visibility, since it doesn’t seem possible to mark comment as answer. (If there is a way please let me know and I’ll fix it.)