Godot - How do I create a subarray of a list in Gdscript?
Asked Answered
C

1

7

I know it's possible to slice an array in python by array[2:4]. The way I get around this is to just loop through the indexes I want and append them to the new_list. This way requires more work is there just a simple way to do it like in python?

Conaway answered 13/2, 2020 at 0:6 Comment(0)
M
12

You can use the Array.slice() method added in Godot 3.2 for this purpose:

Array slice ( int begin, int end, int step=1, bool deep=False )

Duplicates the subset described in the function and returns it in an array, deeply copying the array if deep is true. Lower and upper index are inclusive, with the step describing the change between indices while slicing.

Example:

var array = [2, 4, 6, 8]
var subset = array.slice(1, 2)
print(subset)  # Should print [4, 6]
Medlock answered 13/2, 2020 at 10:50 Comment(1)
Thanks It works perfectly, I still need to learn the 3.2 featuresConaway

© 2022 - 2024 — McMap. All rights reserved.