Skip to content

Instantiating Objects

Each object provided by the engine is available under the graphics.gd/classdb, they can be instantiated using the New function defined in their respective package.

The New function will return the convenient Instance type, which will include all of the methods available on that object (which will be exclusively using convienience types and do not include any optional arguments). Instance values can be converted to both Advanced types for low-level use and Expanded types (which include all optional arguments).

package main
import (
"graphics.gd/startup"
"graphics.gd/classdb/Node"
"graphics.gd/variant/String"
)
func main() {
startup.Rendering()
var node Node.Instance = Node.New()
node.SetName("Hello World")
var advanced = Node.Advanced(node)
advanced.SetName(String.New("Hello World"))
var id Node.ID = node.ID() // all instances have an ID type and method.
fmt.Println("Node ID:", id)
}

Sometimes you will receive a generic Node.Instance and you want to check if it is a Node3D.Instance or *ExtensionNode, use the Object.Is,
Object.As and Object.To functions to check and convert between these types.

if node3D, ok := Object.As[Node3D.Instance](node); ok {
fmt.Println(node3D, " is a Node3D!")
}
if Object.Is[*ExtensionNode](node) {
fmt.Println("Node is an ExtensionNode!")
}
// if you already know the type of the object and just
// want to convert it, use Object.To, this will panic
// if the type is incorrect.
node2d := Object.To[Node2D.Instance](node)