GitHub - godot-dlang/godot-dlang: D language bindings for the Godot Engine's GDExtension API (original) (raw)

DLang Logo

Godot-DLang

dub git dub rating dub rating

D language bindings for the Godot Engine'sGDExtension API.

Originally a fork of godot-d

WIP: These bindings are still under development. Until v1.0.0, expect breaking changes, bugs, and missing documentation. Please report any issues and confusing or undocumented features on the GitHub page.

Usage

Dependencies

Before you start please keep in mind that this is purely experimental unstable volatile WIP project intended for those brave people who would like to try D and Godot.

In no situation do not use it to ship your product, doing so of course is possible but by no means the author is responsible for the consequences.

Install godot-dlang using dub

This will download and cache dub package

Proceed to Manually creating project for adding it to your D project.

Manually building (advanced)

Normaly one would use dub package, this section is for advanced users who would like to develop or hack godot-dlang.

Note that if you have strange errors in dub run you might have godot-dlang cached in dub, you might need to remove it by using dub remove godot-dlang

Generating Godot Bindings

This step is one time process, though you would need to re-generate API and bindings for every godot release

Manually creating project

Creating Godot project

Creating dub project

dub.json:

{ "authors": [ "Godot-DLang" ], "targetType": "dynamicLibrary", "dflags-windows-ldc": ["-dllimport=defaultLibsOnly"], "copyright": "Copyright © 2022, Godot-DLang", "dependencies": { "godot-dlang": "~master", }, "description": "A minimal D application.", "license": "proprietary", "name": "mydplugin" }

Creating your first D script

source/greeter.d:

import godot; // import godot.api.script; // for GodotScript! // import godot.api.register; // for GodotNativeLibrary // import godot.string; // for gs!

import godot.node;

// minimal class example with _ready method that will be invoked on creation class Greeter : GodotScript!Node { @Property String name;

// this method is a special godot entry point when object is added to the scene
@Method
void _ready() {
    // 'gs' is a string wrapper that converts D string to godot string
    // usually there is helper functions that takes regular D strings and do this for you
    print(gs!"Hello ", name);
}

}

// register classes, initialize and terminate D runtime, only one per plugin mixin GodotNativeLibrary!( // this is a name prefix of the plugin to be acessible inside godot // it must match the prefix in .gdextension file: // entry_symbol = "mydplugin_gdextension_entry" "mydplugin",

// here goes the list of classes you would like to expose in godot
Greeter,

);

You would need to build your plugin every time you have modified the code in order to see changes

Currently AFAIK there is no way to unload/reload GDExtension, because of that on Windows it will prevent rebuilding plugin until you close the editor!

Register GDExtension

mydplugin.gdextension:

[configuration]

entry_symbol = "mydplugin_gdextension_entry"
compatibility_minimum = "4.1"

[libraries]

linux.64 = "libmydplugin.so"
windows.64 = "mydplugin.dll"

Note that entry_symbol should match class registration in D inside of GodotNativeLibrary declaration

Use D scripts in godot

hellod

Creating project by using init script

Run command dub run godot-dlang:init to initialize new project in current folder. This script will walk you through standard dub project set up and will create dub config, library entrypoint and gdextension plugin.

It is important that you use this script after creating godot project since you can't create godot project in non-empty directory.

Arguments:

Example:

Initialize project in current directory:

dub run godot-dlang:init

Initialize project in custom directory:

dub run godot-dlang:init -- --path path/to/folder

Initialize project with custom godot-dlang path:

dub run godot-dlang:init -- --custom path/to/godot/dlang

Initialize project with custom godot-dlang path in custom directory:

dub run godot-dlang:init -- -p folder/ -c godot-dlang/

After running this script you'll have theese new files in selected folder:

├── dub.json/sdl # - Your dub config ├── projectname.gdextension # - GDExtension plugin └── source/ └── lib.d # - Library entrypoint that contains "mixin GodotNativeLibrary!"

Extend as you wish!

Enjoy your new game!

Godot API

Godot's full script API can be used from D:

Versioning

The GDExtension API should be binary-compatible between Godot minor versions as in SemVer convention, so a D library built for Godot v4.0.0 should work with any v4.0.x versions but not guaranteed to work with v4.1.0 or later.

D bindings must be generated for your target Godot minor release version (godot.exe --dump-extension-api).

Extension version properties can be checked to prevent newer functions from being called with older Godot binaries. For example:

import godot.apiinfo; // contains version information about bindings

if(VERSION_MINOR > 0) useNewGodotFunctions(); else doNothing();

License

MIT - https://opensource.org/licenses/MIT