Loop works but parts within it happen after it has finished

Hello, I have a map that allows the players to select their team (2 teams) when the round starts. This works but if someone doesn’t pick a team, I want the game to auto assign a team to them with it balancing the team sizes

I have made a device that checks team sizes and allocates the players that are inside of a mutator zone into the team with the lower amount of players.

This is my code:

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

# See https://dev.epicgames.com/documentation/en-us/uefn/create-your-own-device-in-verse for how to create a verse device.

# A Verse-authored creative device that can be placed in a level
AutoSelectTeams := class(creative_device):
    @editable PreGameMutatorZone : mutator_zone_device := mutator_zone_device{}
    @editable TeamOneClassDesigner : class_and_team_selector_device := class_and_team_selector_device{}
    @editable TeamTwoClassDesigner : class_and_team_selector_device := class_and_team_selector_device{}

    var TeamOneCount : int = 0
    var TeamTwoCount : int = 0

    # Runs when the device is started in a running game
    OnBegin<override>()<suspends>:void=
        PreGameMutatorZone.AgentEntersEvent.Subscribe(AutoPickTeam)
        TeamOneClassDesigner.TeamSwitchedEvent.Subscribe(TeamOneCountIncrease)
        TeamTwoClassDesigner.TeamSwitchedEvent.Subscribe(TeamTwoCountIncrease)
    
    GetPlayers():[]player=
        return GetPlayspace().GetPlayers()

    TeamOneCountIncrease(AgentIn : agent): void =
        set TeamOneCount += 1
        Print("Team One Count : {TeamOneCount}")
        return

    TeamTwoCountIncrease(AgentIn : agent): void =
        set TeamTwoCount += 1
        Print("Team Two Count : {TeamTwoCount}")
        return

    AutoPickTeam(AgentIn : agent): void =
        Print("Auto Picking Team")
        Players:=GetPlayers()

        for (P:Players):
            if (TeamOneCount > TeamTwoCount):
                Print("Team One has more players")
                TeamTwoClassDesigner.ChangeTeamAndClass(AgentIn)
                return
            else if (TeamOneCount < TeamTwoCount):
                Print("Team Two has more players")
                TeamOneClassDesigner.ChangeTeamAndClass(AgentIn)
                return
            else:
                Print("Team sizes are the same")
                TeamTwoClassDesigner.ChangeTeamAndClass(AgentIn)
                return

This is what is output when 2 players didn’t pick a team and both teams are empty:

LogVerse: : Auto Picking Team
LogVerse: : Team sizes are the same
LogVerse: : Auto Picking Team
LogVerse: : Team sizes are the same
LogVerse: : Team Two Count : 1
LogVerse: : Team Two Count : 2

For some reason it seems that it does work but then it sets the teams AFTER the for loop even though the set teams is inside of it…

Does anyone know if I’ve done something wrong or if I’m just misunderstanding how things work.

Thank You

1 Like

Why are you running a for for every player? but not use the “P” that you have established?

I only started doing verse code a couple days ago, I assumed “P” would output a number and not the agent

Ok I figured out why it’s happening…
I didn’t make the team player count increase within the loop so it had no way of getting the lower team.
I have fixed this and it now works but because it also increases the team player count outside of the loop, the numbers aren’t accurate.

I’m going to try to get the team sizes using “GetAgents” or something like that
OR
I’m going to try to “unsubscribe” the “AgentEntersEvent” when it auto picks the teams

I have updated my code and it now appears to work.
My solution seems like it could be much simpler but I’m not knowledgeable in verse as I’ve only been using it for a couple of days…

This is my updated code:

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

# See https://dev.epicgames.com/documentation/en-us/uefn/create-your-own-device-in-verse for how to create a verse device.

# A Verse-authored creative device that can be placed in a level
AutoSelectTeams := class(creative_device):
    @editable PreGameMutatorZone : mutator_zone_device := mutator_zone_device{}
    @editable TeamOneClassDesigner : class_and_team_selector_device := class_and_team_selector_device{}
    @editable TeamTwoClassDesigner : class_and_team_selector_device := class_and_team_selector_device{}

    var TeamOneCount : int = 0
    var TeamTwoCount : int = 0
    var TempTeamOneCount : int = 0
    var TempTeamTwoCount : int = 0

    # Runs when the device is started in a running game
    OnBegin<override>()<suspends>:void=
        PreGameMutatorZone.AgentEntersEvent.Subscribe(AutoPickTeam)
        TeamOneClassDesigner.TeamSwitchedEvent.Subscribe(TeamOneCountIncrease)
        TeamTwoClassDesigner.TeamSwitchedEvent.Subscribe(TeamTwoCountIncrease)
    
    GetPlayers():[]player=
        return GetPlayspace().GetPlayers()

    TeamOneCountIncrease(AgentIn : agent): void =
        set TeamOneCount += 1
        Print("Team One Count : {TeamOneCount}")
        return

    TeamTwoCountIncrease(AgentIn : agent): void =
        set TeamTwoCount += 1
        Print("Team Two Count : {TeamTwoCount}")
        return

    AutoPickTeam(AgentIn : agent): void =
        Print("Auto Picking Team")
        Players:=GetPlayers()
        set TempTeamOneCount += TeamOneCount
        set TempTeamTwoCount += TeamTwoCount

        for (P:Players):
            if (TempTeamOneCount > TempTeamTwoCount):
                Print("Team One has more players")
                ChangeToTeamTwo(AgentIn)
                return
            else if (TempTeamOneCount < TempTeamTwoCount):
                Print("Team Two has more players")
                ChangeToTeamOne(AgentIn)
                return
            else:
                Print("Team sizes are the same")
                ChangeToTeamOne(AgentIn)
                return
    
    ChangeToTeamOne(AgentIn : agent): void =
        Print("Changing to team One")
        TeamOneClassDesigner.ChangeTeamAndClass(AgentIn)
        set TempTeamOneCount += 1
        Print("Team One Count : {TempTeamOneCount}")
        return
    
    ChangeToTeamTwo(AgentIn : agent): void =
        Print("Changing to team Two")
        TeamTwoClassDesigner.ChangeTeamAndClass(AgentIn)
        set TempTeamTwoCount += 1
        Print("Team Two Count : {TempTeamTwoCount}")
        return

This works for what I need it to do but if anyone has any simpler way of doing this I’d be happy to see just so I can get a better understanding of verse as a whole.

1 Like

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