This section shows how to find players and teams at runtime that you set up earlier.
Defining Class Members
Open Verse Explorer and double-click triad_infiltration_game.verse to open the script in Visual Studio Code.
At the top of the file, add
using { /Verse.org/Random }to access theShuffle()function. You'll use this to randomly shuffle teams of players before balancing. Also addusing { /Fortnite.com/FortPlayerUtilities }to access theRespawn()function for players, which you'll use later to teleport players to their spawn areas at the start of the game.using { /Fortnite.com/Devices } using { /Fortnite.com/FortPlayerUtilities } using { /Verse.org/Simulation } using { /Verse.org/Random } using { /UnrealEngine.com/Temporary/Diagnostics }In the
triad_infiltration_gameclass definition, add the following fields:Three editable integers named
MaximumInfiltrators,MaximumAttackers, andMaximumDefenders. InitializeMaximumInfiltratorsto 2 andMaximumAttackersandMaximumDefendersto 4. These track the maximum number of players on each team, you'll use them to balance teams dynamically. You can change these numbers for testing purposes, and to create interesting variations in the game.triad_infiltration := class(creative_device): # To avoid players not being able to join a team, you should set the maximum number # of players in the island settings to the sum of all of the Maximum(Team) variables. # Maximum number of players on the Infiltrators Team. @editable MaximumInfiltrators:int = 2 # Maxmimum number of players on the Attackers Team. @editable MaximumAttackers:int = 4 # Maximum number of players on the Defenders Team. @editable MaximumDefenders:int = 4A variable map named
TeamsAndTotals. This will map teams of players to the maximum number of players on that team.# Maximum number of players on the Infiltrators Team. @editable MaximumInfiltrators:int = 2 # Maxmimum number of players on the Attackers Team. @editable MaximumAttackers:int = 4 # Maximum number of players on the Defenders Team. @editable MaximumDefenders:int = 4 # Map of teams to their maximum number of players. var TeamsAndTotals:[team]int = map{}An editable array of teleporters named
Teleporters. This holds a reference to the teleporters which you'll use to teleport players to their spawns after team balancing.# Map of teams to their maximum number of players. var TeamsAndTotals:[team]int = map{} # Array of Teleporters that teleport players to their team's spawn once the game starts. @editable Teleporters:[]teleporter_device = array{}An editable
item_granter_devicearray namedWeaponGranters. This stores the item granters needed to grant players a weapon based on their team when they spawn.# Array of Teleporters that teleport players to their team's spawn once the game starts. @editable Teleporters:[]teleporter_device = array{} # Array of weapon granters for each team. @editable var WeaponGranters:[]item_granter_device = array{}Three optional
teamvariables namedMaybeInfiltrators,MaybeAttackers, andMaybeDefenders. These store a reference to each team to allow you to check that teams are set up correctly.# Array of weapon granters for each team. @editable var WeaponGranters:[]item_granter_device = array{} # Reference to the infiltrators team. var MaybeInfiltrators:?team = false # Reference to the attackers team. var MaybeAttackers:?team = false # Rerfernece to the defenders team. var MaybeDefenders:?team = falseA variable array of teams named
Teams. This holds a reference to all teams in the game, and you'll use this to set the optional team variables above as well as find teams to assign players to when balancing.# Reference to the infiltrators team. var MaybeInfiltrators:?team = false # Reference to the attackers team. var MaybeAttackers:?team = false # Rerfernece to the defenders team. var MaybeDefenders:?team = false # Array of all teams in the game. var Teams:[]team = array{}
Finding Players and Teams at Runtime
In
OnBegin(), update theTeamsarray with each team that you set up earlier in Experience Settings. You can use theGetTeams()function from thefort_team_collectionAPI to get an array of all teams in the playspace.OnBegin<override>()<suspends>:void = # Get all the Teams. set Teams = GetPlayspace().GetTeamCollection().GetTeams()Save a reference to each team by assigning
MaybeInfiltrators,MaybeAttackers, andMaybeDefendersto their respective teams in theTeamsarray.# Save the teams to later reference them. set MaybeInfiltrators = option{Teams[0]} set MaybeAttackers = option{Teams[1]} set MaybeDefenders = option{Teams[2]}Now you check if all three teams have been set up correctly. Because
MaybeInfiltrators,MaybeAttackers, andMaybeDefendersare each anoption, you can do this by checking if they contain a real value. If so, set the value of each team inTeamsAndTotalsto the maximum number of players for that team.Verseif: Infiltrators := MaybeInfiltrators? Attackers := MaybeAttackers? Defenders := MaybeDefenders? Logger.Print("Found all three teams") set TeamsAndTotals[Infiltrators] = MaximumInfiltrators set TeamsAndTotals[Attackers] = MaximumAttackers set TeamsAndTotals[Defenders] = MaximumDefenders Logger.Print("Set all three teams in TeamsAndTotals") else:Your
triad_infiltration_gamecode should now look like:Versetriad_infiltration_game := class(creative_device): Logger:log = log{Channel := triad_infiltration_log_channel} # To avoid players not being able to join a team, you should set the maximum number # of players in the island settings to the sum of all of the Maximum(Team) variables. # Maximum number of players on the Infiltrators Team. @editable MaximumInfiltrators:int = 2Save the script in Visual Studio Code, and in the Main Menu, under Verse, click Build Verse Scripts to update your Verse-authored device in the level.
Click Launch Session in the UEFN toolbar to playtest the level. When you playtest your level, all three teams should be set up in
TeamsAndTotals. Verify this behavior with the log.
Next Step
In the next step of this tutorial, you'll learn how to balance teams of players asymmetrically at the start of a game.