godot-nodes-scenes
它能做什么
该技能讲解如何用节点和场景构建 Godot 游戏,它们是引擎的基本构件。适合需要清晰项目结构的游戏制作者。
适用场景
- ✓当你在 Godot 中为角色、子弹、敌人或菜单设计场景时。
- ✓当你想在游戏运行时创建场景的副本时。
- ✓当你看到节点找不到或节点已被删除的错误时。
技能链接
https://github.com/gamedev-skills/awesome-gamedev-agent-skills/blob/main/skills/godot/godot-nodes-scenes/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请在 Godot 中为我制作一个可复用的敌人场景,让我可以在关卡中放置多个。”
每周 5 个新技能,1 分钟读完。
工作原理
该技能讲解如何制作小型场景,将其变成可复用的模板,在游戏运行时添加它们,并安全地访问节点,同时说明如何注册名为自动加载的全局辅助对象。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Godot Nodes & Scenes (4.x)
Compose games from nodes and scenes, instance them at runtime, and access the tree without crashing on freed or missing nodes. Targets Godot 4.7.
When to use
- Use when structuring
.tscnscenes, choosing how to break a feature into nodes, instancing aPackedScene(bullets, enemies, UI), or setting up autoload singletons. - Use when debugging
get_node()/$Pathreturningnull, or "Attempt to call on a previously freed instance".
When not to use: GDScript language/syntax → godot-gdscript; signal-based
decoupling → godot-signals-groups; physics bodies/collisions → godot-physics.
Core workflow
- Model with composition. A scene is a tree of nodes saved as
.tscn. Build small, single-purpose scenes (Player, Bullet, Enemy) and compose larger scenes from them. Favor adding child nodes over deep inheritance. - Make a scene reusable by giving its root a script and exposing
@exportconfig. Save it; it becomes aPackedSceneyou can instance many times. - Instance at runtime with
preload/load→scene.instantiate()→add_child(instance). Set position/state after adding (or before, both work). - Access nodes safely. Use
@onready var x = $Pathfor fixed children; use unique names (%Name) for nodes deep in the tree; never assume a node still exists. - Use autoloads for global state/services (game state, audio, scene switching) — registered in Project Settings > Globals (Autoload), accessible by name everywhere.
- Free nodes with
queue_free()and guard later access withis_instance_valid(). - Check references before you finish. After creating, moving or renaming scenes,
resources or scripts, run
python scripts/check_godot_project.py <project-dir>(no Godot install needed;--jsonfor machine-readable output). Fix everyERROR(missing or wrong-caseres://paths, undeclaredExtResource/SubResourceids, badparent=paths, missing main scene or autoload) and re-run until it exits 0. Warnings such as a stalepath=that Godot still finds by uid are safe but worth fixing. Run the script path relative to this skill directory, or resolve the installed skill path first. It needs Python 3.10+ and nothing else, and it does not replace opening the project in Godot.
Patterns
1. Instance a scene at runtime
extends Node2D
const BULLET := preload("res://bullet.tscn") # preload: loaded at compile time
func shoot(at: Vector2, dir: Vector2) -> void:
var bullet := BULLET.instantiate() # create an instance of the scene
bullet.global_position = at
bullet.direction = dir # set exported/public state
add_child(bullet) # now it's in the tree and runs
2. Safe node access: $, get_node_or_null, and unique names
@onready var label: Label = $UI/Label # $ is sugar for get_node("UI/Label")
@onready var health_bar: ProgressBar = %HealthBar # % = scene-unique name (rename-proof)
func update() -> void:
var optional := get_node_or_null("Maybe/Missing") # returns null instead of erroring
if optional:
optional.queue_free()
3. An autoload singleton (global game state)
# game_state.gd — add in Project Settings > Globals > Autoload as "GameState".
extends Node
var score := 0
signal score_changed(value: int)
func add_score(points: int) -> void:
score += points
score_changed.emit(score) # any scene can: GameState.score_changed.connect(...)
4. Change the running scene
func go_to_level_2() -> void:
# Swaps the current scene for another. Frees the old scene tree.
get_tree().change_scene_to_file("res://levels/level_2.tscn")
# Or, with a preloaded PackedScene:
# get_tree().change_scene_to_packed(LEVEL_2)
Pitfalls
$Path/get_node()returnnullor error when the path is wrong or the node isn't in the tree yet. Use@onready, verify the path matches the scene, or useget_node_or_null()for optional nodes.- Renaming a node breaks
$Path. Use unique names (%Name, set via right-clickAccess as Unique Name) so deep references survive renames and reparenting.
free()mid-frame can crash other code still using the node. Preferqueue_free()(deletes at end of frame) and checkis_instance_valid(node).- **Setting child state before `add
常见问题
Godot 中的场景是什么?
场景是保存下来的节点树,可以复用,也可以放置在其他场景中。
什么是自动加载?
自动加载是在整个游戏过程中保持加载的全局对象,例如共享的设置容器。
它涵盖物理吗?
涵盖得不深入。物理刚体和碰撞由另一份 Godot 技能负责。
详情
作者的原始描述
Structure a Godot 4.7 project with the scene tree and node composition: build reusable scenes, instance PackedScenes at runtime, navigate the tree safely, and register autoload singletons. Use when designing .tscn scenes, deciding how to split nodes, spawning instances with instantiate(), wiring autoloads, or fixing "node not found"/freed-node errors in a Godot project.
热度
- GitHub 星标
- 6.6k
- 排名
- #13825
- 许可证
- Apache-2.0
- 更新时间
- 2026/10/28
评论
- …