- Home /
Creating List of Player
Hello, I'm trying to create a list of player to identify them by their id.
If one player join the lobby, his id should be 1 and I then I can send info by this id .
If someone leave, his id should be unallocate and availaible for the next player .
I suppose that I need a list, and then when someone join the lobby I set this Id. Can you help a bit pls ?
Answer by UnityVeris · Jan 22, 2018 at 02:46 AM
So you are also assigning an ID to each player it seems. In effect each player IS their ID, as far as your system is concerned.
A generic list could be used to handle this well. Populate it with empty ints to begin with, up to 32 or whatever your max number of players is. Create an iterator that finds and returns the first empty slot in the list whenever a new player joins.
For example:
public int FindEmptySlot(){
int iterNum = 0;
while (returnVal == 0){ // Go through all the slots
if (playerList[iterNum] == 0){ // Check if the slot is empty
// It is empty, so return the position to be filled
// in the list by the new player
return iterNum;
}
iterNum ++;
}
}
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
public Queue 2 Answers
Im not sure if i should use a list or queue for a battle sequence 0 Answers
String taken from a JSON file is not being Queued 1 Answer
Queueing methods and calling them at specific times 1 Answer