how to setup 2 team balancing?

0xTommi
Mon, 08 Jan 2024 23:59:57 +0000
Programming & Scripting

introduction to the problem
so i have a huge problem with team balancing in my game, there should be 2 teams with 2 different class and a balanced numbers of players in each team so with a 1:1 ratio

setup in my map:

island settings

spawn pads for the 2 teams

the 2 team settings in witch i only changed the name and set the class 2 on team 2

-the class 2 designer

-this verse script from the official documentation

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

tim_balance := class(creative_device):
        # Holds the teams found with GetTeams()
        var Teams : []team = array{}
        OnBegin<override>()<suspends> : void =

            Print("Verse Device Started!")
            set Teams = Self.GetPlayspace().GetTeamCollection().GetTeams()
            AllPlayers := GetPlayspace().GetPlayers()
            #Subscribe to PlayerAddedEvent to allow team rebalancing when a new player joins the game
            Self.GetPlayspace().PlayerAddedEvent().Subscribe(OnPlayerAdded)
            Print("Beginning to balance teams")
            BalanceTeams()

        #Handles a new player joining the game

        OnPlayerAdded(InPlayer : player) : void =
            Print("A new Player joined, assigning them to a team!")
            BalanceTeams()

        <#
        For each player, find the number of players of the team they're on. Iterate through the
        list of teams and assign them to the team with the least amount of players, or their
        starting team in case of ties.
        #>

        BalanceTeams() : void =
            AllPlayers := GetPlayspace().GetPlayers()
            for (TeamPlayer : AllPlayers, CurrentTeam := GetPlayspace().GetTeamCollection().GetTeam[TeamPlayer]):
                # Assign Players to a new team if teams are unbalanced
                var TeamSize:int = 0

                if(set TeamSize = GetPlayspace().GetTeamCollection().GetAgents[CurrentTeam].Length):
                    Print("Size of this player's starting team is {TeamSize}")

                SmallestTeam : ?team = FindSmallestTeam(TeamSize)
                if (TeamToAssign := SmallestTeam?, GetPlayspace().GetTeamCollection().AddToTeam[TeamPlayer, TeamToAssign]):
                    Print("Attempting to assign player to a new team")

        FindSmallestTeam(CurrentTeamSize : int) : ?team =
            var SmallestTeam : ?team = false
            var TeamSize : int = CurrentTeamSize

            <#
            For each team Team, get the number of players on that team. If it has less players than SmallestTeam,
            set SmallestTeam to Team and update TeamSize to the number of players on the new Team
            #>

            for(Team : Teams, CandidateTeamSize := GetPlayspace().GetTeamCollection().GetAgents[Team].Length, TeamSize > CandidateTeamSize):
                set SmallestTeam = option{Team}
                set TeamSize = CandidateTeamSize
                Print("Found a team with less players: {CandidateTeamSize}")
            return SmallestTeam

problem
when i test the map i spawn as team 2 with the team 1 class in the team 1 spawner
and when i tested the map with friends actually 2 people from the same team got spawned in the same theam spawner

2 posts - 2 participants