Get list of all connected players?

I recently posted this question on the question board;

And I’ve since tried even more things to get the output I want. It’s a really weird thing why it doesn’t work.

Anyway, in absence I’m forced to consider alternatives. One alternative is to store all logged in players in a database, then update it every time you open or close the game.
This isn’t really a problem to do, as I’ve already got the basis of a database connection up and running.

The problem is that I have no idea of how I would create sessions with the data I can store in a database. At most I’d have a user id (from another table) or a username, but since I’ve yet to find a way to change the Name variable through arguments, I’m stuck with everyone being named Player.

1 Like

The connected players can be get in different ways.

2 Examples:

  • Iterate through the PlayerControllers. The server has one for every player (but only the server. The Clients only know their own).

  • Get the GameState and get the “PlayerArray”. This is an array of all PlayerStates. These contain PlayerName, Score, Ping and such things.

I would recommened the second example.

6 Likes

Getting the GameState still gives me an extra playerstate I don’t want. I run it trough “Run on Sever” replication, and just running it in the editor I’m getting two iterations, running dedicated server and a second client (still in editor) I’m getting 3 iterations. So there’s a dud in there somewhere, and I have no idea how to remove it. I tried Has Authority, but that won’t run at all.

[Edit] I solved it. Apparently one of them has an empty PlayerName, so I just check if it’s empty. Odd.

2 Likes

I’m a tad late, but for anyone wondering, I believe the empty PlayerName/extra player is generated by the GameMode on launch. I found this in the GameModeBase documentation.

4 Likes

I think you guys are all making this too complicated. I am currently testing a solution to this issue, and if it works it will work for Locally Hosted games and for Dedicated Servers. The Solution is to GetCurrentPlayers, then set that value in the Widget that displays the Player Count (This Lets a Locally Hosted Match add 1 Controller for the Local Host). Then in the Gamemode, add +1 and -1(since all controllers exist there) OnPostLogin, and OnLogout, which is updated in the widget that displays it every time someone hits refresh. I’ll let you guys know how it goes.