lvncerpedia

attachって何? sceneって何? nodeって何?

OK、ここ超重要だから、かなり整理して説明するね。 これ理解するとGodotの見え方かなり変わる。

まず全体像

Godotは極端に言うと:

Scene
└── Node
    └── Script(Attach)

でできてる。

Node とは?

「部品」これ。

例えば:

Button
Label
Camera
Sprite
Audio

全部Node。

Webで例えると

GodotWeb
NodeHTML要素
Button Node
Label Node

かなり近い。

例えば

Button

Nodeを置くとボタンが表示できる。

Nodeは親子関係を持つ

例えば:

Control
├── Label
└── Button

これは:

  • Control が親
  • Label が子
  • Button が子

Webで言うと

<div>
  <p></p>
  <button></button>
</div>

みたいな感じ。

Scene とは?

「Nodeのセット」

例えば:

TitleScene
├── Background
├── Title
├── StartButton
└── ExitButton

これ全部まとめて「1 Scene」

Scene = 画面

と思ってOK。

Webで例えると

GodotWeb
Scene1ページ
Node TreeDOM Tree

実際のSceneファイル

main.tscn とか。

中身イメージ

main.tscn
└── Control
    ├── Label
    └── Button

Attach とは?

「NodeにScriptを付ける」こと。

Scriptって何?

Nodeの「動き」

例えば Button だけだとただの見た目。

ScriptをAttachすると

押されたらゲーム開始

できる。

図で見ると

Attach前

Button
(何もしない)

Attach後

Button
└── start_button.gd

つまり

Attach = 「このNodeをこのコードで動かす」こと。

全部まとめると

例えばタイトル画面。

Scene

main.tscn

中のNode

Control
├── Label
├── StartButton
└── ExitButton

Script Attach

Control       ← main.gd
StartButton   ← start_button.gd

実際の関係

main.tscn
└── Control  ← main.gd
    ├── Label
    ├── StartButton
    └── ExitButton

君が今やってること

実は「Node Tree を組み立ててる」

add_child の意味

例えば:

add_child(button)

は:

Control
└── Button

を作ってる。

Reactっぽく言うと

かなり:

return (
  <Button />
)

に近い。

instantiate とは?

これは「Sceneから実物を生成」 例えば Enemy.tscn が設計図。 instantiateすると 本物のEnemyが出る。

図

Enemy.tscn
   ↓ instantiate()
Enemy Instance

Reactで言うと

かなり:

<Enemy />

に近い。

一番重要な理解

Godotは「Nodeの木」を作るゲームエンジン。

例えば最終的には

GameScene
├── Player
├── Enemy
├── UI
│   ├── HPBar
│   └── Timer
└── AudioManager

こんなのになる。

君向けに超ざっくり言うと

GodotWeb感覚
NodeHTML要素
Scene画面/Component
AttachJS紐付け
TreeDOM Tree
add_childappendChild
instantiateComponent生成

こんな感じ。