Add players /agents to an array and send it to the persistence function.

Can someone help me? I’m a beginner and I’m only used to C# unity and verse is being very difficult for me. I have a script that is data persistence and another to organize a team in an array, and give coins to the team that wins, but I’m not able to get all the players on a team and send them as agents to another script to give the coins and save. To call the AwardTeam() that calls datapersistence.IncreaseGold(agent) and give the coins to all players on the team that are in the array.

YellowVSRed := class(creative_device):

@editable yellowtrigger : trigger_device= trigger_device{}
@editable redtrigger : trigger_device= trigger_device{}
@editable removeTeamtrigger: trigger_device= trigger_device{}
@editable datapersistence : DataPersistence = DataPersistence{}
var PlayersInYellowTeam : []agent = array{}
var PlayersInRedTeam : []agent = array{}

OnBegin< override>()< suspends>:void=
    yellowtrigger.TriggeredEvent.Subscribe(OnPlayerEnteredYellowTeam)

OnPlayerEnteredYellowTeam(Agent : ?agent) : void =
    if (Agent?):
        PlayersInYellowTeam + array{Agent}
        Print("team yellow")
        AwardTeam(PlayersInYellowTeam, 10)


AwardTeam(Team: []agent, Coins: int): void =
    Print("called")
    for (Index := 0..Team.Length - 1): #First test option
        if (Element := Team[Index]):
            datapersistence.IncreaseGold(Element)

    for (Player : Team):#Second test option
        datapersistence.IncreaseGold(Player)
        datapersistence.AddValueGold(Player, Coins)
        Print("team yellow coin")


spawners := class(tag){}
TextForUI< localizes>(InText : string) : message = “Coins: {InText}”

player_data := class< final>< persistable>{
Gold : int = 0
Fun1 : logic = false
Fun2 : logic = false
}

player_data_update< constructor>(OldStat:player_data)< transacts> := player_data {
Gold := OldStat.Gold
Fun1 := OldStat.Fun1
Fun2 := OldStat.Fun2
}

var Data:weak_map(player, player_data) = map{}

DataPersistence := class(creative_device):

var PlayerMap : [agent]spawners = map{}

@editable Increase : button_device = button_device{}
@editable Decrease : button_device = button_device{}
        

InitSpawners():void = {
    Spawners := GetCreativeObjectsWithTag(spawners{})
    for (Obj : Spawners) {
        if (Spawner := player_spawner_device[Obj]) {
            Spawner.SpawnedEvent.Subscribe(OnPlayerSpawned)
        }
    }
}

OnPlayerSpawned(Agent : agent) : void = {
    if (PlayerObj := player[Agent]) {
        if (PlayerExists := PlayerMap[Agent]) {

        } else {
            if (set PlayerMap[PlayerObj] = spawners{}) {
                if (not Data[PlayerObj]) {
                    if (set Data[PlayerObj] = player_data{}) {
                        Print("New Player Data Created")
                    }
                }

                spawn{UpdateValues(PlayerObj)}
            }
        }
    }
}

IncreaseGold<public>(Agent : agent) : void = {
    if (PlayerObj := player[Agent]) {
        if (PlayerData := Data[PlayerObj]) {
            if:
                OldData := Data[PlayerObj]

                set Data[PlayerObj] = player_data{
                    player_data_update< constructor>(OldData)
                    Gold := OldData.Gold + 1 
                }
            then:
                Print("Gold Increased")
        }
    }
}


AddValueGold<public>(Agent : agent, value : int): void = {
    if (PlayerObj := player[Agent]) {
        if (PlayerData := Data[PlayerObj]) {
            if:
                OldData := Data[PlayerObj]

                set Data[PlayerObj] = player_data{
                    player_data_update< constructor>(OldData)
                    Gold := OldData.Gold + value 
                }
            then:
                Print("Gold Decreased")
        }
    }
}

UnlockFun1<public>(Agent : agent): void = {
    if (PlayerObj := player[Agent]) {
        if (PlayerData := Data[PlayerObj]) {
            if:
                OldData := Data[PlayerObj]

                set Data[PlayerObj] = player_data{
                    player_data_update< constructor>(OldData)
                    Fun1 := true
                }
            then:
                Print("Fun1 Unlocked")
        }
    }
}

OnBegin< override>()< suspends>:void=
    AllPlayers := GetPlayspace().GetPlayers()
    for (Player : AllPlayers) {
        OnPlayerSpawned(Player)
    }
    Sleep(0.8)
    InitSpawners()
    GetPlayspace().PlayerRemovedEvent().Subscribe(OnPlayerRemoved)
    Increase.InteractedWithEvent.Subscribe(IncreaseGold)

onTriggerActivated(MaybeAgent : ?agent):void=
    if (Agent := MaybeAgent?):
        IncreaseGold(Agent)
    else:
        # Your code here in case the MaybeAgent = false

OnPlayerRemoved(PlayerLeft : agent): void = {
    if (Agent := agent[PlayerLeft]) {
        if (ActualPlayer := PlayerMap[Agent]) {
            var TempPlayerMap : [agent]spawners = map{}
            
            for (Key -> Value : PlayerMap, Key <> Agent) {
                set TempPlayerMap = ConcatenateMaps(TempPlayerMap, map{Key => Value})
            }

            set PlayerMap = TempPlayerMap
        }
    }
}

