Tracking Players Using Maps

For UEFN, Tracking Players Using Maps, I’m a little lost on where my code is supposed to go. When I go to test, it’s not behaving as expected and my log is not appearing some of the messages I’m supposed to see.

sing { /Fortnite.com/Characters }
using { /Fortnite.com/Devices }
using { /Fortnite.com/Game }
using { /Fortnite.com/Teams }
using { /Verse.org/Simulation }

team_elimination_game := class(creative_device):
var PlayerMap : [player]int = map{}
@editable
EndGameDevice : end_game_device = end_game_device{}
@editable
var WeaponGranters : []item_granter_device = array{}
GrantWeapon(InPlayer : ?agent, WeaponTier : int) : void=
Print(“Granting Player a weapon of Tier {WeaponTier}”)
if(ItemGranter := WeaponGranters[WeaponTier], GrantedPlayer := InPlayer?):
ItemGranter.GrantItem(GrantedPlayer)
@editable
var PlayerSpawners : []player_spawner_device = array{}
@editable
var Sentries : []sentry_device = array{}
var EliminationsToEndGame : int = 0
var Teams : []team = array{}

OnPlayerSpawn(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}")

PopulateTeamsAndPlayers() : void=
    Print("Beginning to populate players")
    AllPlayers := 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)

OnPlayerEliminated(Result : elimination_result) : void=
    Print("A Player was eliminated!")

OnBegin<override>()<suspends> : void =
    # Get all the players
    set Teams = GetPlayspace().GetTeamCollection().GetTeams()
    set EliminationsToEndGame = WeaponGranters.Length
    Print("Number of eliminations to end game is {EliminationsToEndGame}")  
    for (Spawner : PlayerSpawners):
        Spawner.SpawnedEvent.Subscribe(OnPlayerSpawn) # Subscribe to each player spawn pad
1 Like

@Labyrinthx You can see the solution for your problem in the code bellow that I wrote.

The issue was that inside the OnBegin() function, call of PopulateTeamsAndPlayers() function needs to come after the “set Teams = GetPlayspace().GetTeamCollection().GetTeams()”
because Teams array needs to be populated first, not empty, so that PopulateTeamsAndPlayers() can function correctly.

If you have any more issues let me know.


using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }
using { /Fortnite.com/Game }
using { /Fortnite.com/Characters }

player_map := [player]int #this is type alias!

team_elimination_game := class(creative_device):

@editable
var WeaponGranters : []item_granter_device = array{}

var EliminationsToEndGame : int = 0

@editable
EndGameDevice : end_game_device = end_game_device{}

@editable
var Sentries : []sentry_device = array{}

@editable
var PlayerSpawners : []player_spawner_device = array{}

var Teams : []team = array{}

#var PlayerMap : [player]int = map{}
var TeamMap : [team]player_map = map{}

# Runs when the device is started in a running game
OnBegin<override>()<suspends>:void=
    # TODO: Replace this with your code
    # PopulateTeamsAndPlayers()

    set Teams = GetPlayspace().GetTeamCollection().GetTeams()

    PopulateTeamsAndPlayers()

    set EliminationsToEndGame = WeaponGranters.Length
    Print("Number of eliminations to end game is {EliminationsToEndGame}")
    for(Spawner : PlayerSpawners):
        Spawner.SpawnedEvent.Subscribe(OnPlayerSpawn)

<#        
OnPlayerSpawn(InPlayer : agent) : void =
    Print("A player just spawned!")
    #WeaponTier : int = 0
    if(WeaponTier := PlayerMap[InPlayer]) :
        GrantWeapon(option{InPlayer}, WeaponTier)
    #Print("Spawned Player was granted a gun of tier {WeaponTier}")
#>

OnPlayerSpawn(InPlayer : agent) : void =
    Print("A player just spawned!")
    if:
        PlayerTeam:team := GetPlayspace().GetTeamCollection().GetTeam[InPlayer]
        WeaponTier:int := TeamMap[PlayerTeam][InPlayer]
    then:
        GrantWeapon(option{InPlayer}, WeaponTier)
        Print("Spawned Player has granted a gun of tier {WeaponTier}") 
    else:
        Print("FAILS IN ON-PLAYER-SPAWN")       
    
GrantWeapon(InPlayer : ?agent , WeaponTier : int) : void =
    Print("Granting Player a weapon of Tier {WeaponTier}")
    if(ItemGranter := WeaponGranters[WeaponTier], GrantedPlayer := InPlayer?) :
        ItemGranter.GrantItem(GrantedPlayer)
    else:
        Print("FAILS IN GRANT-WEAPON")         
    
# PopulateTeamsAndPlayers() : void =
#     Print("Beginning to populate players")
#     AllPlayers := 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)

PopulateTeamsAndPlayers() : void =
    Print("Beginning to populate players")
    for(Team : Teams, TeamPlayers := GetPlayspace().GetTeamCollection().GetAgents[Team]):
        Print("ENTERED #1 FOR")
        var PlayerMap : player_map = map {}
        for(Agent : TeamPlayers, TeamPlayer := player[Agent], FortCharacter := Agent.GetFortCharacter[]):
            Print("ENTERED #2 FOR")
            if(set PlayerMap[TeamPlayer] = 0, WeaponTier := PlayerMap[TeamPlayer]):
                Print("Assigned Player to PlayerMap with Tier {WeaponTier}")
                FortCharacter.EliminatedEvent().Subscribe(OnPlayerEliminated)
            else:
                Print("FAILS IN POPULATE TEAMS AND PLAYERS #1 IF")         
        if(set TeamMap[Team] = PlayerMap):
            Print("Successfully set this team in the TeamMap")
        else:
            Print("FAILS IN POPULATE TEAMS AND PLAYERS #2 IF")
    # if expression fails in FOR failure context you must to find a way to handle that and show info about failure                                   


OnPlayerEliminated(Result : elimination_result) : void =
    Print("A Player was eliminated!")