Skip to content

godot-nim/gdext-nim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

godot-nim/gdext

Nim for GDExtension. A pure library and a CLI tool.

Documentation | Coding Guide | Tutorial | Examples | Templates | Forum | Source


Quick start

nimble install gdext
mkdir testproject && cd $_
touch project.godot
gdextwiz new-extension MyExtension
gdextwiz run

Features

  • All classes and methods provided by the engine are available from Nim.
  • New extension classes can be defined that inherit from the engine class and exposed to editors and GDScript.
  • New methods, signals, properties, and enums can be defined in your extension classes and exposed.
  • New virtual functions can be defined and exposed, and overridden from both Nim and GDScript.
  • Recompiled GDExtension can be loaded without restarting the editor. (Hot Reloading)
  • Arithmetic operations such as Vector are more expressive than those in Godot, for example, GLSLang's swizzle operation is also available.
  • It has a CLI tool that allows you to create new extensions, compile, run projects, and so on from a unified interface. (see wiki - gdextwiz)
  • Generate class references from comments and annotations left in the code.
  • Web platform support via Emscripten.

Limitation

  • Editor plug-ins cannot be created using only pure Nim code.
  • Due to engine specifications, Nim code works in the form of calls from the engine; it is not possible to run Nim stand-alone.

vs. GDScript

Nim and GDScript have very similar syntax. Porting is relatively easy.

# Nim

import gdext
import gdext/classes/[gdSprite2D, gdInput]

type MySprite2D* {.gdsync.} = ptr object of Sprite2D
  speed: float = 400
  angular_speed: float = PI


method process(self: MySprite2D; delta: float64) {.gdsync.} =
  var direction = 0
  if Input.is_action_pressed("ui_left"):
    direction = -1
  if Input.is_action_pressed("ui_right"):
    direction = 1

  self.rotation = self.rotation + self.angular_speed * direction * delta

  var velocity: Vector2
  if Input.is_action_pressed("ui_up"):
    velocity = Vector2.Up.rotated(self.rotation) * self.speed

  self.position = self.position + velocity * delta
# GDScript

extends Sprite2D

var speed = 400
var angular_speed = PI


func _process(delta):
	var direction = 0
	if Input.is_action_pressed("ui_left"):
		direction = -1
	if Input.is_action_pressed("ui_right"):
		direction = 1

	rotation += angular_speed * direction * delta

	var velocity = Vector2.ZERO
	if Input.is_action_pressed("ui_up"):
		velocity = Vector2.UP.rotated(rotation) * speed

	position += velocity * delta

gif

Guntur Sarwohadi (@guntur-ctech) reports that setting better build options for a simple port will give approximately six times the performance.

https://github.com/guntur-ctech/simulation-performance-comparison

Commands

install

nimble install gdext

uninstall

nimble uninstall gdext

Supported environments

OS

  • Linux
  • Macos
  • Windows

Engine

Nim compiler

  • nim-lang/nim 2.0.12 or higher

    Macos

    Use Nim installed via Homebrew, not choosenim. Due to the execution environment of Godot itself, a native AArch64/ARM64 build of Nim is required, and that choosenim installs a x86_64 build emulated via Rosetta.

Tested by author

Note

Support for environments not listed here is based on your reports. Please consider contributing.

  • OS: Linux (Arch)
  • Engine: Godot 4.3.stable.arch_linux
  • Nim: 2.0.12, 2.0.14, 2.2.0, 2.2.2
  • CC: gcc version 14.2.1 20240910 (GCC)