跳到正文
Skillpedia
通用编程6.6k · #13823

godot-gdscript

它能做什么

该技能教智能体如何为 Godot 4 编写正确的 GDScript。GDScript 是 Godot 内置的脚本语言。适合用 Godot 制作游戏的人。

适用场景

  • ✓当你在 Godot 项目中编写或修复 GDScript 文件时。
  • ✓当你想为 Godot 脚本添加导出设置、信号连接或 await 调用时。
  • ✓当你把旧的 Godot 3 脚本迁移到 Godot 4,而它已无法运行时。

技能链接

https://github.com/gamedev-skills/awesome-gamedev-agent-skills/blob/main/skills/godot/godot-gdscript/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/gamedev-skills/awesome-gamedev-agent-skills/blob/main/skills/godot/godot-gdscript/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“我的 Godot 脚本在更新到 Godot 4 之后出现错误,你能帮我修复吗?”

每周 5 个新技能,1 分钟读完。

工作原理

该技能讲解如何为变量和函数指定类型,使用内置的节点生命周期函数,以及使用信号和 await,使智能体编写的代码符合 Godot 的预期。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Godot GDScript (4.x)

Write correct, statically typed GDScript and use the node lifecycle and signal system the way the engine intends. Targets Godot 4.7 (GDScript 2.0).

When to use

  • Use when writing or fixing .gd files: declaring variables, functions, classes, using @export/@onready, connecting signals, or awaiting coroutines/signals.
  • Use when porting Godot 3.x scripts to 4.x and the script no longer parses.

When not to use: scene/node structure and instancing questions → godot-nodes-scenes; signal architecture/decoupling patterns → godot-signals-groups; using C# instead of GDScript → godot-csharp.

Core workflow

  1. Type everything you can. GDScript 2.0 supports static types (var hp: int = 10, func add(a: int, b: int) -> int:). Types catch errors at parse time and speed up the VM. Use := for inferred types.
  2. Use the lifecycle callbacks for their purpose: _ready() once when the node and its children enter the tree; _process(delta) every rendered frame; _physics_process(delta) on the fixed physics tick (use it for movement/physics).
  3. Grab node references with @onready, not in _init() — children do not exist until the node enters the tree.
  4. Expose tunables with @export so designers edit them in the Inspector.
  5. React to events with signals + await, not polling, where it reads cleanly.
  6. Run and read errors. The Debugger panel prints typed errors with line numbers; fix the first error first (later ones are often cascades).

Patterns

1. A typed script with lifecycle, @export, and @onready

extends Node2D
class_name Spinner            # registers a global type usable in other scripts

@export var speed: float = 90.0          # editable in the Inspector (degrees/sec)
@export_range(0, 10, 0.5) var wobble := 2.0
@onready var sprite: Sprite2D = $Sprite2D # resolved when the node enters the tree

func _ready() -> void:
    # Runs once, after children are ready. Safe to touch $Sprite2D here.
    sprite.modulate = Color.AQUA

func _process(delta: float) -> void:
    # delta is seconds since last frame; multiply rates by it for FPS independence.
    rotation_degrees += speed * delta

2. Signals: declare, emit, connect (4.x Callable syntax)

extends Node

signal health_changed(current: int, maximum: int)   # typed signal params

var health := 100

func take_damage(amount: int) -> void:
    health = maxi(health - amount, 0)    # maxi/maxf are typed; max() returns Variant
    health_changed.emit(health, 100)     # 4.x: emit as a method on the signal

func _ready() -> void:
    # 4.x: connect with a Callable, not a string method name.
    health_changed.connect(_on_health_changed)

func _on_health_changed(current: int, maximum: int) -> void:
    print("HP: %d/%d" % [current, maximum])

3. await — pause until a timer or signal fires (replaces 3.x yield)

func flash_then_continue() -> void:
    modulate = Color.RED
    await get_tree().create_timer(0.2).timeout   # resume after 0.2s
    modulate = Color.WHITE
    # await any signal: var result = await some_node.some_signal

4. Lambdas, typed arrays, and safe access

var enemies: Array[Node] = []                    # typed array

func cull_dead() -> void:
    enemies = enemies.filter(func(e): return e.is_inside_tree())

func get_first_name(d: Dictionary) -> String:
    return d.get("name", "unknown")              # default avoids missing-key errors

Pitfalls

  • 3.x → 4.x signal API changed. emit_signal("x") still works but prefer x.emit(...); connect("x", self, "_on_x") is gone — use x.connect(_on_x) with a Callable. yield(obj, "sig") is now await obj.sig.
  • export var is now @export var (annotation). Likewise onready→@onready, tool→@tool, remote/master RPC keywords → the @rpc(...) annotation.
  • @onready and $NodePath in _init() fail — the node isn't in the tree yet. Initialize node references in _ready() or with @onready.
  • Integer division truncates. 5 / 2 == 2. Use 5.0 / 2 or cast to float.
  • _process vs _physics_process. Put move_and_slide() and physics in _physics_process(delta); using _process makes motion frame-rate dependent.
  • class_name must be unique project-wide and is required to use the type name in other scripts or as an Inspector type.
  • **An override stops parsing after upgrading to
常见问题

什么是 GDScript?

GDScript 是 Godot 内置的脚本语言,其写法与 Python 相似。

它能帮助处理旧的 Godot 3 项目吗?

可以。该技能涵盖从 Godot 3 到 Godot 4 的变化,例如 yield 变为 await,export 变为 @export。

这个技能适用于 C# 代码吗?

不适用。Godot 中的 C# 代码由另一份技能涵盖。

详情

作者的原始描述

Write idiomatic GDScript for Godot 4.7: static typing, the node lifecycle (_ready/_process/_physics_process), @export/@onready/@tool annotations, signals, and await for asynchronous flow. Use when editing .gd scripts in a Godot project (project.godot), writing or debugging GDScript, or porting 3.x GDScript to 4.x (function signatures, yield to await, export to @export).

repo: gamedev-skills/awesome-gamedev-agent-skills

热度

GitHub 星标
6.6k
排名
#13823
许可证
Apache-2.0
更新时间
2026/10/28

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能