0 votes

I am making a multiplayer game with two teams competing against each other. How do I randomly sort these players onto the two teams and have the teams remain even?

Godot version 3.5.stable
in Engine by (28 points)

create two teams

use list (array)

var team_a = []
var team_b = []

For example, teams of 4 each

if team_a.size < 4:

Just an idea. Maybe it will guide you

2 Answers

+1 vote
Best answer

Do you have a set of players that need to be sorted into any team, as long as the two teams are even in numbers? Maybe try putting them in different groups. If you have an array of the players, a simple method is to sort them like this:

# The array that has all of the player objects. Here, we'll first shuffle it up.
# Go through the array, adding the first half of the array to a group.
if array_of_players.size() % 2 == 0:
    for n in range(array_of_players.size()):
        # The "+1" is added here because iterating over a range starts at 0.
        if n + 1 <= array_of_players.size() / 2:
            array_of_players[n].add_to_group("Red team")
            array_of_players[n].add_to_group("Blue team")

Hope this helps.

by (3,144 points)
selected by
0 votes

If you have two teams then the number of players should be an even number, otherwise you get one team with exactly one player more than the other. Now if the number of players is even then you can do the following for each player:

  • toss a coin
  • if heads go to team 1
  • else go to team 2
by (14 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.