godot-signals-groups
它能做什么
该技能讲解如何让 Godot 游戏的各个部分通过信号互相通信,以及如何通过分组一次控制许多节点。适合希望项目结构更清晰的开发者。
适用场景
- ✓当游戏的一部分需要告诉其他部分某件事发生了,例如玩家死亡时。
- ✓当你希望一次性暂停或保存整组对象时。
- ✓当你在 Godot 项目中用信号替代场景之间的直接引用时。
技能链接
https://github.com/gamedev-skills/awesome-gamedev-agent-skills/blob/main/skills/godot/godot-signals-groups/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“我的玩家在 Godot 中死亡时,我希望计分界面和声音都能做出反应,而不需要直接把它们连接在一起。”
每周 5 个新技能,1 分钟读完。
工作原理
该技能讲解子对象如何向上发送信号,其他对象如何连接到它,以及分组如何让你用一次调用作用于许多节点。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Godot Signals & Groups (4.x)
Decouple nodes with the observer pattern (signals) and act on many nodes at once (groups), instead of hard-coding references between scenes. Targets Godot 4.7.
When to use
- Use when a node needs to tell others "something happened" (player died, item picked up, wave cleared) without holding direct references to them.
- Use when you need to address a whole category of nodes at once ("pause all enemies", "save every checkpoint").
When not to use: raw signal syntax basics → godot-gdscript; scene structure
and instancing → godot-nodes-scenes. For cross-scene global events, emit from an
autoload (see godot-nodes-scenes).
Core workflow
- Decide the direction. A child/sub-scene should emit a signal upward; the parent connects to it. This keeps the child reusable and ignorant of who listens.
- Declare typed signals on the emitter;
emit()them when the event occurs. - Connect with a Callable (
sig.connect(_on_sig)), optionally in the editor's Node dock. UseCONNECT_ONE_SHOTfor fire-once,bind()to pass extra context. - Use groups for broadcast: add nodes to a named group, then iterate
get_tree().get_nodes_in_group(...)orcall_group(...). - Disconnect when needed (e.g. before freeing a long-lived listener) and check
is_connected()to avoid duplicate connections.
Patterns
1. Emit upward, connect from the parent
# coin.gd (reusable pickup — knows nothing about the player or HUD)
extends Area2D
signal collected(value: int)
func _on_body_entered(body: Node) -> void:
if body.is_in_group("player"):
collected.emit(10)
queue_free()
# level.gd (the parent wires the coin to game state)
func _ready() -> void:
for coin in get_tree().get_nodes_in_group("coins"):
coin.collected.connect(_on_coin_collected)
func _on_coin_collected(value: int) -> void:
GameState.add_score(value)
2. Connect flags: one-shot and bind extra arguments
func _ready() -> void:
# Fire exactly once, then auto-disconnect.
$Door.opened.connect(_on_door_opened, CONNECT_ONE_SHOT)
# bind() appends arguments supplied at connect time (after the signal's own args).
$RedButton.pressed.connect(_on_button.bind("red"))
func _on_button(color: String) -> void:
print("Pressed the %s button" % color)
3. Groups: broadcast to many nodes
func pause_all_enemies() -> void:
# Call a method on every node in the "enemies" group (no-op if missing).
get_tree().call_group("enemies", "set_paused", true)
func count_enemies() -> int:
return get_tree().get_nodes_in_group("enemies").size()
Add a node to a group from code or via the editor's Node > Groups tab:
func _ready() -> void:
add_to_group("enemies") # remove_from_group("enemies") to leave
4. Await a signal inline
func open_chest() -> void:
$AnimationPlayer.play("open")
await $AnimationPlayer.animation_finished # pause until it emits
spawn_loot()
Pitfalls
- 3.x connect signature is gone.
connect("died", self, "_on_died")→died.connect(_on_died). The target is implied by the Callable. The legacyObject.connect("died", Callable(self, "_on_died"))works but the method-name string form does not. - Duplicate connections fire handlers multiple times. Connecting again in
_ready()after re-adding a node stacks callbacks. Guard withif not sig.is_connected(cb): sig.connect(cb). - Connecting to a freed node errors. Godot removes a connection when the emitter or the Callable's owner is freed, but a lambda belongs to the script that created it — one that captures a node freed later keeps firing and errors. Connect listener methods, or disconnect stored lambdas yourself.
- Groups are global to the SceneTree, not per-scene. Two levels using the same group name share membership. Namespace group names if that matters.
call_groupsilently ignores nodes lacking the method. Typos in the method name fail quietly — prefer a typed signal when the contract matters.- Signal args must match the handlers.
emit()does not validate arguments against thesignaldeclaration; a handler with a different argument count fails with "Method expected N argument(s)" at emit time. Declare typed params and emit exactly those.
References
- For connection flags, deferred c
常见问题
什么是信号?
信号是节点在某件事发生时发出的消息,其他节点可以对它作出响应。
什么是分组?
分组是一组有名称的节点,可以一起操作,例如所有敌人。
它会讲解 GDScript 的基础语法吗?
不会。基础语法由另一份 GDScript 技能负责。
详情
作者的原始描述
Build event-driven, decoupled Godot 4.7 gameplay with signals and node groups: declare and emit custom signals, connect with Callables (incl. bind/one-shot), and broadcast to many nodes via groups and call_group. Use when wiring node communication in a Godot project, replacing tight references with signals, emitting/connecting events, or porting 3.x connect("sig", self, "method") code.
热度
- GitHub 星标
- 6.6k
- 排名
- #13829
- 许可证
- Apache-2.0
- 更新时间
- 2026/10/28
评论
- …