0 votes

I have the following code that generates a random number, stores it in the list, and then removes that number so there are no repeats

var list = range(0,10)
var sample =[]

func _ready():
    for i in range(4):
        var x = randi()%list.size()

sample is now a list of 4 integers [X X X X]. I'd like to be able to get the first 3 integers from the list and index into them later, and use the last number for something else. In python, I could do something like sample[0:3] but I'm now learning its much harder to do something like this in godot.

Any good workarounds?

in Engine by (45 points)

2 Answers

+1 vote

Maybe you want the Array.slice() method? Here's an example:

func _ready():
    var a = [0, 1, 2, 3]
    var b = a.slice(0, 2)


[0, 1, 2, 3]
[0, 1, 2]

Details in the docs:


by (10,932 points)
0 votes

Hmm its funny i thought sample[0:3] applied ☹️

var nth = sample.slice(0, 2)
by (4,698 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.