Hi guys. Following this thread about the Networking Blueprint tutorial made by Billy Bramer, I though I’d cross-post the C++ version I made. (Still using 4.3.1, no 4.4.0 yet )
But I’m especially interested with feedback on a problem I faced concerning ReplicatedUsing property types, where I found that a non-dedicated server was not “using” its own Rep_(function) like a normal client would do. In my case, the OnRep_Armed() function was successful on all clients in changing the bomb’s colour, but not on the server. On a dedicated server, it would be a lesser evil as changing a visual element such as the colour of the material would be a non-issue. But a server who hosts other clients (i.e. non-dedicated) could not see the change in colour. So I had to call the function myself inside the authority branch.
void ABomb::OnProjectileBounce(const FHitResult& ImpactResult, const FVector& ImpactVelocity)
{
if (Role == ROLE_Authority)
{
Armed = true;
// You must call the rep function yourself for the server or it will never change color.
OnRep_Armed();
GetWorldTimerManager().SetTimer(this, &ABomb::OnFuseExpired, FuseTime, false);
}
}
So, am I missing a UPROPERTY macro parameter, is this part of a carefully crafted way to not execute trivial code on a dedicated server (and I would be missing an additional test for it) or is this simply a bug ?
I think that’s how it’s supposed to be. The server does the actual changing of his variables since the server OWNS it and the ‘ReplicatedUsing’ tag will make sure the clients get synced with the change. OnReps won’t be called on the server. I might be wrong however.
Interesting. Just as an additional information, Blueprints don’t have this problem: a non-dedicated server does call OnRep_ on itself (I haven’t traced dedicated servers in Blueprints, though).
Neat, thanks for taking a stab at this! I’ll have to check it out later
is correct, the ReplicatedUsing functions are not automatically called on the server in C++. That was a convenience added for blueprints, but does have the unfortunate side effect of making them inconsistent right now. You can manually call them on the server if you need though, and if you need to call it but don’t want parts to execute on dedicated servers, you can also wrap code with this:
if (GetNetMode() != NM_DedicatedServer)
{
}
Thanks again, will try to look at this in-depth later when I can. I haven’t had as much time on the forums as I’d like lately (free time trapped toward moving to a new house ><), but let me know if you have any other questions and I’ll try to help out as best I can.
No, the ‘GetNetMode’ enum tells you if you are a client, dedicated server, listen server, etc. The ‘Role’ enum tells you what your role on that actor is.
So, you can be a client yet still have Role_Authority over some objects. This is my understanding so far and how I’ve been using it, I might be wrong however.