Teams and Classes: Organizing a Multiplayer Town
In this lesson you'll learn to
- βStudent can configure teams and classes, use class_selector devices, and read a player's team from Verse.
Student can configure teams and classes, use class_selector devices, and read a player's team from Verse.
π Reference & full walkthrough: Teams and Classes: Organizing a Multiplayer Town
π§© Your capstone piece: Red/Blue plaza teams + Explorer/Builder classes for the town CTF event
Team-Based Games: Assigning Players to Teams in Verse
Have you ever played a game where you choose between being a hunter or a piece of furniture? That's Prop Hunt! For it to work, your island must know who is on which team. In this lesson we'll set up teams in the editor and write Verse that actually reads each player's team using the real Fortnite API.
What you'll learn
- What a Team is and how UEFN tracks teams.
- How to reach the team collection from Verse.
- How to loop over every player and find their team.
- How to detect when a player joins and welcome them.
How it works
A Team is a label the game gives a group of players β like "Hunters" and "Props." In the editor you place Team Settings & Inventory devices (one per team) and Class Selector devices (Verse type: class_and_team_selector_device) so players can pick a role. Those devices route each player onto the correct team.
Verse can then read those assignments. Every playspace has a team collection, reached with GetPlayspace().GetTeamCollection(). From it you can:
GetTeams()β get the list of all teams.GetTeam[Player]β get the team a specific player is on. This is fallible (it can fail), so we call it with[]inside anifand handle failure.GetTeamAttitude[TeamA, TeamB]β check if two teams are friendly or hostile.
Let's build it
Place two Team Settings & Inventory devices (Hunters, Props) and a Class Selector in your level. Then drop in this Verse device and, in its Details panel, drag the Class Selector into the ClassSelector slot. It reports each player's team number at startup, welcomes anyone who joins mid-game, and re-reports whenever someone switches teams at the selector.
using { /Fortnite.com/Devices }
using { /Fortnite.com/Characters }
using { /Fortnite.com/Teams }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }
# A device that reads team assignments from the playspace's team collection.
team_reporter_device := class(creative_device):
# Drag your Class Selector device into this slot in the Details panel.
@editable
ClassSelector : class_and_team_selector_device = class_and_team_selector_device{}
OnBegin<override>()<suspends>: void =
# The team collection knows every team and who belongs to it.
TeamCollection := GetPlayspace().GetTeamCollection()
AllTeams := TeamCollection.GetTeams()
Print("This island has {AllTeams.Length} teams.")
# Report the current team for everyone already in the match.
for (Player : GetPlayspace().GetPlayers()):
ReportTeam(Player, TeamCollection)
# When someone joins mid-game, welcome them and report their team.
GetPlayspace().PlayerAddedEvent().Subscribe(OnPlayerJoined)
# When a player switches teams at the Class Selector, re-report.
ClassSelector.TeamSwitchedEvent.Subscribe(OnTeamSwitched)
# Handler for new players. Runs each time PlayerAddedEvent fires.
OnPlayerJoined(NewPlayer: player): void =
Print("A new player joined the match!")
ReportTeam(NewPlayer, GetPlayspace().GetTeamCollection())
# Handler for team switches made at the Class Selector device.
OnTeamSwitched(Agent: agent): void =
Print("A player switched teams at the Class Selector!")
ReportTeam(Agent, GetPlayspace().GetTeamCollection())
# Look up which team a player is on and print its position in the list.
# Takes agent (not just player) so team-switch events can reuse it.
ReportTeam(Agent: agent, Teams: fort_team_collection): void =
# GetTeam[] is fallible, so we call it inside an if with [] brackets.
if (PlayerTeam := Teams.GetTeam[Agent]):
AllTeams := Teams.GetTeams()
# Find this team's index so we can name it (0 = first team).
var Index : int = 0
for (T : AllTeams):
if (T = PlayerTeam):
Print("Player is on team number {Index + 1}.")
set Index = Index + 1
else:
Print("Player is not on any team yet.")
Walk-through:
GetTeamCollection()gives us the object that tracks all teams.GetTeams()returns the array of teams β its.Lengthtells us how many exist.GetTeam[Player]is fallible, so it lives insideif (...)with[]brackets; theelsehandles a player with no team.- We compare each team with
=(Verse equality is a single=) to find its index. PlayerAddedEvent().Subscribe(...)runsOnPlayerJoinedwhenever someone new enters.ClassSelector.TeamSwitchedEvent.Subscribe(...)fires whenever a player switches teams at the Class Selector β because the device is wired into the@editableslot, Verse can listen to it directly.
Try it yourself
- Change the welcome
Printto name the team ("Welcome, Hunter!") based on the index. - Use
GetTeamAttitude[TeamA, TeamB]to print whether two players are allies or enemies. - Subscribe to
PlayerRemovedEvent()and print a goodbye message. - Subscribe to the selector's
ClassSwitchedEventtoo, and print a message when a player picks a new class (not just a new team).
Recap
- A Team groups players; UEFN tracks teams in a team collection.
- Reach it with
GetPlayspace().GetTeamCollection(). GetTeams()lists teams;GetTeam[Player](fallible, use[]in anif) finds a player's team.PlayerAddedEvent().Subscribe(...)reacts to players joining.- Team Settings & Inventory and Class Selector devices in the editor do the actual assigning; Verse reads and reacts.
- The Class Selector's Verse type is
class_and_team_selector_deviceβ itsTeamSwitchedEventandClassSwitchedEventlet your code react to picks.
Keep going β free
You've read the intro. The rest of this lesson is free for members. Sign in to continue and track your progress.
Sign in free to continue this lessonSources
/guides/using-teams-and-classes-to-organize-your-gameBiloxi Studios original lesson
Add this lesson to your free study plan.
π§ The Keeperβs log
Quest complete? Chart your next heading from the π§ Center Village (Free) expedition.
β΅ Take me there