- Home /
How to set the player number in a network game?
Hello,
I'm creating a game that involves playing over the network. However, I am stuck on an issue regarding how to determine which player is player one or two. The way the players meet online is through matchmaking (server pairs up players randomly). How would I be able to set a variable known as playerNumber once a match has been made? Thanks!
If server pairs up players randomly, set the numbers randomly too.. If I understood your question, you are making game for a limited number of players in game.. If it's so, there should be a maximum number of players in scene or in game, so set them gradually as they connect or randomly, just make sure that every player has it's own numer..
Ok, thanks! I will have it so that once the amount of players are met I will set their playerNumber into the order they connected.
Answer by Noah Dyer · Jan 08, 2015 at 01:56 AM
You may want to combine this logic with other gamestate issues, but here's an example dealing only with your specific question regarding player number.
Create an empty gameobject called something like PlayerNumberManager, or something similar
Create a Script on the object, call it something like PlayerNumberManagerScript.
In the script create an int called playerNumber. You can leave it at null, or initialize to 0 or whatever suits your purposes.
Create a function called something like GetPlayerNumberFromServer(). That function should ask the server what player it considers you to be, and sets playerNumber accordingly.
Call GetPlayerNumberFromServer() when the appropriate condition is met.
Reference playerNumber where relevant.
If playerNumber is going to be referenced by other scripts, it has to be public. If you want to trigger GetPlayerNumberFromServer() from another script, it will need to be public.
You might also consider doing additional work to make PlayerNumberManagerScript a singleton class, if you're worried about someone creating a second object and attaching a PlayerNumberManagerScript.
Thanks for breaking it down. I'm new to the whole network program$$anonymous$$g side of unity. I know how to code, I've just been wondering how to accomplish this task for a little while. Thanks a lot!
Your answer
Follow this Question
Related Questions
Unity Photon Virtual Joystick Problem HELP???? 1 Answer
send message by networking 0 Answers
Multiplayer Game Disconnects 1 Answer
How can I create a simple chat system for my multiplayer game? 1 Answer
Online Rpg Animation does not work 0 Answers