📖 Scenario: You are creating a simple multiplayer game lobby where players can join and be matched into a game room. This is a common feature in online games where players wait in a lobby before starting a match.
🎯 Goal: Build a basic lobby system that stores player names, sets a maximum number of players per room, matches players into rooms, and displays the matched rooms.
📋 What You'll Learn
Create a list to store player names
Create a variable for maximum players per room
Write logic to group players into rooms based on the max players
Print the list of rooms with their players
💡 Why This Matters
🌍 Real World
Game developers use lobbies and matchmaking to organize players into game sessions smoothly.
💼 Career
Understanding lobby and matchmaking basics is essential for multiplayer game programming roles.
Progress0 / 4 steps
1
Create the player list
Create a List<string> called players and add these exact player names: "Alice", "Bob", "Charlie", "Diana", "Ethan", "Fiona".
Unity
Hint
Use new List<string> { ... } to create the list with the exact player names.
2
Set maximum players per room
Create an int variable called maxPlayersPerRoom and set it to 3.
Unity
Hint
Just declare an integer variable with the exact name and value.
3
Group players into rooms
Create a List<List<string>> called rooms. Use a for loop with variable i to group players from players into rooms of size maxPlayersPerRoom. Add each group as a new list to rooms.
Unity
Hint
Use players.GetRange(i, Math.Min(maxPlayersPerRoom, players.Count - i)) inside the loop to get each room's players.
4
Display the rooms and players
Use a for loop with variable roomIndex to print each room number starting from 1. Inside it, use a foreach loop with variable player to print each player's name in that room. Use System.Console.WriteLine for printing.
Unity
Hint
Use nested loops: outer loop for rooms, inner loop for players. Use Console.WriteLine with string interpolation to print.
Practice
(1/5)
1. What is the main purpose of a lobby in Unity multiplayer games?
easy
A. To let players wait and prepare before the game starts
B. To automatically find and connect players
C. To render 3D graphics for the game
D. To save player progress locally
Solution
Step 1: Understand the role of a lobby
A lobby is a waiting area where players gather before the game begins.
Step 2: Differentiate from matchmaking
Matchmaking is the process of finding and connecting players automatically, not the lobby itself.
Final Answer:
To let players wait and prepare before the game starts -> Option A
Quick Check:
Lobby = waiting area before game [OK]
Hint: Lobbies are for waiting; matchmaking is for finding players [OK]
Common Mistakes:
Confusing lobby with matchmaking
Thinking lobby handles graphics
Assuming lobby saves progress
2. Which of the following is the correct way to override a lobby method in Unity C#?
easy
A. public override void OnPlayerJoined() {}
B. override public void OnPlayerJoined() {}
C. public void OnPlayerJoined() override {}
D. void override OnPlayerJoined() {}
Solution
Step 1: Recall C# override syntax
In C#, the correct syntax places 'override' before the return type.
Step 2: Check each option
public override void OnPlayerJoined() {} uses 'public override void MethodName()' which is correct syntax.
Final Answer:
public override void OnPlayerJoined() {} -> Option A
Quick Check:
Override keyword before return type [OK]
Hint: In C#, 'override' comes before the return type [OK]
Common Mistakes:
Placing override after method name
Omitting override keyword
Wrong order of keywords
3. Given this code snippet in a Unity lobby script:
public override void OnPlayerJoined(Player player) {
Debug.Log($"Player {player.Name} joined.");
if (Players.Count >= 4) {
StartGame();
}
}
What happens when the 4th player joins?
medium
A. The lobby closes without starting the game
B. Nothing happens until manually started
C. The game starts automatically
D. An error occurs because Players.Count is undefined
Solution
Step 1: Analyze the OnPlayerJoined method
When a player joins, it logs their name and checks if Players.Count is 4 or more.
Step 2: Understand the condition
If Players.Count is 4 or more, StartGame() is called, starting the game automatically.
Final Answer:
The game starts automatically -> Option C
Quick Check:
Players.Count >= 4 triggers StartGame() [OK]
Hint: Check conditions that trigger StartGame() call [OK]
Common Mistakes:
Assuming manual start needed
Thinking Players.Count is undefined
Confusing lobby closing with game start
4. Identify the error in this matchmaking code snippet:
public void Matchmake() {
var match = Matchmaker.CreateMatch();
match.OnPlayerJoined += OnPlayerJoined;
match.Start();
}
void OnPlayerJoined(Player player) {
Debug.Log("Player joined: " + player.Name);
}
medium
A. Start() method does not exist on match object
B. Event subscription syntax is incorrect
C. OnPlayerJoined method must be public to subscribe
D. Matchmaker.CreateMatch() might return null and is not checked
Solution
Step 1: Review event subscription
The syntax 'match.OnPlayerJoined += OnPlayerJoined;' is correct for subscribing to events.
Step 2: Check for null safety
Matchmaker.CreateMatch() might return null if no match is found, but code does not check for null before using match.
Final Answer:
Matchmaker.CreateMatch() might return null and is not checked -> Option D
Quick Check:
Always check for null after CreateMatch() [OK]
Hint: Always check if CreateMatch() returns null before use [OK]
Common Mistakes:
Assuming event subscription syntax is wrong
Ignoring null checks
Assuming Start() method is missing
5. You want to create a lobby that only starts the game when all players are ready. Which approach is best in Unity matchmaking?
hard
A. Call StartGame() immediately when the first player joins
B. Override OnPlayerJoined to check a 'ready' flag on each player before calling StartGame()
C. Use a timer to start the game after 30 seconds regardless of players
D. Disable matchmaking and start game manually from UI
Solution
Step 1: Understand the requirement
The game should start only when all players are ready, not just when they join.
Step 2: Choose the best method
Overriding OnPlayerJoined to check each player's 'ready' status before starting the game ensures all are prepared.
Final Answer:
Override OnPlayerJoined to check a 'ready' flag on each player before calling StartGame() -> Option B
Quick Check:
Check readiness before starting game [OK]
Hint: Check all players' ready flags before starting game [OK]
Common Mistakes:
Starting game too early
Ignoring player readiness
Disabling matchmaking unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.