Godot .tscn 场景文件格式规范

SkillFiles & storage

Godot 4.x .tscn 场景文件纯文本格式规范与 AI 操作指南 — 文件结构、资源引用、 Transform 变换、属性值格式、信号连接,以及 AI 直接读写 .tscn 文件的操作方法。

Available today. Use it from your connected AI after setup.

Connect ahel once, and every AI you use reads what you have installed.

Then ask your AI: use the Godot .tscn 场景文件格式规范 skill

About this capability

Plain-text format specification and AI operation guide for Godot 4.x .tscn scene files — file structure, resource references, Transform transforms, property value formats, signal connections, and how AI can directly read and write .tscn files.

What this skill tells your AI

The instructions your AI receives, as published by 925236118/alphaagent in .claude/skills/godot-tscn-format/SKILL.md and read by ahel’s review.

用途:让 AI 能够直接读写 Godot 场景文件 版本:v1.0 · 2026-04-24 适用引擎:Godot 4.x


核心原则

Godot 场景文件是纯文本,AI 可以直接通过 write_to_filereplace_in_file 操作,无需任何中间层。


一、文件结构总览

[gd_scene load_steps=N format=3 uid="uid://xxx"]    ← 头部(必需)

[ext_resource type="Type" path="res://..." id="ID"]  ← 外部资源引用(可选)
[ext_resource ...]

[sub_resource type="Type" id="ID"]                   ← 内嵌资源定义(可选)
property = value
[sub_resource ...]

[node name="Name" type="Type"]                       ← 根节点(必需)
property = value

[node name="Child" type="Type" parent="."]           ← 子节点
property = value

[connection signal="sig" from="Node" to="Target" method="func"]  ← 信号连接(可选)

二、头部声明

2.1 基本格式

[gd_scene load_steps=4 format=3]
字段说明
load_steps资源加载步数 = ext_resource 数 + sub_resource 数 + 1(场景本身)
format固定为 3(Godot 4.x)
uid可选,Godot 自动生成的唯一 ID,创建时可省略

2.2 计算 load_steps

load_steps = len(ext_resources) + len(sub_resources) + 1

示例:2 个外部资源 + 3 个内嵌资源 → load_steps=6


三、外部资源引用 (ext_resource)

3.1 格式

[ext_resource type="Type" path="res://path/to/file" id="ID"]

3.2 常见类型

type文件类型示例
ScriptGDScriptpath="res://scripts/player.gd"
PackedScene场景path="res://scenes/enemy.tscn"
Material材质资源path="res://materials/floor.tres"
Texture2D2D 纹理path="res://textures/icon.png"
AudioStream音频path="res://audio/shoot.wav"
Shader着色器path="res://shaders/outline.gdshader"

3.3 ID 命名规范

AI 生成时使用有意义的 ID:

[ext_resource type="Script" path="res://scripts/player.gd" id="1_player_script"]
[ext_resource type="Material" path="res://resources/floor_mat.tres" id="2_floor_mat"]
[ext_resource type="PackedScene" path="res://scenes/bullet.tscn" id="3_bullet_scene"]

四、内嵌资源定义 (sub_resource)

4.1 格式

[sub_resource type="Type" id="ID"]
property1 = value1
property2 = value2

4.2 常用碰撞形状

