Object

package
v0.0.0-...-6566898 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 11, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package Object provides methods for working with Object instances.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func As

func As[T gd.IsClass](value gd.IsClass) (T, bool)

As attempts to cast the given class to T, returning true if the cast was successful.

func Call

func Call(object Any, method string, args ...any) any

Call calls the method on the object and returns the result.

func Get

func Get(object Any, property string) any

Get returns the Variant value of the given property. If the property does not exist, this method returns null.

Note: property must be in snake_case when referring to built-in Godot properties.

func HasMethod

func HasMethod(object Any, method string) bool

HasMethod returns true if the given method name exists in the object.

Note: In C#, method must be in snake_case when referring to built-in Godot methods. Prefer using the names exposed in the MethodName class to avoid allocating a new StringName on each call.

func InstanceIsValid

func InstanceIsValid(obj Any) bool

InstanceIsValid returns true if the given object instance is valid (the reference has not been invalidated and the object has not been freed).

func Is

func Is[T gd.IsClass](value gd.IsClass) bool

Is returns true if the given class is of type T.

func Set

func Set(object Any, property string, value any)

Set assigns value to the given property. If the property does not exist or the given value's type doesn't match, nothing happens.

Note: property must be in snake_case when referring to built-in Godot properties.

func To

func To[T gd.IsClass](value gd.IsClass) T

To attempts to cast the given class to T, returning the casted value if successful, or panicking if not.

func Use

func Use(obj interface{ AsObject() [1]gdclass.Object })

Use keeps an object alive, preventing it from being garbage collected until the next frame.

Types

type Advanced

type Advanced [1]gdclass.Object

func (Advanced) AsObject

func (obj Advanced) AsObject() [1]gd.Object

func (*Advanced) UnsafePointer

func (self *Advanced) UnsafePointer() unsafe.Pointer

func (Advanced) Virtual

func (obj Advanced) Virtual(name string) reflect.Value

Virtual method lookup.

type Any

type Any interface {
	gd.IsClass
	AsObject() [1]gd.Object
}

Any object.

type Extension

type Extension[T gdclass.Interface] struct {
	gdclass.Extension[T, Instance]
}

Extension can be embedded in a struct to create a new class. T should be the type of the struct that embeds this Extension.

func (*Extension[T]) AsObject

func (class *Extension[T]) AsObject() [1]gdclass.Object

type ID

type ID uint64

ID uniquely and opaquely identifies an Object instance.

func (ID) Instance

func (id ID) Instance() Instance

Instance returns the Object instance identified by this ID.

type Instance

type Instance [1]gdclass.Object

Instance is an advanced Variant type. All classes in the engine inherit from Object. Each class may define new properties, methods or signals, which are available to all inheriting classes. For example, a Sprite2D instance is able to call Node.add_child because it inherits from Node.

You can create new instances, using New.

Objects can have a Script attached to them. Once the Script is instantiated, it effectively acts as an extension to the base class, allowing it to define and inherit new properties, methods and signals.

Each Interface method can be overidden independently:

type Interface interface {
	Get(property string) any
	GetPropertyList() []ClassDB.PropertyInfo
	Notification(what int, reversed bool)
	PropertyCanRevert(property string) bool
	PropertyGetRevert(property string) any
	Set(property string, value any) bool
	ToString() string
	ValidateProperty(ClassDB.PropertyInfo)
}
var Nil Instance

Nil is a nil Object instance. Useful for comparisons.

func New

func New() Instance

New creates a new Object instance.

func (Instance) AsObject

func (obj Instance) AsObject() [1]gd.Object

func (Instance) CanTranslateMessages

func (obj Instance) CanTranslateMessages() bool

CanTranslateMessages returns true if the object is allowed to translate messages with tr and tr_n. See also Instance.SetMessageTranslation.

func (Instance) ClassName

func (obj Instance) ClassName() string

ClassName returns the object's built-in class name, as a string.

func (Instance) Connect

func (obj Instance) Connect(signal string, callable any, flags ...Signal.Flags) error

Connect connects a signal by name to a callable. Optional flags can be also added to configure the connection's behavior (see Signal.Flags constants).

A signal can only be connected once to the same Callable. If the signal is already connected, this method returns Error.InvalidParameter and pushes an error message, unless the signal is connected with [Signal.Weak]. To prevent this, use Instance.IsConnected first to check for existing connections.

If the callable's object is freed, the connection will be lost.

func (Instance) HasMethod

func (obj Instance) HasMethod(name string) bool

HasMethod returns true if the object has a method with the given name.

