0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Godotで実行時のノード構成が見たい

Posted at

Godot で実行時のノード構成や定義されているメソッドを確認した場合のヘルパー関数

ヘルパー関数の例

func print_node_tree(node: Node, indent: int = 0) -> void:
	# インデントを作成
	var prefix = ""
	for i in range(indent):
		prefix += "  "
	
	var type_name = node.get_class()
	var line = prefix + "|-- " + node.name + " (" + type_name + ")"
	
	# スクリプトがアタッチされている場合はそのパスも表示
	var script = node.get_script()
	if script != null:
		line += " [Script]" # 詳細なパスは長くなるため[Script]のみ
		
	print(line)

# スクリプトに定義されているメソッド一覧を出力
	if script != null and script is Script:
		var method_list = script.get_script_method_list()
		if not method_list.is_empty():
			var methods_str = []
			for method in method_list:
				# 辞書の'name'キーから関数名を取得
				methods_str.append(method.name)
			
			# メソッドリストを整形して出力
			# 組み込み関数(_readyなど)は除外されないため、全て出力されます。
			print(prefix + "  |-> Methods: [" + ", ".join(methods_str) + "]")

	# 子ノードを再帰的に処理
	for child in node.get_children():
		print_node_tree(child, indent + 1)

使い方の例

シーンにアタッチされたスクリプトの _ready()メソッド内から呼び出しするなどで利用可能

func _ready():
	# _ready()の最後にツリー全体を出力
	print("====================================")
	print("Current Scene Tree Structure:")
	print("====================================")
	# シーンツリーのルートから処理を開始
	Global.print_node_tree(get_tree().get_root())
	print("====================================")

その他

  • Globalスクリプトに追加して、プロジェクト内のどこからでも利用可能とすることも可能
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?