Material Box

Material Box

WEBデザイン & フリー素材

Godot - グループ内のNodeのプロパティ値でソートする

Godot

グループ内のNodeのプロパティでソートするGDScriptです。


sort_custom()を利用してグループ内のNodeのプロパティでソートする関数を作成しています。
コード例では、Nodeに付与したプロパティ「life」の値の多い順にソートしています。

func sort_group_by_life(group):
	var nodes = get_tree().get_nodes_in_group(group)
	nodes.sort_custom(func(a, b): 
		return a.life > b.life
	)
	return nodes


func _ready():
	var enemies = sort_group_by_life("enemies")

以下は複数のプロパティに対応する為に、引数にてプロパティの種類を受け取るように変更しています。

func sort_group_by_hp(group, property):
	var nodes = get_tree().get_nodes_in_group(group)
	nodes.sort_custom(func(a, b): 
		return a.property > b.property
	)
	return nodes


func _ready():
	var enemies = sort_group_by_nearest("enemies", "life")

Godot

TitleGodot - グループ内のNodeのプロパティ値でソートする

CategoryGodot

Created

Update

AuthorYousuke.U