func (Instance) ID

func (obj Instance) ID() ID

ID returns the object's unique instance ID. This ID can be saved in EncodedObjectAsID, and can be used to retrieve this object instance with ID.Instance.

func (Instance) IsConnected

func (obj Instance) IsConnected(signal string, callable any) bool

IsConnected returns true if a connection exists between the given signal name and callable.

func (Instance) NotifyPropertyListChanged

func (obj Instance) NotifyPropertyListChanged()

NotifyPropertyListChanged emits the property_list_changed signal. This is mainly used to refresh the editor, so that the Inspector and editor plugins are properly updated.

func (Instance) Script

func (obj Instance) Script() ([1]gdclass.Script, bool)

ScriptInstance returns the object's Script instance, or false if no script is attached.

func (Instance) SetMessageTranslation

func (obj Instance) SetMessageTranslation(enable bool)

SetMessageTranslation if set to true, allows the object to translate messages with tr and tr_n. Enabled by default. See also Instance.CanTranslateMessages.

func (*Instance) SetObject

func (self *Instance) SetObject(obj [1]gdclass.Object) bool

func (Instance) SetScript

func (obj Instance) SetScript(script [1]gdclass.Script)

SetScript attaches script to the object, and instantiates it. As a result, the script's _init is called. A Script is used to extend the object's functionality.

If a script already exists, its instance is detached, and its property values and state are lost. Built-in property values are still kept.

func (Instance) SetSignalsBlocked

func (obj Instance) SetSignalsBlocked(enable bool)

SetBlockSignals if set to true, the object becomes unable to emit signals. Signal connections will not work, until it is set to false.

func (Instance) Signal

func (obj Instance) Signal(name string) Signal.Any

Signal returns the signal with the given name, or a nil signal if it does not exist.

func (Instance) SignalsBlocked

func (obj Instance) SignalsBlocked() bool

SignalsBlocked returns true if the object is blocking its signals from being emitted. See Instance.SetSignalsBlocked.

func (Instance) String

func (obj Instance) String() string

String returns a String representing the object. Defaults to "<ClassName#RID>". Override _to_string to customize the string representation of the object.

func (Instance) Translate

func (obj Instance) Translate(message string) string

Translate translates a message, using the translation catalogs configured in the Project Settings. Note that most Control nodes automatically translate their strings, so this method is mostly useful for formatted strings or custom drawn text.

If Instance.CanTranslateMessages is false, or no translation is available, this method returns the message without changes. See Instance.SetMessageTranslation.

func (Instance) Translation

func (obj Instance) Translation(message string, plural_message string, n int, context string) string

Translation translates a message or plural_message, using the translation catalogs configured in the Project Settings. Further context can be specified to help with the translation.

If Instance.CanTranslateMessages is false, or no translation is available, this method returns message or plural_message, without changes. See Instance.SetMessageTranslation.

The n is the number, or amount, of the message's subject. It is used by the translation system to fetch the correct plural form for the current language.

For detailed examples, see Localization using gettext.

Note: Negative and float numbers may not properly apply to some countable subjects. It's recommended to handle these cases with Instance.Translate.

Note: This method can't be used without an Object instance, as it requires the Instance.CanTranslateMessages method. To translate strings in a static context, use [TranslationServer.TranslatePlural].

func (*Instance) UnsafePointer

func (self *Instance) UnsafePointer() unsafe.Pointer

func (Instance) Virtual

func (obj Instance) Virtual(name string) reflect.Value

Virtual method lookup.

type Interface

type Interface interface {
	Get(property string) any
	GetPropertyList() []PropertyInfo
	Notification(what int, reversed bool)
	PropertyCanRevert(property string) bool
	PropertyGetRevert(property string) any
	Set(property string, value any) bool
	ToString() string
	ValidateProperty(PropertyInfo)
}

type Notification

type Notification int
const (
	NotificationPostInitialize    Notification = 0 // Notification received when the object is initialized, before its script is attached. Used internally.
	NotificationPreDelete         Notification = 1 // Notification received when the object is about to be deleted. Can be used like destructors in object-oriented programming languages.
	NotificationExtensionReloaded Notification = 2 // Notification received when the object finishes hot reloading. This notification is only sent for extensions classes and derived.
)

type PropertyInfo

type PropertyInfo struct {
	ClassName  string       `gd:"class_name"`
	Name       string       `gd:"name"`
	Hint       int          `gd:"hint"`
	HintString string       `gd:"hint_string"`
	Type       reflect.Type `gd:"type"`
	Usage      int          `gd:"usage"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL