Material Box

Material Box

WEBデザイン & フリー素材

Godot - グループ内のNodeを近い順にソートする

Godot

Godot 4でGDScriptを利用してグループ内のNodeを近い順にソートする方法です。


sort_custom()を利用してグループ内のNodeを近い順にソートするsort_group_nearest()を作成しています。

sort_group_nearest()には以下の特徴があります。
・引数には任意のグループ名を指定する
・自身から近い順にNodeを並び替えた結果を返す

func sort_group_by_nearest(group):
	var nodes = get_tree().get_nodes_in_group(group)
	nodes.sort_custom(func(a, b): 
		return a.global_position.distance_to(global_position) < b.global_position.distance_to(global_position)
	)
	return nodes


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

Godot

TitleGodot - グループ内のNodeを近い順にソートする

CategoryGodot

Created

Update

AuthorYousuke.U