Need help assigning roles to players when they join a match

I’m working on an asymmetrical game (like Dead by Daylight), and I’m struggling to find a good method of assigning a role to players (enum variable in PlayerState, either killer or survivor). What I’d like to achieve for now is that the owner of the server will be the killer, others are survivors.

Right now I have it in the game mode’s PostLogin event. I tried counting the players there and assigning the killer role to the first player, but it turns out that this event only fires for clients(?) so I’m kinda lost at this point. Let me know if you guys have any tips!

Update! It turns out that PostLogin does get called for all players who join, including the ListenServer player. My solution does in fact work, I just missed the first log as it was buried under a lot of server initialization logs.

Isn’t there an IsServer() method that tells you whether the client running the code is the server?

@Natalo77 Sounds promising. Though this will only work if I test this locally for each player right? From the game mode this would just return true.

Here is how I did something similar. In my GameManager I have a APlayerController variable named as Host. During BeginPlay I do

Host = UGameplayStatics::GetPlayerController(GetWorld(), 0)


Since PlayerController id 0 will always be the local player my host Player Controller will be the host’s Player Controller.

A WARNING: This method will only work if your host is a listen server. If you are using a dedicated server this ain’t goin to work

@EmreBugday This is a good solution as well. As it turns out my initial solution of counting players did in fact work though! Do you know what the connection flow is if someone connects to the server? I fear that a client can enter the game sooner than the host and so the wrong person gets the killer’s role.

If your host is the listen server it is impossible for the clients to join before as the host it self is the server therefore it is the first Player Controller inside the level to ever join.

The difference between a dedicated server and a listen server is that Listen Server is a client hosted server but as I mentioned before if you are using a dedicated server which you host yourself on a physical or virtual machine than yeah, your are very right to fear from what you have mentioned.

The workflow for connection is;

PreLogin: This is where you evaluate the connection. You decide if you want to accept the connection or not. I generally use this for checking if the max player limit is reached, if the player is blacklisted by the host, does the player bought the DLC required to play in the level he/she is joining etc. I can cancel the connection if the player doesn’t meet any of these requirements. If everything is fine I let the player in.

PostLogin: This is after the player’s connection is accepted and established a successful connection with the server and joins the game. This is generally where I assign it’s team, add the player inside my ConnectedPlayers TArray, do all the checks necessary before placing the player inside the game world(such as if the starting base is destroyed or occupied by other team etc.).

And there was a method something like OnLogout or Logout which I can’t remember exactly but it’s pretty self explanatory. This is called when the connected client intentionally or unintentionally drops the connection. I use it mostly for cleaning their data and their stuff in game world etc.

these are the basic workflows for establishing connection but ofc you can always deep diver and expand it even more.

@EmreBugday Thanks for the help, I’ll figure out the rest! One last question though, in PostLogin you assign the player a team. How do you decide which team the player gets assigned to? Player count in the teams? Or is there a way to bring in data with the connection? For example, if two friends join a match, they should be in the same team.

It’s %100 dependent on your implementation because Unreal doesn’t have a logic / algorithm to decide which player is going to be in which team. What I do is; I assign the player to the team with less player count compared to other. If both teams are equal I choose randomly but ofc I give the option to change their team after they join if they so desire. I don’t have any logic which checks if they have a friend in either team but that seems like a cool idea. Shouldn’t be that hard to do as well if you are using Steam as it supports social integrations out of the box.