# 胶囊体(角色常用)
[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_player"]
radius = 0.35
height = 1.8

# 球体
[sub_resource type="SphereShape3D" id="SphereShape3D_001"]
radius = 0.5

# 盒子
[sub_resource type="BoxShape3D" id="BoxShape3D_floor"]
size = Vector3(10, 0.2, 10)

# 圆柱体
[sub_resource type="CylinderShape3D" id="CylinderShape3D_001"]
height = 2.0
radius = 0.5

# 2D 形状
[sub_resource type="RectangleShape2D" id="RectangleShape2D_001"]
size = Vector2(32, 32)

[sub_resource type="CircleShape2D" id="CircleShape2D_001"]
radius = 16.0

4.3 常用网格

# 盒子网格
[sub_resource type="BoxMesh" id="BoxMesh_001"]
size = Vector3(1, 1, 1)

# 球体网格
[sub_resource type="SphereMesh" id="SphereMesh_001"]
radius = 0.5
height = 1.0

# 胶囊网格
[sub_resource type="CapsuleMesh" id="CapsuleMesh_001"]
radius = 0.5
height = 2.0

# 圆柱网格
[sub_resource type="CylinderMesh" id="CylinderMesh_001"]
top_radius = 0.5
bottom_radius = 0.5
height = 2.0

# 平面网格
[sub_resource type="PlaneMesh" id="PlaneMesh_001"]
size = Vector2(10, 10)

4.4 材质(内嵌)

[sub_resource type="StandardMaterial3D" id="Material_red"]
albedo_color = Color(1, 0, 0, 1)
roughness = 0.8
metallic = 0.2

# 发光材质
[sub_resource type="StandardMaterial3D" id="Material_glow"]
emission_enabled = true
emission = Color(1, 0.8, 0, 1)
emission_energy_multiplier = 2.0

五、节点声明 (node)

5.1 根节点

[node name="Player" type="CharacterBody3D"]
script = ExtResource("1_player_script")

5.2 子节点

[node name="ChildName" type="NodeType" parent="."]
property = value
parent 值含义
.直接子节点(父节点是根)
ParentName指定父节点名
Parent/Child嵌套路径

5.3 引用资源

# 引用外部资源
script = ExtResource("1_player_script")
mesh = ExtResource("2_mesh")

# 引用内嵌资源
shape = SubResource("CapsuleShape3D_player")
mesh = SubResource("BoxMesh_001")

5.4 实例化场景

[node name="Enemy1" parent="Enemies" instance=ExtResource("4_enemy_scene")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 5, 0, -10)

六、Transform 变换

6.1 Transform3D 格式

transform = Transform3D(bx.x, bx.y, bx.z, by.x, by.y, by.z, bz.x, bz.y, bz.z, ox, oy, oz)
参数含义
bx (1-3)X 轴基向量
by (4-6)Y 轴基向量
bz (7-9)Z 轴基向量
o (10-12)位置 (origin)

6.2 常用变换

# 单位变换(默认位置)
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0)

# 仅位置
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 5, 2, -10)

# 位置 + 缩放 0.5
transform = Transform3D(0.5, 0, 0, 0, 0.5, 0, 0, 0, 0.5, 5, 2, -10)

# 绕 Y 轴旋转 90°(sin90=1, cos90=0)
transform = Transform3D(0, 0, 1, 0, 1, 0, -1, 0, 0, 0, 0, 0)

6.3 Transform2D 格式

transform = Transform2D(cos, sin, -sin, cos, x, y)

# 单位变换
transform = Transform2D(1, 0, 0, 1, 100, 200)

# 旋转 45° 位于 (100, 200)
transform = Transform2D(0.707, 0.707, -0.707, 0.707, 100, 200)

七、常用属性值格式

7.1 基础类型

# 布尔
visible = true
enabled = false

# 整数
health = 100

# 浮点
speed = 10.5

# 字符串
name = "Player"

7.2 向量

# Vector2
position = Vector2(100, 200)
size = Vector2(32, 32)

# Vector3
position = Vector3(0, 1.5, 0)
target_position = Vector3(0, 0, -100)

# Vector4
custom_data = Vector4(1, 2, 3, 4)

7.3 颜色

# Color(R, G, B, A) 范围 0.0-1.0
light_color = Color(1, 0.85, 0.3, 1)
albedo_color = Color(0.25, 0.25, 0.3, 1)

# 半透明
modulate = Color(1, 1, 1, 0.5)

7.4 数组

# PackedStringArray
config/features = PackedStringArray("4.6", "Forward Plus")

# PackedVector2Array
polygon = PackedVector2Array(0, 0, 100, 0, 100, 100, 0, 100)

# 普通数组
groups = ["enemy", "damageable"]

八、信号连接 (connection)

8.1 格式

[connection signal="signal_name" from="NodePath" to="TargetPath" method="method_name"]

8.2 示例

# 同级节点连接
[connection signal="timeout" from="ShootCooldown" to="." method="_on_shoot_cooldown_timeout"]

