0 votes

Hey All,

Trying to use the shuffle() method on an array returns null for me. Here is a simple example:

func _ready():
    var nums = [1,2,3,4,5]
    print(nums)
    print(nums.shuffle())

Output:

[1, 2, 3, 4, 5]
Null

What am I doing wrong? It doesn't help if I capture it with something like:

numsShuffled = nums.shuffle()

It also doesn't help if I move it out of the _ready() function

Am I missing something or do I need to write my own shuffle function?

in Engine by (576 points)

2 Answers

0 votes
Best answer

The shuffle() function doesn't return anything; it shuffles the array on which it was called.

by (2,058 points)
selected by
0 votes

Shuffle returns void.

func _ready():
    var nums = [1,2,3,4,5]
    print(nums)
    print(nums.shuffle())
    print(nums)
by (25 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 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.