You're going to use a map to track the number of eliminations a player has scored. Maps provide a handy association of keys to values, and in this tutorial, you’ll use the player as the key and their associated weapon tier (represented as an int) as the value. That way, you can retrieve a player’s current tier given just a reference to the player. An example of how a map of player-to-weapon tier associations might look is provided below.
Key | player 1 | player 2 | player 3 |
Value | 1 | 2 | 2 |
Weapon in Game | Combat Pistol L1 | Flint-Knock Pistol L1 | Flint-Knock Pistol L1 |
Follow these steps to set up and populate your map of players:
Add
[player]intmap namedPlayerMapto theteam_elimination_gameclass. This stores a reference to each player and their weapon tier.Verseteam_elimination_game := class(creative_device): var PlayerMap : [player]int = map{}Add a new method
PopulateTeamsAndPlayers()to theteam_elimination_gameclass. This method populates yourPlayerMap, and will be called fromOnBegin().VersePopulateTeamsAndPlayers() : void= Print("Beginning to populate players")Add a new method,
OnPlayerEliminated, to theteam_elimination_gameclass. This is called whenever a player is eliminated, and will determine who the eliminating player is by accessing theelimination_resultstruct passed as an argument.VerseOnPlayerEliminated(Result : elimination_result) : void= Print("A Player was eliminated!")When the game begins, you need to iterate through the list of players and set them all to the first weapon tier. Inside
PopulateTeamsAndPlayers, get all players usingGetPlaySpace().GetPlayers()and save them in an arrayAllPlayers. For each player, retrieve theFortCharacterfor that player and save it in a variableFortCharacter. Set that player’s score in thePlayerMapto 0 to represent the first weapon in theWeaponGrantersarray, then subscribeFortCharacter.EliminatedEvent()toOnPlayerEliminated.VerseAllPlayers := GetPlayspace().GetPlayers() for (Agent : AllPlayers, TeamPlayer := player[Agent], FortCharacter := TeamPlayer.GetFortCharacter[]): if(set PlayerMap[TeamPlayer] = 0, WeaponTier := PlayerMap[TeamPlayer]): Print("Assigned Player to PlayerMap with Tier {WeaponTier}") FortCharacter.EliminatedEvent().Subscribe(OnPlayerEliminated)With a reference to the player’s score in the
PlayerMapset, you can grant players their first weapon. ModifyOnPlayerSpawnto grant players their first weapon. When a player spawns, retrieve their weapon tier fromPlayerMapand store it in a variableWeaponTier, then callGrantWeaponpassingWeaponTierand a reference to the player.VerseOnPlayerSpawn(InPlayer : agent) : void = Print("A player just spawned!") if(WeaponTier := PlayerMap[InPlayer]): GrantWeapon(option{InPlayer}, WeaponTier) Print("Spawned Player was granted a gun of tier {WeaponTier}")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 spawn with the first weapon in the
WeaponGrantersarray. Verify this behavior with the log.
Mapping Teams of Players
With your map of players set up, it’s helpful to think about how you check a player’s score. Since your script will automatically promote the player on the team with the least eliminations, it is not useful to check the weapon tier of the enemy team’s players. You might already see a problem emerging, since PlayerMap doesn’t differentiate between teams.
To get around this problem, you can use another map. For this, you’ll adapt the PlayerMap you set up previously into a nested map system. Since key-value pairs in a map can be any pairs of types, it makes sense that a team key could have another map as its value. The first map will associate teams (key) to another map of players on that team (value). The inner map will associate players (key) to their score (value).
Now, given a player, you can verify what team they’re on using GetTeam(). From there you can retrieve a list of teammates to compare the number of eliminations against.
The syntax of the nested map [team][player]int might not be clear. To make this easier to understand, you can take advantage of type aliasing to create a simpler name to reference the map. In this tutorial you’ll give [player]int the alias of player_map. This means you can use the name player_map any time you would need [player]int, and the nested map can be rewritten as [team]player_map, or a map that associates teams to maps of players.
Follow these steps to adapt your map into a nested map system:
Above the
team_elimination_gameclass definition, add an alias for[player]intnamedplayer_map.Verseplayer_map := [player]int # This is a type alias! team_elimination_game := class(creative_device):Replace the
PlayerMapvariable you set up earlier inteam_elimination_gamewith a new variableTeamMapof type[team]player_map.Verse# Map of Team Maps, where the key is the team and the value is a map of # player->int key-value pairs var TeamMap : [team]player_map = map{}Since the value of
TeamMapis of typeplayer_map, for each team, you need to initialize a map of players, populate them, set each player’s score to0, then assign the map of players toTeamMap. ModifyPopulateTeamsAndPlayers()with the updated code.For each team, retrieve the players in that team and store them in a variable
TeamPlayers. Initialize a new variablePlayerMapof typeplayer_mapto map players to their score.VersePopulateTeamsAndPlayers() : void= Print("Beginning to populate players") for (Team : Teams, TeamPlayers := GetPlayspace().GetTeamCollection().GetAgents[Team]): var PlayerMap : player_map = map {}For each player in
TeamPlayers, retrieve theFortCharacterfor that player and save it in a variableFortCharacter. Set that player’s score in thePlayerMapto0to represent the first weapon in theWeaponGrantersarray, then subscribeFortCharacter.EliminatedEvent()toOnPlayerEliminated.VersePopulateTeamsAndPlayers() : void = Print("Beginning to populate players") for (Team : Teams, TeamPlayers := GetPlayspace().GetTeamCollection().GetAgents[Team]): var PlayerMap : player_map = map {} for (Agent : TeamPlayers, TeamPlayer := player[Agent], FortCharacter := Agent.GetFortCharacter[]): if(set PlayerMap[TeamPlayer] = 0, WeaponTier := PlayerMap[TeamPlayer]): Print("Assigned Player to PlayerMap with Tier {WeaponTier}") FortCharacter.EliminatedEvent().Subscribe(OnPlayerEliminated)Finally, set
PlayerMapas the value of the currentTeamkey inTeamMap. YourPopulateTeamsAndPlayerscode should look like below.VersePopulateTeamsAndPlayers() : void = Print("Beginning to populate players") for (Team : Teams, TeamPlayers := GetPlayspace().GetTeamCollection().GetAgents[Team]): var PlayerMap : player_map = map {} for (Agent : TeamPlayers, TeamPlayer := player[Agent], FortCharacter := Agent.GetFortCharacter[]): if(set PlayerMap[TeamPlayer] = 0, WeaponTier := PlayerMap[TeamPlayer]): Print("Assigned Player to PlayerMap with Tier {WeaponTier}") FortCharacter.EliminatedEvent().Subscribe(OnPlayerEliminated) if(set TeamMap[Team] = PlayerMap): Print("Successfully set this team in the TeamMap")
Once you've set the
TeamMap, updateOnPlayerSpawnto access a player’s team usingGetTeam[]and store it in a local variablePlayerTeam. SetWeaponTierby retrieving the player’s current score fromTeamMap, using bothPlayerTeamand a reference to the player. The updatedOnPlayerSpawn()should look like the following code.VerseOnPlayerSpawn(InPlayer : agent) : void = Print("A player just spawned!") if: PlayerTeam := GetPlayspace().GetTeamCollection().GetTeam[InPlayer] WeaponTier:int := TeamMap[PlayerTeam][InPlayer] then: GrantWeapon(option{InPlayer}, WeaponTier) Print("Spawned Player was granted a gun of tier {WeaponTier}")
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.
Next Step
In the next step of this tutorial, you’ll learn how to grant players weapons when they score an elimination.