# 子节点连接到根
[connection signal="body_entered" from="HitArea" to="." method="_on_hit_area_body_entered"]

# 带 flags
[connection signal="pressed" from="Button" to="." method="_on_button_pressed" flags=1]

九、完整示例模板

9.1 3D 角色场景

[gd_scene load_steps=3 format=3]

[ext_resource type="Script" path="res://scripts/player.gd" id="1_script"]

[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_001"]
radius = 0.35
height = 1.8

[node name="Player" type="CharacterBody3D"]
script = ExtResource("1_script")

[node name="CollisionShape3D" type="CollisionShape3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.9, 0)
shape = SubResource("CapsuleShape3D_001")

[node name="Camera3D" type="Camera3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.6, 0)
fov = 75.0

[node name="MeshInstance3D" type="MeshInstance3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.9, 0)

9.2 2D 角色场景

[gd_scene load_steps=3 format=3]

[ext_resource type="Script" path="res://scripts/player_2d.gd" id="1_script"]
[ext_resource type="Texture2D" path="res://sprites/player.png" id="2_texture"]

[sub_resource type="RectangleShape2D" id="RectangleShape2D_001"]
size = Vector2(32, 48)

[node name="Player" type="CharacterBody2D"]
script = ExtResource("1_script")

[node name="Sprite2D" type="Sprite2D" parent="."]
texture = ExtResource("2_texture")

[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource("RectangleShape2D_001")

9.3 UI 场景

[gd_scene load_steps=2 format=3]

[ext_resource type="Script" path="res://scripts/hud.gd" id="1_script"]

[node name="HUD" type="CanvasLayer"]
script = ExtResource("1_script")

[node name="MarginContainer" type="MarginContainer" parent="."]
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
offset_left = 20.0
offset_top = 20.0
offset_right = -20.0
offset_bottom = -20.0

[node name="VBoxContainer" type="VBoxContainer" parent="MarginContainer"]
layout_mode = 2

[node name="ScoreLabel" type="Label" parent="MarginContainer/VBoxContainer"]
layout_mode = 2
text = "Score: 0"

[node name="TimeLabel" type="Label" parent="MarginContainer/VBoxContainer"]
layout_mode = 2
text = "Time: 60"

十、AI 操作指南

10.1 创建新场景

# AI 直接使用 write_to_file
write_to_file("res://scenes/new_scene.tscn", """
[gd_scene load_steps=1 format=3]

[node name="Root" type="Node3D"]
""")

10.2 修改现有场景

# 使用 replace_in_file 修改属性
replace_in_file("res://scenes/player.tscn",
    old_str='fov = 75.0',
    new_str='fov = 90.0'
)

10.3 添加节点

# 在场景末尾添加节点(信号连接之前)
replace_in_file("res://scenes/player.tscn",
    old_str='[connection signal=',
    new_str='''[node name="NewChild" type="Node3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0)

[connection signal='''
)

10.4 常见错误排查

错误原因修复
load_steps 不匹配资源数计算错误重新计算 ext + sub + 1
id not found引用了不存在的资源 ID检查 ExtResource/SubResource ID
场景无法加载parent 路径错误检查节点层级关系
属性无效类型拼写错误查阅 Godot 文档确认属性名

十一、快速参考

常用节点类型

类别3D2D
基础Node3DNode2D
物理角色CharacterBody3DCharacterBody2D
刚体RigidBody3DRigidBody2D
静态体StaticBody3DStaticBody2D
碰撞形状CollisionShape3DCollisionShape2D
网格MeshInstance3D-
精灵-Sprite2D
相机Camera3DCamera2D
灯光DirectionalLight3D, OmniLight3D, SpotLight3DPointLight2D, DirectionalLight2D

常用 UI 节点

节点用途
ControlUI 基类
CanvasLayerUI 层
Label文本
Button按钮
TextureRect图片
ProgressBar进度条
VBoxContainer / HBoxContainer布局容器
MarginContainer边距容器

Signals

GitHub stars
103
Forks
14
Last commit
Jul 2026

Others that do the same job

Advanced
Catalog kind
skill
Gateway key
godot-tscn-format
Source
github.com/925236118/alphaagent