Select random player in an online game?

Did every client print different selected player?

Calling create player for each player that joins is fine.
But there is going to be a few problems with this code then.

This variable is local to the instance.
I don’t see you using the variable yet, but note again that this variable is local to the instance and will not be shared with the next connected player.
It should probably be moved into a Game scene that controls logic.

The function _on_start_pressed() is being fired by each player connected. (I assume this is a button press) If the signal is connected for each player either directly via code or indirectly, via scene connection and scene instantiate, then this function is likely firing for each player, every time a player connects.

If you are seeing more “Start” being printed than there are players connected then this signal is propagating.

I think the main problem of why you are seeing only player 1 is that the variable players_in_game is also local to the instance.
So each player connecting gets its own version of players_in_game and that should only ever have 1 member.
So then why are we seeing more than 1 member? It must be that the start_game() is firing for each connection resulting in a train:

  • On first connection (the host or player 1) results in only 1 signal firing because it is the only 1 that exists at that point.
  • On second connection the new player fires the signal and runs its own instance of this code. The signal propagates and fires for the 1st instance resulting in it having 2 members.
  • The third connection the new player fires the signal and runs its own instance of this code.
  • The code runs in the 2nd players instance.
  • Then the code runs once again in the 1st players instance.
  • So at this point you have 3 distinct instances:
[Player1] players_in_game.size() = 3 , players = 3   
[2nd Player] players_in_game.size() = 2, players = 2    
[3rd Player] players_in_game.size() = 1, players = 1

It very hard to tell for sure. It may be all of the above issues, 1 of the above issues, or sadly none of the above.

For sure though that if everyone connected presses start_game button, then _on_start_pressed() runs for each, and all that code runs for each, and each will have its own version of players_in_game, etc.

I recommend put the project up on github. The only way to be sure is to try running it.