Now that you’ve balanced players into teams, you want to grant them the correct weapons based on what team they were balanced onto. Follow the steps below to learn how to grant players the appropriate weapons when they spawn.
Granting Weapons based on Team
In the
triad_infiltration_gameclass definition, add a new function namedGrantTeamWeapon(). This function grants a weapon to a player based on their respective team.GrantTeamWeapon(InPlayer:player):void=In
GrantTeamWeapon(), get the team for the given player. Then in aforloop, iterate through each team in theTeamsarray, getting the index for that team and storing it in a variableTeamIndex. Check if the given player’s team matches this team as a filter condition in yourforloop.VerseGrantTeamWeapon(InPlayer:player):void= if(CurrentTeam := GetPlayspace().GetTeamCollection().GetTeam[InPlayer]): for(TeamIndex -> PlayerTeam:Teams, PlayerTeam = CurrentTeam):Since your filter condition will ensure the code within the
forloop runs with the correct team, retrieve the appropriate item granter for that team by indexing intoWeaponGrantersusing theTeamIndexof that team. Finally, callGrantItem()using the given player. YourGrantTeamWeapon()code should look like:Verse# Grants players a weapon based on the index of their team in the Teams array # by indexing into the WeaponGranters array. GrantTeamWeapon(InPlayer:player):void= if(CurrentTeam := GetPlayspace().GetTeamCollection().GetTeam[InPlayer]): for(TeamIndex -> PlayerTeam:Teams, PlayerTeam = CurrentTeam): if(WeaponGranter := WeaponGranters[TeamIndex]): WeaponGranter.GrantItem(InPlayer) Logger.Print("Granted a Player on team {TeamIndex + 1} a weapon")Make sure that the order of your teams in
Teamsmatches the order of your item granters inWeaponGranters. If the Infiltrators are Team 1, then the first granter inWeaponGrantersshould be for the Infiltrators. Double-check in the editor that these values are correct.
Granting Weapons When Players Spawn
In the
triad_infiltration_gameclass definition, add a new functionOnPlayerSpawn(). This function takes anagentand uses it to callGrantTeamWeapon()to grant the appropriate weapon to the player.OnPlayerSpawn(SpawnedAgent:agent):void=In
OnPlayerSpawn(), cast theSpawnedAgentto aplayer. Then callGrantTeamWeapon()passing the player. YourOnPlayerSpawn()function should look like:Verse# Runs when any player spawns from a spawn pad. # Calls GrantTeamWeapon using the provided SpawnedAgent. OnPlayerSpawn(SpawnedAgent:agent):void= if(SpawnedPlayer := player[SpawnedAgent]): Logger.Print("Attempting to grant spawned player a weapon") GrantTeamWeapon(SpawnedPlayer)In
OnBegin(), before the call toBalanceTeams(), subscribe each player spawner’sSpawnedEventusing aforloop to theOnPlayerSpawn()function you just defined.VerseOnBegin<override>()<suspends>:void = # Get all the Teams set Teams = GetPlayspace().GetTeamCollection().GetTeams() # Save the teams to later reference them set InfiltratorsOpt = option{Teams[0]} set AttackersOpt = option{Teams[1]} set DefendersOpt = option{Teams[2]} if: Infiltrators := InfiltratorsOpt? Attackers := AttackersOpt?Save the script, build it, and click Launch Session in the UEFN toolbar to playtest the level. When you playtest your level, each player should end up on the team with the largest difference, and should spawn with an appropriate weapon for their team. Verify this behavior using the log.
Next Step
In the next step of this tutorial, you'll learn how to make the Infiltrators invisible when they spawn and when the game begins.