Engine Integration Patterns
graphics.gd
projects have a few different options for integrating with the engine.
You can ignore the object-oriented nature of the engine entirely and use the startup.Rendering
function
to wait for rendering to be available, then you can iterate over each frame and use the RenderingServer
directly. Choose this option when porting an existing Go application to graphics.gd
and when you will be
porting existing rendering code to use the RenderingServer
.
package main
import "graphics.gd/startup"
func main() { frames := startup.Rendering() // engine has started, rendering is available for frame := range frames { // do something each frame }}
MainLoop
Section titled “MainLoop”Alternatively, implement the MainLoop.Interface
and register this with the Engine to replace the default
SceneTree
.
package main
import ( "graphics.gd/classdb/MainLoop" "graphics.gd/startup" "graphics.gd/variant/Float")
type MyMainLoop struct{ MainLoop.Implementation}
func (m *MyMainLoop) Initialize() {} // do something when the engine startsfunc (m *MyMainLoop) Process(delta Float.X) {} // do something each framefunc (m *MyMainLoop) Finalize() {} // do something when the engine shuts down
func main() { startup.MainLoop(new(MyMainLoop))}
SceneTree
Section titled “SceneTree”Otherwise, leave the default SceneTree
main loop implementation in place and pass control
to the engine using startup.Scene()
which will block until the engine shuts down. Projects setup
using the gd
command will open the main.tscn
by default.
package main
import ( "graphics.gd/classdb/SceneTree" "graphics.gd/classdb/Node" "graphics.gd/startup")
func main() { startup.LoadingScene() // (optional) wait until the engine is ready to load the scene. SceneTree.Add(Node.New()) // (optional) setup the SceneTree, e.g. add nodes startup.Scene() // starts up the scene and blocks until the engine shuts down.}
AsExtension
Section titled “AsExtension”Want to create extensions that can be redistributed to other Godot engine users? Please note that only a single Go runtime can be active within any OS process, so all Go extensions within a project should be built together into a single shared library.
package main
import ( "graphics.gd/startup")
func main() { startup.AsExtension()}