Download

Is it possible to turn parent class's Replicated variable into OnRepNotify from child class?

I’m working on a party and matchmaking plugin. The issue I have right now is that when a lobby client’s OnLoginComplete() is called, these values are not yet replicated. That means I can’t really handle anything in the login function. I could use a timer, and delay handling stuff by a second or so, but that’s a hacky solution, and I’d like to avoid that.



// ALobbyBeaconClient.h - made by EPIC, the parent class of my custom lobby beacon client class

/** Client view of the lobby state */
UPROPERTY(Replicated)
ALobbyBeaconState* LobbyState;

/** Player state associated with this beacon (@todo not splitscreen safe) */
UPROPERTY(Replicated)
ALobbyBeaconPlayerState* PlayerState;


So, is there a way of overriding the behaviour of these replicated UPROPERTY variables behaviour without changing the source code? Instead of simply replicating, they would use the ReplicatedUsing=OnRep_SomeFunction macro.

Thanks in advance!

This should be possible through the reflection system. It’s just going to take some digging through the engine source code and might be more effort than it’s worth. The code below is how you might go about it.


UClass* reflectionClass = ALobbyBeaconClient::StaticClass();
FProperty* currentProperty  = reflectionClass->PropertyLink;

while(currentProperty){

    if(currentProperty->GetNameCPP().Equals(FString("LobbyState"))){
        currentProperty->SetPropetryFlags(EPropertyFlags::CPF_RepNotify);
        currentProperty->RepNotifyFunc = FName("OnRep_SomeFunction");
        //Any other logic that alters the underlyng property and its replication to use the function
    }

    currentProperty = currentProperty->PropertyLinkNext;
}

Please note that I have not tested the code above. Setting the notify may require applying the repnotify flag on the variables corresponding property. A major potential problem with doing this might be on how it determines the function to use. I’m assuming it uses the RepNotifyFunc variable to find the function to use on replication. If the function must exist within the ALobbyBeaconClient class, then this won’t work at all since you cannot add the function to the base class. If the function is checked to exist on the derived class then this should work. There’s probably other nuances with the reflection system and replication that I’m missing, but you’re going to need to figure those out. Hopefully someone else with more knowledge on the reflection system aspect of relication can chime in.