Question is off-topic or not relevant
Assign role randomly from array for my online game
hello I just started developing games. I have an idea, but I don't know how. . First there will be 3 roles, these are x, y, z. for example (10 people in the game, 2 of them will be x, 1 of them will be y, 7 of them will be z) how to randomly assign these roles to online players, and in the game at night when the options will appear on the screen and only x's will make a choice and kill someone after 10 seconds and y will make the selection if the person chosen by x also chooses y, the person z or y selected by x will not die, then this will be repeated 2-3 times
Answer by Mokomaki · Oct 04, 2019 at 08:14 PM
There must be a better way to do this but here is what i would do:
I would have a class for every player that would hold the role and an index for later:
public class Player
{
public int index;
public char role;
}
For the script that handles the role shuffling i would have an array of all the players we want to suffle:
Player[] players = new Player[10];
You would probaply assing thease players differently but for the sake of this post im going to just do it like this:
for (int i =0; i<players.Length;i++)
{
players[i] = new Player();
players[i].index = i;
}
Now every time you want to suffle the roles you can make use of the Knuth shuffle algorithm :
void Suffle(Player[] arr)
{
// Knuth shuffle algorithm
for (int t = 0; t < arr.Length; t++)
{
Player tmp = arr[t];
int r = Random.Range(t, arr.Length);
arr[t] = arr[r];
arr[r] = tmp;
}
}
After you have suffeled the indexes you can just assing the roles with a simple for loop:
for(int i = 0; i<players.Length;i++)
{
if(i<2)
players[i].role = 'x';
if (i == 2)
players[i].role = 'y';
if (i > 2)
players[i].role = 'z';
}
here is the whole code:
public class Player //
{ //
public int index; //example player class
public char role; //
} //
public class randomRoles : MonoBehaviour
{
Player[] players = new Player[10];
void Start()
{
for (int i =0; i<players.Length;i++) //
{ //
players[i] = new Player(); // example assingment
players[i].index = i; //
} //
Suffle(players); // shuffles the order of players in the array
for(int i = 0; i<players.Length;i++) //goes through every player and assings the role
{
if(i<2)
players[i].role = 'x';
if (i == 2)
players[i].role = 'y';
if (i > 2)
players[i].role = 'z';
}
}
void Suffle(Player[] arr) // Knuth shuffle algorithm
{
for (int t = 0; t < arr.Length; t++)
{
Player tmp = arr[t];
int r = Random.Range(t, arr.Length);
arr[t] = arr[r];
arr[r] = tmp;
}
}
}
can you $$anonymous$$ch me how to create a character when I'm new to this, thanks for your answer
Answer by GazihanDream · Oct 04, 2019 at 08:35 PM
can you tell me everything from the beginning
I also want to set up rooms for people to play with friends Can you $$anonymous$$ch me to set up a room? I actually want to make a mafia citizen game can you tell me everything from the beginning?
$$anonymous$$an look as much as i would like to, i just dont have the time to figure out how to make the game you want and then $$anonymous$$ch it to you. Again there are alot of great tutorials and guides on how to get started. I suggest just getting the basics from there then just start playing around with everything you can and when you get stuck somewhere you can ask here but try things yourself first. Good Luck!
Omg!, they spent like 20 $$anonymous$$utes making an answer for you and you still asking for more? GO LEARN BY YOURSELF, THIS IS NOT A CLASROO$$anonymous$$, WE ARE NOT TEACHERS.
I saw your other post asking for everything from 0.
Last advice. Read the FAQ of Unity Answers. you can come to ask for everything. If dont know ehat is an array you shouldnt be here, you still need spend at least 20 hours learning from 0.
Post closed