Skip to content

Registration

In order to register a new class that will be available within the editor, create a Go struct that embeds the T.Extension type of the object you would like to extend.

All struct fields beginning with an uppercase letter will be exported as properties in the editor, and all methods beginning with an uppercase letter will be exported for use in .gd scripts.

package main
import (
"fmt"
"graphics.gd/classdb"
"graphics.gd/classdb/Node"
"graphics.gd/startup"
)
type MyClass struct {
Node.Extension[MyClass]
MyProperty string // properties will be exported as snake_case
}
// Implements the Ready method of the [Node.Interface]
func (*MyClass) Ready() {
fmt.Println("MyClass is ready!")
}
// methods will be available to scripts in snake_case, ie. my_script_method
func (*MyClass) MyScriptMethod() {
fmt.Println("MyClass script method called!")
}
func main() {
classdb.Register[MyClass]()
startup.Scene()
}

graphics.gd will automatically register everything inside your struct for use with scripts, struct tags are available for more control on how struct fields will be presented to the engine.

  • gd:"rename" sets the name of of the property within the engine.
  • range:"min_value, max_value, step, flags" configures the allowed range of values for the property, this property will be represented as a slider in the editor.
  • group:"name" sets the associated group that the property will be organised under within the editor.

Go doesn’t have constructors like .gd script, instead, if you have a function that you are use to initialize your structures, you can register this with the engine, so that it will be used as the constructor by scripts.

package main
import (
"graphics.gd/classdb"
"graphics.gd/classdb/Node"
"graphics.gd/startup"
)
type MapContainer struct {
Node.Extension[MapContainer]
my_map map[string]string
}
func NewMapContainer() *MapContainer {
return &MapContainer{
my_map: make(map[string]string),
}
}
func main() {
classdb.Register[MyClass](NewMapContainer)
startup.Scene()
}

You can register additional functions to be bundled with your class and available to scripts by passing them to classdb.Register. You can also rename these functions by passing a map[string]any.

package main
import (
"graphics.gd/classdb"
"graphics.gd/variant/Object"
"graphics.gd/startup"
)
type UtilityFunctions struct { Object.Extension[UtilityFunctions] }
func DoSomething() {}
func main() {
classdb.Register[UtilityFunctions](DoSomething)
startup.Scene()
}

Functions and methods can be renamed inside the engine by passing a map[string]any to classdb.Register with the functions and methods you would like to specify the name of.

package main
import (
"graphics.gd/classdb"
"graphics.gd/variant/Object"
"graphics.gd/startup"
)
type MyObject struct { Object.Extension[MyObject] }
func (*MyObject) DoSomething() {}
func main() {
classdb.Register[RenamedFunctions](map[string]any{
"do_something_renamed": MyObject.DoSomething,
})
startup.Scene()
}

If you use the graphics.gd/variant/Enum package to define your enums, they will be registered automatically if they are used by any functions or methods. You can also register constants by passing them in a map[string]int to classdb.Register.

package main
import (
"graphics.gd/classdb"
"graphics.gd/variant/Object"
"graphics.gd/variant/Enum"
"graphics.gd/startup"
)
const (
MyConstant = 1
)
type MyEnum Enum.Int[struct{
SomeValue MyEnum
}]
var MyEnums = Enum.Values[MyEnum]()
type HasEnumsConstants struct { Object.Extension[HasEnumsConstants] }
func (*HasEnumsConstants) Enum() MyEnum {
return MyEnums.SomeValue
}
func main() {
classdb.Register[HasEnumsConstants](map[string]int{
"MY_CONSTANT": MyConstant,
})
startup.Scene()
}