Godot - 如何在 Gdscript 中创建列表的子数组?
Godot - How do I create a subarray of a list in Gdscript?
我知道可以按数组 [2:4] 对 python 中的数组进行切片。我解决这个问题的方法是循环遍历我想要的索引并将它们附加到 new_list。这种方法需要更多的工作,有没有像 python?
中那样的简单方法
您可以使用 Godot 3.2 中添加的 Array.slice()
方法来实现此目的:
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.
示例:
var array = [2, 4, 6, 8]
var subset = array.slice(1, 2)
print(subset) # Should print [4, 6]
我知道可以按数组 [2:4] 对 python 中的数组进行切片。我解决这个问题的方法是循环遍历我想要的索引并将它们附加到 new_list。这种方法需要更多的工作,有没有像 python?
中那样的简单方法您可以使用 Godot 3.2 中添加的 Array.slice()
方法来实现此目的:
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
istrue
. Lower and upper index are inclusive, with thestep
describing the change between indices while slicing.
示例:
var array = [2, 4, 6, 8]
var subset = array.slice(1, 2)
print(subset) # Should print [4, 6]