I finally managed to change my script. But there is still a question, how can I remove a player from the array after it goes through a trigger, can anyone give me an example?

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

YellowVSRed := class(creative_device):
    @editable yellowtrigger : trigger_device= trigger_device{}
    @editable redtrigger : trigger_device= trigger_device{}
    @editable removeTeamtrigger: trigger_device= trigger_device{}
    @editable rewardredtrigger : trigger_device= trigger_device{}
    @editable rewardyellowtrigger : trigger_device= trigger_device{}
    @editable cleararraytrigger : trigger_device= trigger_device{}
    @editable timerDevice : timer_device= timer_device{}
    @editable teleporter : teleporter_device = teleporter_device{}
    @editable datapersistence : DataPersistence = DataPersistence{}
    
    IsPlayerInTeam : logic = false

    var PlayersInYellowTeam : []agent = array{}
    var PlayersInRedTeam : []agent = array{}

    OnBegin< override>()< suspends>:void=
        yellowtrigger.TriggeredEvent.Subscribe(OnPlayerEnteredYellowTeam)
        redtrigger.TriggeredEvent.Subscribe(OnPlayerEnteredRedTeam)
        rewardredtrigger.TriggeredEvent.Subscribe(AwardRedTeam)
        rewardyellowtrigger.TriggeredEvent.Subscribe(AwardYellowTeam)
        cleararraytrigger.TriggeredEvent.Subscribe(RemovePlayerFromYellowTeam)
        #timerDevice.SuccessEvent.Subscribe(AwardRedTeam)
        for (Player : PlayersInYellowTeam, Agent := agent[Player], Fort := Agent.GetFortCharacter[]):
            Fort.EliminatedEvent().Subscribe(OnYellowEliminated)
        for (Player : PlayersInRedTeam, Agent := agent[Player], Fort := Agent.GetFortCharacter[]):
            Fort.EliminatedEvent().Subscribe(OnRedEliminated)
    
    OnYellowEliminated(EliminationResult : elimination_result): void =
        EliminatedFortCharacter := EliminationResult.EliminatedCharacter
        if (EliminatedAgent := EliminatedFortCharacter.GetAgent[]):
            teleporter.Teleport(EliminatedAgent)

    OnRedEliminated(EliminationResult : elimination_result): void =
        EliminatedFortCharacter := EliminationResult.EliminatedCharacter
        if (EliminatedAgent := EliminatedFortCharacter.GetAgent[]):
            teleporter.Teleport(EliminatedAgent)
        
            

    OnYellowPlayerEliminated(AgentFromChar : agent): void = 
        EliminatedPlayer : agent = AgentFromChar
        teleporter.Teleport(EliminatedPlayer)
            
              
              
    TeleporterAgents(): void=
        for (PlayerInYellow : PlayersInYellowTeam) 
        {
            teleporter.Teleport(PlayerInYellow)
        }
        for (PlayerInRed : PlayersInRedTeam) 
        {
            teleporter.Teleport(PlayerInRed)
        }

    OnPlayerEnteredYellowTeam(Agent: ?agent): void = 
    {
        if (Player := Agent?):
            # Check if player is already in team using loop
            var FoundPlayer : logic = false
            for (ExistingPlayer : PlayersInYellowTeam) 
            {
                if (ExistingPlayer = Player) 
                {
                  set FoundPlayer = true
                }
            }
          
            # Add player to team if not already present
            if (not FoundPlayer?) 
            {
                set PlayersInYellowTeam += array { Player }
                Print("Player added to team yellow")
                DebugTeam("Yellow Team", PlayersInYellowTeam)
            }
    }
    OnPlayerEnteredRedTeam(Agent: ?agent): void = 
        {
            if (Player := Agent?):
                # Check if player is already in team using loop
                var FoundPlayer : logic = false
                for (ExistingPlayer : PlayersInRedTeam) 
                {
                    if (ExistingPlayer = Player) 
                    {
                      set FoundPlayer = true
                    }
                }
              
                # Add player to team if not already present
                if (not FoundPlayer?) 
                {
                    set PlayersInRedTeam += array { Player }
                    Print("Player added to team Red")
                    DebugTeam("Red Team", PlayersInRedTeam)
                }
        }
    RemovePlayerFromYellowTeam(Agent: ?agent) : void = 
        set PlayersInYellowTeam = array {}


    AwardYellowTeam(Agent : ?agent) : void=
        AwardTeam(PlayersInYellowTeam, 10)
        
    AwardRedTeam(Agent : ?agent) : void=
        AwardTeam(PlayersInRedTeam, 10)

    AwardTeam(Team : []agent, Coins : int) : void =
        Print("Awarding team")
        for (Player : Team):
            datapersistence.AddValueGold(Player, Coins)
            Print("Coins awarded to")

    DebugTeam(TeamName : string, Team : []agent) : void =
        Print("{TeamName} has {Team.Length} players")
        for (Player : Team):
            Print("{TeamName} player")

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.