Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Go language bindings for the Godot Engine's GDNative API.

License

NotificationsYou must be signed in to change notification settings

ShadowApex/godot-go

Repository files navigation

Build StatusGo Report CardGoDocMIT licensed

Godot-Go logo

Godot-Go

Go language bindings for theGodot Engine'sGDNative API.

NOTE: These bindings are currently still under development. Not all of the design,implementation, or documentation is final. Comments/suggestions are welcome (see GitHub issues).The API is subject to change.

Usage

The Godot Engine can interface with code written in Go using the GDNative module.It works by using Go's ability to compile code as a shared library (.so,.dylib,.dll),which the Godot Engine can load to call Go methods.

The godot-go library provides you with a method calledgodot.AutoRegister, which willallow you to expose a Go struct that follows thegodot.Class interface to the GodotEngine when it loads your shared library. It also provides bindings to all ofthe available Godot Engine classes and their methods, so you can call Godotfunctions from your Go code.

Setup

go get github.com/shadowapex/godot-go/godot

Build

When you're ready to build your code as a dynamic library that can be imported intoGodot, use the instructions below depending on your platform.

Linux

go build -v -buildmode=c-shared -o libgodot.so <your_go_library>.go

Mac OS X

go build -v -buildmode=c-shared -o libgodot.dylib <your_go_library>.go

Tutorial

To write a Go shared library that can be used in Godot, you'll first need to createa.go file that will act as the entrypoint to your library. This filemusthavepackage main as the package name along withmain() andinit() functionsdefined. Themain() function will be empty, but is required to compile itas a shared library:

package mainfuncinit() {}funcmain() {}

After setting this up, we can define a new struct that we want to be availablein Godot. In our struct, we can embed one of any available Godot class so it implementsthegodot.Class interface. Note that embedding multiple Godot structs is not supported.

// SimpleClass is a simple go struct that can be attached to a Godot Node2D object.typeSimpleClassstruct {godot.Node2D}

Once we have our struct defined, we can now attach method receivers to our struct.All methods attached to this struct will be callable from Godot, provided that theytake and/or return built-in or godot types. Let's go ahead and define a methodreceiver for theX_ready method, which Godot will call when our node entersthe scene.

// X_ready is called as soon as the node enters the scene.func (h*SimpleClass)X_ready() {godot.Log.Warning("Hello World!")}

Now that we have a struct and a method defined, we need to create a constructorfunction that will return a new instance of ourSimpleClass struct. This methodwill be called by Godot whenever it needs to create a new instance of your object.

// NewSimpleClass is a constructor that we can pass to godot.funcNewSimpleClass() godot.Class {return&SimpleClass{}}

Now that we have a constructor function, we can register the function withGodot, so it knows how to create a new instance of your struct and call itsmethods. We can register the class by calling thegodot.AutoRegister methodin ourinit() function, which is a special Go function that will be executedwhen our shared library is loaded, and passing our constructor function.

funcinit() {// AutoRegister will register the given class constructor with Godot.godot.AutoRegister(NewSimpleClass)}

Thegodot.AutoRegister function works by calling your constructor and inspectingyour Godot struct with reflection for all public receiver methods and structfields. It will then register the constructor and your struct's methods and fieldswith Godot through Godot's GDNative C API.

Now we can compile our project into a shared library:

Linux
go build -v -buildmode=c-shared -o libgodot.so <your_go_library>.go

Mac OS X
go build -v -buildmode=c-shared -o libgodot.dylib <your_go_library>.go

This will create a shared library object that you can use in Godot! To learn howto set up your library in Godot, refer to the section below.

How do I use native scripts from the editor?

First, copy your.so,.dylib, and/or.dll library that you compiled intoyour project folder.

Create a newGDNativeLibrary resource by clicking the new icon in the inspector.AGDNativeLibrary resource is a platform-agnostic abstraction of our native library.With it, it allows us to specify different shared library object files for differentplatforms, such as.so for Linux platforms,.dylib for Mac, and.dll for Windows.

SelectGDNativeLibrary from the list of resource types.

Select the folder icon next to the platform that you want to support in the inspector.For Linux platforms, you'll want to select a.so file,.dylib for Mac, and.dll forWindows. You can add each shared library file for each platform you want to support.

Select your shared library file from your project directory.

Click the save icon and then click "Save As.."


Save your GDNativeLibrary resource to your project directory. This file simplycontains the paths to the shared library objects for each platform you want tosupport.

Now create a new node that you want to attach your Go library to.


Click the add script icon to attach your Go library to the node.

Select "NativeScript" as the script language, and enter the name of the structthat you registered in your Go library that you would like to be attached to thisnode. You should also select "Built-in Script", so this setting is built in tothe scene.

With your node selected, you can now attach our GDNativeLibrary resource that wecreated earlier to this node.


Attributions

The Go gopher was designed byRenee French.

The logo used above was based on the image byTakuya Ueda.Licensed under the Creative Commons 3.0 Attributions license.Available unmodified from:https://github.com/golang-samples/gopher-vector

The logo used above was also based on the image byAndrea CalabróLicensed under the Creative Commons Attribution License version 3.0CC-BY 3.0

License

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

Links

GitHub repository -https://github.com/shadowapex/godot-go
GDNative repository -https://github.com/GodotNativeTools/godot_headers

Godot Engine -https://godotengine.org
Go programming language -https://golang.org

Releases

No releases published

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp