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初心者向け】シーンの修正に強いGDScriptのノード参照法

0
Last updated at Posted at 2025-12-19

はじめに

本記事はGodot Engine初心者の筆者が早めに知っておきたいと感じたGDScriptのノード参照方法に関する記事です。
Godotのバージョンは4.5です。

基本的なノード参照

画像のようにMainシーンの子ノードとしてChildがあり、Childの子ノードとしてGrandchildがあるとします。
main.png

この時Godot公式のチュートリアルではMainのスクリプトからChildやGrandchildを参照するには$<ノード名>またはget_node("ノード名")と書くよう教えられます。

main.gd
extends Node3D

func _ready() -> void:
	# $による参照
	$Child.do_something()
	$Child/Grandchild.do_something()
	
	# get_node()による参照
	var child = get_node("Child")
	child.do_something()
	var grandchild = get_node("Child/Grandchild")
	grandchild.do_something()

問題点

しかし、この書き方だとノード名やノードの親子構造に依存してしまうので、シーンの修正に弱いという問題があります。

問題点1. ノードの親子構造の修正

例えばGrandchildをChildからMainの子に移動すると$Child/Grandchildでnullを踏んでしまいます。
main2.png

main.gd
extends Node3D

func _ready() -> void:
	# $による参照
	$Child.do_something()
	# ノード構造がChild/Grandchildじゃないので↓でnullを踏む
	$Child/Grandchild.do_something()

問題点2. ノード名の修正

また、シーンのノード名をリネームした場合もスクリプト内のノード名ごと修正しないと$Childでnullを踏んでしまいます。
main3.png

main.gd
extends Node3D

func _ready() -> void:
	# ノード名が異なるので↓でnullを踏む
	$Child.do_something()
	$Child/Grandchild.do_something()

対処法1. @exportによるノードの割り当て

@exportでインスペクターに公開するとノードの割り当てが可能になります。
Unityでいう[SerializeField]と似た方法です。
この方法であればノード名の変更や親子構造を修正しても参照を保ったままシーン修正が可能です。

main.gd
extends Node3D

@export var child: Node
@export var grandchild: Node

func _ready() -> void:
	# ノードを割り当てればノード名やノードの親子構造の変更に影響されない
    child.do_something()
	grandchild.do_something()

export.png

対処法2. シーン固有ノード(% 記法)

https://docs.godotengine.org/ja/4.x/tutorials/scripting/scene_unique_nodes.html
もう1つがシーン固有ノードに設定する方法です。
(Godot 3.5から追加されたそうです。)
こちらは@exportとは異なり、問題点1.ノードの親子構造の修正のみに対応可能な方法です。

main.gd
extends Node3D

func _ready() -> void:
	# ノード名がChildでシーン固有ノードに設定されている場合
	%Child.do_something()
	# ノード名がGrandchildでシーン固有ノードに設定されている場合
	%Grandchild.do_something()
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?