Description
Changelog
Reviews (0)

This addon includes a fully compliant TOML v1.1.0 lexer and parser for Godot 4.5+ written in GDScript. They pass the complete official TOML test suite v2.2.0.

This addon makes Godot recognise TOML files as Resources, and enables you to parse them as Dictionary[Variant, Variant]. It can map all TOML values to their Godot counterpart (The addon provides a helper class for DateTime values).

Example

Loading the official TOML example:

# This is a TOML document.

title = "TOML Example"

[owner]
name = "Tom Preston-Werner"
dob = 1979-05-27T07:32:00-08:00 # First class dates

[database]
server = "192.168.1.1"
ports = [ 8000, 8001, 8002 ]
connection_max = 5000
enabled = true

[servers]

  # Indentation (tabs and/or spaces) is allowed but not required
  [servers.alpha]
  ip = "10.0.0.1"
  dc = "eqdc10"

  [servers.beta]
  ip = "10.0.0.2"
  dc = "eqdc10"

[clients]
data = [ ["gamma", "delta"], [1, 2] ]

# Line breaks are OK when inside arrays
hosts = [
  "alpha",
  "omega"
]

In GDScript:

var toml_file: TOML = load("res://official_example.toml")
print(toml_file.data)

# {
#   &"title": "TOML Example", &"owner": { &"name": "Tom Preston-Werner", &"dob": 1979-05-27T07:32:00-08:00 },
#   &"database": { &"server": "192.168.1.1", &"ports": [8000, 8001, 8002], &"connection_max": 5000, &"enabled": true },
#   &"servers": { &"alpha": { &"ip": "10.0.0.1", &"dc": "eqdc10" }, &"beta": { &"ip": "10.0.0.2", &"dc": "eqdc10" } },
#   &"clients": { &"data": [["gamma", "delta"], [1, 2]],
#   &"hosts": ["alpha", "omega"] }
# }

You can also parse strings directly:

var toml_string: String = FileAccess.get_file_as_string("res://official_example.toml")
var dict: Variant = TOML.parse_string(toml_string) # Returns null on error

if dict != null:
    print(dict as Dictionary[Variant, Variant])

TOML.DateTime helper class

The addon also provides a helper class to work with TOML DateTime values. It supports nanosecond precision and conversion to RFC3339 (TOML valid format) and ISO8601 (Godot's Time class methods format) string formats.

The class holds the year, month, day, hours, minutes, seconds, milliseconds/nanoseconds, offset_sign, offset_hours and offset_minutes data of a given DateTime value. The default String representation (through the str() method) is in the RFC3339 format, but you can use the to_iso8601() method to get a ISO8601 format String, compatible with the methods from Godot's Time class.

Changelog for version v1.0.2

No changelog provided for this version.

Reviews

Godot TOML has no reviews yet.

Login to write a review.