Browse the documentation

GDScript

Typhon compiles Godot's GDScript as well as Python: a second front end parses .gd files into the same syntax tree, and inference, C# generation and Roslyn are shared.

Typhon compiles GDScript, the scripting language of the Godot engine, as well as Python. A GDScript lexer and parser turn a .gd file into the same syntax tree that a .ty file produces, and from there the file goes through the same type inference, C# generation and Roslyn. Where GDScript’s semantics differ from Python’s, the C# generation follows GDScript, and Python modules never see those rules. The file extension selects the front end.

Running GDScript

typhon run tests/gdscript/cases/basics.gd
typhon emit tests/gdscript/cases/classes.gd

typhon run runs a SceneTree script that prints, as godot --headless -s basics.gd would, and typhon emit shows the C#. In a Godot C# project, the .gd files to compile are listed as TyphonCompile items, and TyphonEmitToDisk makes Typhon write their C# to disk before the build, so that Godot’s own source generators see the script classes:

<TyphonEmitToDisk>true</TyphonEmitToDisk>
<TyphonCompile Include="sim/boids.gd" />

A project’s other GDScript scripts stay GDScript. The sample Godot compiles one script this way and compares it with the same script run by Godot.

From GDScript to C#

  • Classes. A file is a class, named by class_name or after the file, deriving from what it extends. Member vars become properties, initialized before the body of _init, as in GDScript, and @onready vars are assigned at the start of _ready. Every member is public.
  • Overrides. A method that overrides a virtual of the native base by its GDScript name gets Godot’s C# name and signature: func _process(delta) becomes public override void _Process(double delta).
  • Godot’s API. Members of Godot’s types are found by their GDScript names: position becomes Position, Vector2.ZERO becomes Vector2.Zero and Color.RED becomes Colors.Red. A member that the static type lacks is looked up at run time, as GDScript does for such accesses.
  • Floats. GDScript floats are 64-bit, and Godot’s math types use 32-bit floats, so the compiler converts where C# needs it, as in Vector2(x, y) or v * delta.
  • Engine functions. With Godot referenced, print, the random functions and load call Godot’s, so a compiled script shares the engine’s output and random generator. Without Godot, values print as Godot prints them, and the random functions implement Godot’s PCG32 generator.

GDScript’s semantics

The rules that differ from Python’s follow GDScript: int / int truncates, % is the remainder of that division, ** is left-associative, comparisons do not chain, ints wrap around on overflow, and String % [args] formats like Godot’s String::sprintf. Values print as Godot prints them, for example true, <null> and { "k": 1 }.

The supported language covers classes with inner classes, enums, signals between compiled scripts, match with array and dictionary patterns, one-line lambdas, $Node/Path, property accessors, and the common methods of Array, Dictionary and String. Typhon’s documentation lists everything in detail.

Not supported yet

  • await and coroutines.
  • Godot signals as the engine knows them, such as connections to timer.timeout or from the editor.
  • Multi-line lambdas.
  • extends "res://path.gd", preload("x.gd") as a class, and references between .gd files by class_name.
  • Hints of @export and @rpc.
  • Copy-on-assignment of packed arrays, which are lists shared like Arrays.

Untyped GDScript compiles, but where inference cannot type a value, it is late-bound and slow.

Tests

Godot is the reference for GDScript, as CPython is for Python. The scripts in tests/gdscript/cases print values, and tests/gdscript/expected holds what Godot 4.7.2 printed for them. A test compares the output of typhon run with these files, and it does not need Godot.