model/godot + data/tres + codecs/text/GodotVariant: Godot text scenes (.tscn / .escn) read + write against Godot 4's loader, .tres resources as data

docs/STYLE.md: the layout block lists codecs/text/GodotVariant (DOCS lint).
This commit is contained in:
4DBug committed 2026-11-01 19:25:15 -06:00
1 parent 9c02ebdee5
commit 8f26e32a71
24 files changed
+3649 -5

No files matched your search

+24
View File
@@ -306,6 +306,29 @@ local xml = assert(F.saveString(p, { format = "plist" }))
assert(string.find(xml, "<key>CFBundleName</key>\n\t<string>Demo</string>", 1, true))
```
## Godot resources (.tres)
Godot text resources load as a table: `type` (the resource class), `format`, `uid`, `scriptClass`, `external` (the
`ext_resource` list: `id`, `type`, `path`, `uid`), `subresources` (`id`, `type`, `properties`) and `properties` (the
`[resource]` section, in file order). Godot values that Luau lacks are `{ type, args }` tables: `Vector3(1, 2, 3)` is
`{ type = "Vector3", args = { 1, 2, 3 } }`, likewise `Color`, `Transform3D`, `PackedFloat32Array`, `NodePath`,
`&"StringName"` (`type = "StringName"`), `SubResource("id")` and `ExtResource("id")`; `Object(Class, ...)` has `class`
and its properties in `args[1]`. Godot dictionaries keep their order and may have number keys. Godot tells integers
from floats, so integral floats read from a file (`1.0`) are written back as floats.
`F.format("tres")` and the codec `F._internal("codecs/text/GodotVariant")` (`value`, `format`, `isCtor`, `ctor`)
help with the values. Scenes (`.tscn`) are models: see [the scene guide](scene.md).
```luau
local res = assert(F.data.load(tresText))
assert(res.type == "StandardMaterial3D" and res.external[1].path == "res://brick.png")
local c = res.properties.albedo_color
assert(c.type == "Color" and c.args[1] == 0.8)
assert(res.properties.albedo_texture.type == "ExtResource")
res.properties.metallic = 0.5
local text = assert(F.saveString(res, { format = "tres" }))
assert(string.find(text, "metallic = 0.5", 1, true) and string.find(text, "roughness = 1.0", 1, true))
```
## Save and convert
Data values have no `kind`, so saving needs `opts.format` or a destination extension (an `XmlNode` document too):
@@ -341,6 +364,7 @@ is an error there), CSV needs rows, INI needs sections of scalars, JSON has no b
| cbor (`.cbor`) | yes | yes | values, buffers, Dates, tagged values |
| plist (`.plist`) | XML + binary | XML | values, buffers, Dates |
| nbt (`.nbt`, `.dat`, `.mcstructure`), snbt (`.snbt`) | yes | yes | typed values: see [Minecraft and voxels](voxel.md) |
| tres (`.tres`) | yes | yes | Godot resources: sections, Variant values as `{ type, args }` |
Full capability list: [FORMATS.md](../FORMATS.md).