Now that you’ve set up your maps, the next step is to grant weapons when a player scores an elimination.
When a player scores an elimination, you want to apply these two rules:
If the player is not at the highest weapon tier on their team, or all players are at the same weapon tier, promote them to the next weapon tier.
Otherwise, if there is a player on their team at a lower weapon tier, promote that player instead.
This will ensure that players advance through weapons as a team, and no player will be more than one weapon ahead of or behind any of their teammates.
To do this, you’ll need to know when a player scores an elimination, then compare their weapon tier against all the other players on their team and promote a player based on the two rules above.
Follow these steps to grant weapons to players when they score an elimination.
Add a new method
GiveNextWeaponto theteam_elimination_gameclass. This method grants a player a weapon whenever they or someone on their team scores an elimination based on the two rules above.VerseGiveNextWeapon(EliminatingPlayer : agent) : void = Print("Finding a player to promote")Update
OnPlayerEliminatedto track which player scored an elimination. BecauseOnPlayerEliminatedaccepts anelimination_result, you get reference to both an eliminated and an eliminating character. Because players can be eliminated by various means (such as fall damage, sentries, self damage, and so on), you need to deduce whetherEliminatingCharacteris an actualFortCharacter(that is, an actual player).Get a reference to
Result.EliminatingCharacterand save it in a local option variableEliminator. Check ifEliminatoris a validFortCharacter, and if so save the agent for that character in another variableEliminatorAgent. Finally passEliminatorAgenttoGiveNextWeaponVerseOnPlayerEliminated(Result : elimination_result) : void = Print("A Player was eliminated!") Eliminator := Result.EliminatingCharacter if (FortCharacter := Eliminator?, EliminatorAgent := FortCharacter.GetAgent[]): GiveNextWeapon(EliminatorAgent)
GiveNextWeaponneeds to track several variables in order to grant the correct player a new weapon. Add the following declarations toGiveNextWeapon.A variable int
WeaponTier. This tracks the weapon tier of the player to grant a weapon to.An optional agent variable named
MaybePlayerToGrant, which is the player to grant a weapon to. By default this is the player passed toGiveNextWeapon.An optional team variable named
MaybePlayerTeam, which is the team of the player to grant a weapon to. By default, this is the team of the player passed toGiveNextWeapon.VerseGiveNextWeapon(EliminatingPlayer : agent) : void = Print("Finding a player to promote") var WeaponTier : int = 0 var MaybePlayerToGrant : ?agent = option{EliminatingPlayer} # The player to grant a gun to var MaybePlayerTeam : ?team = option{GetPlayspace().GetTeamCollection().GetTeam[EliminatingPlayer]} # The team this player is on
Extract the value of
MaybePlayerTeaminto a local variablePlayerTeam, then setWeaponTierto the value of the player’s score inTeamMap.Versevar MaybePlayerTeam : ?team = option{GetPlayspace().GetTeamCollection().GetTeam[EliminatingPlayer]} # The team this player is on if(PlayerTeam := MaybePlayerTeam?, set WeaponTier = TeamMap[PlayerTeam][EliminatingPlayer]):Iterate through each player on the team, and compare their weapon tier. If you find a player at a lower tier, set
MaybePlayerToGrantto that player, andWeaponTierto their score. Note that becauseTeamMapis a map, you can extract both the key (player) and value (weapon tier) as local variables using theTeammate -> TeammateTiersyntax.Verseif(PlayerTeam := MaybePlayerTeam?, set WeaponTier = TeamMap[PlayerTeam][EliminatingPlayer]): for(Teammate -> TeammateTier : TeamMap[PlayerTeam], TeammateTier < WeaponTier): Print("Found a Teammate with a lower Tier at Tier {TeammateTier}") if(set WeaponTier = TeamMap[PlayerTeam][Teammate]): set MaybePlayerToGrant = option{Teammate}Once you’ve found the player at the lowest (or tied for lowest) weapon tier, increment
WeaponTierby one, then set their weapon tier inTeamMaptoWeaponTier.Verseif(PlayerTeam := MaybePlayerTeam?, set WeaponTier = TeamMap[PlayerTeam][EliminatingPlayer]): for(Teammate -> TeammateTier : TeamMap[PlayerTeam], TeammateTier < WeaponTier): Print("Found a Teammate with a lower Tier at Tier {TeammateTier}") if(set WeaponTier = TeamMap[PlayerTeam][Teammate]): set MaybePlayerToGrant = option{Teammate} set WeaponTier = WeaponTier + 1 if(PlayerTeam := MaybePlayerTeam?, PlayerToGrant := player[MaybePlayerToGrant?], set TeamMap[PlayerTeam][PlayerToGrant] = WeaponTier): Print("Eliminating Player Tier is now {WeaponTier}")This is a good place to check if a player has won the game, because incrementing their weapon tier may push them over the number of eliminations required to end the game.
Create a new method
EndGamein theteam_elimination_gameclass. This method activates theEndGameDeviceon the given player when they reach the final weapon tier. The completed method should look like this:VerseEndGame(InPlayer : agent) : void = Print("Player reached final Weapon Tier, activating EndGameDevice") EndGameDevice.Activate(InPlayer)Back in
GiveNextWeapon, after incrementing the player’s weapon tier, checkif WeaponTier >= EliminationsToEndGame. If so, callEndGamepassing the player.Verseif(PlayerTeam := MaybePlayerTeam?, PlayerToGrant := player[MaybePlayerToGrant?], set TeamMap[PlayerTeam][PlayerToGrant] = WeaponTier): Print("Eliminating Player Tier is now {WeaponTier}") if(WeaponTier >= EliminationsToEndGame): EndGame(EliminatingPlayer)
Otherwise, call
GrantWeaponon theGrantedPlayer.Verseif(WeaponTier >= EliminationsToEndGame): EndGame(EliminatingPlayer) GrantWeapon(MaybePlayerToGrant, WeaponTier)The completed
GiveNextWeaponmethod should look like the following code.VerseGiveNextWeapon(EliminatingPlayer : agent) : void = Print("Finding a player to promote") var WeaponTier : int = 0 var MaybePlayerToGrant : ?agent = option{EliminatingPlayer} # The player to grant a gun to var MaybePlayerTeam : ?team = option{GetPlayspace().GetTeamCollection().GetTeam[EliminatingPlayer]} # The team this player is on if(PlayerTeam := MaybePlayerTeam?, set WeaponTier = TeamMap[PlayerTeam][EliminatingPlayer]): for(Teammate -> TeammateTier : TeamMap[PlayerTeam], TeammateTier < WeaponTier): Print("Found a Teammate with a lower Tier at Tier {TeammateTier}") if(set WeaponTier = TeamMap[PlayerTeam][Teammate]): set MaybePlayerToGrant = option{Teammate}
Save the script in Visual Studio Code, compile it, and click Launch Session in the UEFN toolbar to playtest the level. When you playtest your level, you should again spawn with the first weapon in the WeaponGranters array. Verify this behavior with the log. Scoring an elimination on an enemy player should promote you to the next weapon. Promotions should follow the two rules outlined above. Promoting past the final weapon in the sequence should end the game with the eliminating player as the winner.
Next Step
In the next step of this tutorial, you’ll learn how to add players to your map and assign them a weapon when they join a game in progress.