How do you assign a role to a random player in Rec Room?
Record the Player Id of the most recent player to join (perhaps getting the player id from the welcome mat). Choose a random number from 1 to the player id inclusive using the RND chip. Verify they are present in the room, and you have a valid random player.
My own take on this was the following:
Preconditions:
- Set up a game rules chip with 2 teams.
Random selection:
- Remove the role from all players (-1 to remove role chip).
- Move everyone to team 1.
- Move everyone to team 2. (This is to get rid of gaps in the player team indexes.)
- Select a random number between 1 and size of team 2 (inclusive), take that as player team index and use the team mapper to get the player id. Assign The role to that player.
This might require some short delays between each step.
However this solution has several drawbacks:
- It interfers with the team assignments. So you can only really do this if you don't mind everyone being on the same team, or can change the team assignments afterwards in circuits.
- It requires each team to be able to contain all players in the room.
- It takes a bit of time, which means that it can break if a player leaves the room during the selection process.
Ok, here is a revised version based on the idea of skipping empty slots (new posting in order to avoid that the old comments become confusing). I'm assuming there are maximum T teams and maximum P players on each team.
Step 1: Remove the role from all players (-1 to Role Mapper chip).
Step 2: Pick a random number A between 0 and T*P (exclusive). Pick another random number B between 1 and the number of players in the game (inclusive) (number of players is provided by Player Event chip).
Step 3: Initialize a counter N with 0.
Step 4: Initialize a "slot counter" S with A.
Step 5: For any value of the slot counter S, compute the team as S/P+1; and the index on the team with (S mod P)+1. Use the Team Mapping chip to get the Player ID for each value of S. If you get a valid Player ID, increase the counter N by 1.
Step 6: If N == B, assign the role to this player and end.
Step 7: Change S to (S+1) mod (T*P).
Step 8: Continue with Step 5