Godot Engine 3.2Alpha2
Godot内置了很多游戏开发常用的功能函数
数组对象内置了一个shuffle()
函数,顾名思义就是打乱数组的顺序,常用于构造枚举类型以及其它离散有限数据的随机,非常好用
Shuffles the array such that the items will have a random order. This method uses the global random number generator common to methods such as @GDScript.randi. Call @GDScript.randomize to ensure that a new seed will be used each time if you want non-reproducible shuffling.
范例extends Node
enum{
IDLE,
MOVE,
SHOOT,
HURT,
DIE
}
var state = MOVE
func _process(delta):
match state:
IDLE:
pass
MOVE:
pass
SHOOT:
pass
HURT:
pass
DIE:
pass
_:
state = choose([IDLE,MOVE,SHOOT,HURT,DIE])
func choose(array):
randomize()
array.shuffle()
return array.front()
shuffle()
可以和front()
配合使用,注意:由于每次打乱顺序都是基于全局的随机算子,因此如果要确保每次结果不重复,需要先调用一下全局函数randomize()