+1 vote

var Players = [
preload("res://Player1.gd"),
preload("res://Player2.gd"),
preload("res://Player3.gd"),
preload("res://Player4.gd")
]

So i have made this array with 4 Scenes, and i want that only one of them is choosen randomly

please help. :/

in Engine by (136 points)

1 Answer

+3 votes
Best answer

Here, I'll give you a freebie. ;) Try this function on for size.

# Based on the "random.choice()" function found in Python.
func random_choice(list: Array):
    if list.empty():
        push_error("Size of array is zero.")
        return
    return list[ randi() % list.size() ]

Pass your array to this function and it'll choose a random element.

var player = random_choice(Players)
by (2,462 points)
selected by
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 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 webmaster@godotengine.org with your username.