C++ Multiplayer Problem

Hey,

I have problems with Networking/Multiplayer…

I want to call UI functions for specific/all clients from the GameMode or GameState.
The UI(UUserWidget) is created inside my PlayerController, but somehow I can’t get the UI functions working for multiplayer.

playercontroller.h

UFUNCTION(Reliable,Client)
void ShowCounterPC();
void ShowCounterPC_Implementation();

playercontroller.cpp

void ANPPlayerController::ShowCounterPC_Implementation()
{
    if(Role != ROLE_Authority)
    {
        if(ui_main_ != nullptr)
        {
//ptr to userwidget
                ui_main_->ShowCounter();

        }
    }
}

somehwere in the GameMode.cpp:

PlayerController->ShowCounterPC();

But the function only gets executed on the server, which i prevent by Role != ROLE_Authority
so it actually never get called.

Can anyone help, how i can execute the function for my client playercontroller?

Hi Sleicreider-

Sorry for late answer. You can read more about replication in this wiki article.

About your question:

  • you must call RPC on actor (or object) which is replicated from server to client(clients). In you situation PlayerController is right place to do this
  • you can call Client(Multicast) RPC only from server, where Role == ROLE_Authority and at all server is only place where you can get ROLE_Authority

Here you must understand that there are 2 (or more) computers which are running your game: first is server-side and second is client-side(if there is no dedicated server ofc).

So when you want some Client RPC to be executed on client-side player controller, you must find this player controller instance on server-side machine, and call there ClientRPC. Then server will replicate this call to second machine and will execute where _Implementation part, where Role != ROLE_Authority (because that isn’t server machine).

And conversely, if on client-side machine you want some code to be executed on server, then you call ServerRPC on replicated actor(or object), in your case inside player controller. Then RPC is replicated to server, server will find player controller for second player(who was initiator) and will execute _Implementation part, where Role == ROLE_Authority.

In your case you want show some widget to clients through PlayerController. Only server have all PC instances (client have only his playercontroller replicated), so you can’t call NetMulticast RPC, you need on server through iterator for each PlayerController call ClientRPC. If you want one NetMulticast RPC then you need call it from any actor, which is replicated to each client (eg. from GameState). Read this for more info about basic gameplay classes

Hth

And one more: you can call ServerRPC only on actor which is owned by client(caller), player controllers are owned by clients, but for example if you want to call ServerRPC from GameState on client, then it will be dropped, because server owns gamestate.

I’ve already solved my problem myself few days ago with the same solution.
But your answer was still very informative :slight_smile: