Godot - 如何在 Gdscript 中创建列表的子数组?

juj*_*umu 4 gdscript godot

我知道可以通过数组 [2:4] 在 python 中对数组进行切片。我解决这个问题的方法是循环遍历我想要的索引并将它们附加到 new_list 中。这种方式需要更多的工作,是否有一种简单的方法可以像在 python 中一样做到这一点?

Cal*_*nou 5

Array.slice()为此,您可以使用Godot 3.2 中添加的方法:

数组切片( int begin, int end, int step=1, bool deep=False )

复制函数中描述的子集并将其返回到数组中,如果deep是,则深度复制数组true。下索引和上索引包含在内,step描述切片时索引之间的变化。

例子:

var array = [2, 4, 6, 8]
var subset = array.slice(1, 2)
print(subset)  # Should print [4, 6]
Run Code Online (Sandbox Code Playgroud)