Shooter Game Team

Hello guys,
i am trying to add a button to the menu to change players current team in the Shooter Games example. I tried to change it from ChooseTeam() function but it only affects Server and set all the clients to the same team.
Does anyone has any idea?

Where are you getting stuck? Are you stuck on how to implement the button, or stuck on how to implement the backend logic?

I’d simplify this a teensy bit more and just have a keyboard button toggle team switching rather than worrying about a menu interface.

Thanks for the answer Slayemin.
I actually tried to set a keyboard input to call SetTeamNum() function with the team number parameter. It’s changing the player team if you’re on server but if you try it on the client side it’s not working. So my main problem is on the client side.

Update: i guess, i need to send a request to the server with the client user id(or something like that) and execute the function as server. But i am not sure how to? Does anyone has any idea?

Consider reading up on networking, this is a good resource that should cover what you need.

In particular, you will want to look at the RPC stuff. There are examples in the above link covering exactly what you are looking for.

you want to do something like (pseudo):



.h
UFUNCTION(Server, Reliable)
void Server_SwapTeam();

void SwapTeam();

.cpp
void AMyActor::Server_SwapTeam_Implementation()
{
        SwapTeam(); // ensures we are the server
}

void AMyActor::SwapTeam()
{
        if (Role < ROLE_Authority) // We don't own the actor, so call the server to run this function
        {
                Server_SwapTeam();
        }
        
        {
                PlayerState->ChangeTeamNum();
                // this might be done by itself, i can't remember
                GetGameMode()->ServerRestartPlayer();
        }

        ...?]
}


Then call swap team on the button press.

Thanks for the answers Thebluefish and NoirQ,
i solved the problem with RPC. Made a custom event(RunOnServer) and execute the function in server behalf of client.
Thanks for the help.