Skip to main content

Object

Struct Object 

Source
pub struct Object { /* private fields */ }
Expand description

Godot class Object.

This is the base class for all other classes at the root of the hierarchy. Every instance of Object can be stored in a Gd smart pointer.

Related symbols:

See also Godot docs for Object.

§Construction

This class is manually managed. You can create a new instance using Object::new_alloc().

Do not forget to call free() or hand over ownership to Godot.

§Godot docs

An advanced [Variant][crate::builtin::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][crate::classes::Sprite2D] instance is able to call add_child because it inherits from Node.

You can create new instances, using Object.new() in GDScript, or new GodotObject in C#.

To delete an Object instance, call free. This is necessary for most classes inheriting Object, because they do not manage memory on their own, and will otherwise cause memory leaks when no longer in use. There are a few classes that perform memory management. For example, RefCounted (and by extension Resource) deletes itself when no longer referenced, and Node deletes its children when freed.

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.

Inside a Script, get_property_list may be overridden to customize properties in several ways. This allows them to be available to the editor, display as lists of options, sub-divide into groups, save on disk, etc. Scripting languages offer easier ways to customize properties, such as with the [annotation @GDScript.@export] annotation.

Godot is very dynamic. An object’s script, and therefore its properties, methods and signals, can be changed at run-time. Because of this, there can be occasions where, for example, a property required by a method may not exist. To prevent run-time errors, see methods such as set, get, call, has_method, has_signal, etc. Note that these methods are much slower than direct references.

In GDScript, you can also check if a given property, method, or signal name exists in an object with the in operator:

var node = Node.new()
print("name" in node)         # Prints true
print("get_parent" in node)   # Prints true
print("tree_entered" in node) # Prints true
print("unknown" in node)      # Prints false

Notifications are int constants commonly sent and received by objects. For example, on every rendered frame, the SceneTree notifies nodes inside the tree with a [constant Node.NOTIFICATION_PROCESS]. The nodes receive it and may call process to update. To make use of notifications, see notify and on_notification.

Lastly, every object can also contain metadata (data about data). set_meta can be useful to store information that the object itself does not depend on. To keep your code clean, making excessive use of metadata is discouraged.

Note: Unlike references to a RefCounted, references to an object stored in a variable can become invalid without being set to null. To check if an object has been deleted, do not compare it against null. Instead, use is_instance_valid. It’s also recommended to inherit from RefCounted for classes storing data instead of Object.

Note: The script is not exposed like most properties. To set or get an object’s Script in code, use [method set_script] and [method get_script], respectively.

Note: In a boolean context, an Object will evaluate to false if it is equal to null or it has been freed. Otherwise, an Object will always evaluate to true. See also is_instance_valid.

Implementations§

Source§

impl Object

Source

pub fn get_script(&self) -> Option<Gd<Script>>

Source

pub fn set_script(&mut self, script: impl AsArg<Option<Gd<Script>>>)

Source

pub fn connect( &mut self, signal: impl AsArg<StringName>, callable: &Callable, ) -> Error

Source

pub fn connect_flags( &mut self, signal: impl AsArg<StringName>, callable: &Callable, flags: ConnectFlags, ) -> Error

Source§

impl Object

Source

pub fn get_class(&self) -> GString

Source

pub fn is_class(&self, class: impl AsArg<GString>) -> bool

Source

pub fn set(&mut self, property: impl AsArg<StringName>, value: &Variant)

Source

pub fn get(&self, property: impl AsArg<StringName>) -> Variant

Source

pub fn set_indexed( &mut self, property_path: impl AsArg<NodePath>, value: &Variant, )

Source

pub fn get_indexed(&self, property_path: impl AsArg<NodePath>) -> Variant

Source

pub fn get_property_list(&self) -> Array<VarDictionary>

Source

pub fn get_method_list(&self) -> Array<VarDictionary>

Source

pub fn property_can_revert(&self, property: impl AsArg<StringName>) -> bool

Source

pub fn property_get_revert(&self, property: impl AsArg<StringName>) -> Variant

Source

pub fn set_meta(&mut self, name: impl AsArg<StringName>, value: &Variant)

Source

pub fn remove_meta(&mut self, name: impl AsArg<StringName>)

Source

pub fn get_meta(&self, name: impl AsArg<StringName>) -> Variant

To set the default parameters, use Self::get_meta_ex and its builder methods. See the book for detailed usage instructions.

Source

pub fn get_meta_ex<'ex>( &'ex self, name: impl AsArg<StringName> + 'ex, ) -> ExGetMeta<'ex>

Source

pub fn has_meta(&self, name: impl AsArg<StringName>) -> bool

Source

pub fn get_meta_list(&self) -> Array<StringName>

Source

pub fn add_user_signal(&mut self, signal: impl AsArg<GString>)

To set the default parameters, use Self::add_user_signal_ex and its builder methods. See the book for detailed usage instructions.

Source

pub fn add_user_signal_ex<'ex>( &'ex mut self, signal: impl AsArg<GString> + 'ex, ) -> ExAddUserSignal<'ex>

Source

pub fn has_user_signal(&self, signal: impl AsArg<StringName>) -> bool

Source

pub fn remove_user_signal(&mut self, signal: impl AsArg<StringName>)

Source

pub fn emit_signal( &mut self, signal: impl AsArg<StringName>, varargs: &[Variant], ) -> Error

§Panics

This is a varcall method, meaning parameters and return values are passed as Variant. It can detect call failures and will panic in such a case.

Source

pub fn try_emit_signal( &mut self, signal: impl AsArg<StringName>, varargs: &[Variant], ) -> Result<Error, CallError>

§Return type

This is a varcall method, meaning parameters and return values are passed as Variant. It can detect call failures and will return Err in such a case.

Source

pub fn call( &mut self, method: impl AsArg<StringName>, varargs: &[Variant], ) -> Variant

§Panics

This is a varcall method, meaning parameters and return values are passed as Variant. It can detect call failures and will panic in such a case.

Source

pub fn try_call( &mut self, method: impl AsArg<StringName>, varargs: &[Variant], ) -> Result<Variant, CallError>

§Return type

This is a varcall method, meaning parameters and return values are passed as Variant. It can detect call failures and will return Err in such a case.

Source

pub fn call_deferred( &mut self, method: impl AsArg<StringName>, varargs: &[Variant], ) -> Variant

§Panics

This is a varcall method, meaning parameters and return values are passed as Variant. It can detect call failures and will panic in such a case.

Source

pub fn try_call_deferred( &mut self, method: impl AsArg<StringName>, varargs: &[Variant], ) -> Result<Variant, CallError>

§Return type

This is a varcall method, meaning parameters and return values are passed as Variant. It can detect call failures and will return Err in such a case.

Source

pub fn set_deferred( &mut self, property: impl AsArg<StringName>, value: &Variant, )

Source

pub fn callv( &mut self, method: impl AsArg<StringName>, arg_array: &AnyArray, ) -> Variant

Source

pub fn has_method(&self, method: impl AsArg<StringName>) -> bool

Source

pub fn get_method_argument_count(&self, method: impl AsArg<StringName>) -> i32

Source

pub fn has_signal(&self, signal: impl AsArg<StringName>) -> bool

Source

pub fn get_signal_list(&self) -> Array<VarDictionary>

Source

pub fn get_signal_connection_list( &self, signal: impl AsArg<StringName>, ) -> Array<VarDictionary>

Source

pub fn get_incoming_connections(&self) -> Array<VarDictionary>

Source

pub fn disconnect( &mut self, signal: impl AsArg<StringName>, callable: &Callable, )

Source

pub fn is_connected( &self, signal: impl AsArg<StringName>, callable: &Callable, ) -> bool

Source

pub fn has_connections(&self, signal: impl AsArg<StringName>) -> bool

Source

pub fn set_block_signals(&mut self, enable: bool)

Source

pub fn is_blocking_signals(&self) -> bool

Source

pub fn notify_property_list_changed(&mut self)

Source

pub fn set_message_translation(&mut self, enable: bool)

Source

pub fn can_translate_messages(&self) -> bool

Source

pub fn tr(&self, message: impl AsArg<StringName>) -> GString

To set the default parameters, use Self::tr_ex and its builder methods. See the book for detailed usage instructions.

Source

pub fn tr_ex<'ex>(&'ex self, message: impl AsArg<StringName> + 'ex) -> ExTr<'ex>

Source

pub fn tr_n( &self, message: impl AsArg<StringName>, plural_message: impl AsArg<StringName>, n: i32, ) -> GString

To set the default parameters, use Self::tr_n_ex and its builder methods. See the book for detailed usage instructions.

Source

pub fn tr_n_ex<'ex>( &'ex self, message: impl AsArg<StringName> + 'ex, plural_message: impl AsArg<StringName> + 'ex, n: i32, ) -> ExTrN<'ex>

Source

pub fn get_translation_domain(&self) -> StringName

Source

pub fn set_translation_domain(&mut self, domain: impl AsArg<StringName>)

Source

pub fn is_queued_for_deletion(&self) -> bool

Source

pub fn cancel_free(&mut self)

Source

pub fn notify(&mut self, what: ObjectNotification)

⚠️ Sends a Godot notification to all classes inherited by the object.

Triggers calls to on_notification(), and depending on the notification, also to Godot’s lifecycle callbacks such as ready().

Starts from the highest ancestor (the Object class) and goes down the hierarchy. See also Godot docs for Object::notification().

§Panics

If you call this method on a user-defined object while holding a GdRef or GdMut guard on the instance, you will encounter a panic. The reason is that the receiving virtual method on_notification() acquires a GdMut lock dynamically, which must be exclusive.

Source

pub fn notify_reversed(&mut self, what: ObjectNotification)

⚠️ Like Self::notify(), but starts at the most-derived class and goes up the hierarchy.

See docs of that method, including the panics.

Trait Implementations§

Source§

impl Bounds for Object

Source§

type Memory = MemManual

Defines the memory strategy of the static type.
Source§

type Declarer = DeclEngine

Whether this class is a core Godot class provided by the engine, or declared by the user as a Rust struct.
Source§

impl Debug for Object

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl GodotClass for Object

Source§

const INIT_LEVEL: InitLevel = crate::init::InitLevel::Core

Initialization level, during which this class should be initialized with Godot. Read more
Source§

type Base = NoBase

The immediate superclass of T. This is always a Godot engine class.
Source§

fn class_id() -> ClassId

Globally unique class ID, linked to the name under which the class is registered in Godot. Read more
Source§

fn inherits<Base: GodotClass>() -> bool

Returns whether Self inherits from Base. Read more
Source§

impl Inherits<Object> for ArrayMesh

Source§

const IS_SAME_CLASS: bool = false

True iff Self == Base. Read more
Source§

impl Inherits<Object> for CanvasItem

Source§

const IS_SAME_CLASS: bool = false

True iff Self == Base. Read more
Source§

impl Inherits<Object> for ClassDb

Source§

const IS_SAME_CLASS: bool = false

True iff Self == Base. Read more
Source§

impl Inherits<Object> for EditorExportPlugin

Source§

const IS_SAME_CLASS: bool = false

True iff Self == Base. Read more
Source§

impl Inherits<Object> for EditorPlugin

Source§

const IS_SAME_CLASS: bool = false

True iff Self == Base. Read more
Source§

impl Inherits<Object> for Engine

Source§

const IS_SAME_CLASS: bool = false

True iff Self == Base. Read more
Source§

impl Inherits<Object> for FileAccess

Source§

const IS_SAME_CLASS: bool = false

True iff Self == Base. Read more
Source§

impl Inherits<Object> for GDScript

Source§

const IS_SAME_CLASS: bool = false

True iff Self == Base. Read more
Source§

impl Inherits<Object> for HttpRequest

Source§

const IS_SAME_CLASS: bool = false

True iff Self == Base. Read more
Source§

impl Inherits<Object> for Input

Source§

const IS_SAME_CLASS: bool = false

True iff Self == Base. Read more
Source§

impl Inherits<Object> for InputEvent

Source§

const IS_SAME_CLASS: bool = false

True iff Self == Base. Read more
Source§

impl Inherits<Object> for InputEventAction

Source§

const IS_SAME_CLASS: bool = false

True iff Self == Base. Read more
Source§

impl Inherits<Object> for MainLoop

Source§

const IS_SAME_CLASS: bool = false

True iff Self == Base. Read more
Source§

impl Inherits<Object> for Mesh

Source§

const IS_SAME_CLASS: bool = false

True iff Self == Base. Read more
Source§

impl Inherits<Object> for Node

Source§

const IS_SAME_CLASS: bool = false

True iff Self == Base. Read more
Source§

impl Inherits<Object> for Node2D

Source§

const IS_SAME_CLASS: bool = false

True iff Self == Base. Read more
Source§

impl Inherits<Object> for Node3D

Source§

const IS_SAME_CLASS: bool = false

True iff Self == Base. Read more
Source§

impl Inherits<Object> for Os

Source§

const IS_SAME_CLASS: bool = false

True iff Self == Base. Read more
Source§

impl Inherits<Object> for PackedScene

Source§

const IS_SAME_CLASS: bool = false

True iff Self == Base. Read more
Source§

impl Inherits<Object> for PrimitiveMesh

Source§

const IS_SAME_CLASS: bool = false

True iff Self == Base. Read more
Source§

impl Inherits<Object> for RefCounted

Source§

const IS_SAME_CLASS: bool = false

True iff Self == Base. Read more
Source§

impl Inherits<Object> for RenderingServer

Source§

const IS_SAME_CLASS: bool = false

True iff Self == Base. Read more
Source§

impl Inherits<Object> for Resource

Source§

const IS_SAME_CLASS: bool = false

True iff Self == Base. Read more
Source§

impl Inherits<Object> for ResourceFormatLoader

Source§

const IS_SAME_CLASS: bool = false

True iff Self == Base. Read more
Source§

impl Inherits<Object> for ResourceLoader

Source§

const IS_SAME_CLASS: bool = false

True iff Self == Base. Read more
Source§

impl Inherits<Object> for ResourceSaver

Source§

const IS_SAME_CLASS: bool = false

True iff Self == Base. Read more
Source§

impl Inherits<Object> for SceneTree

Source§

const IS_SAME_CLASS: bool = false

True iff Self == Base. Read more
Source§

impl Inherits<Object> for Script

Source§

const IS_SAME_CLASS: bool = false

True iff Self == Base. Read more
Source§

impl Inherits<Object> for ScriptExtension

Source§

const IS_SAME_CLASS: bool = false

True iff Self == Base. Read more
Source§

impl Inherits<Object> for ScriptLanguage

Source§

const IS_SAME_CLASS: bool = false

True iff Self == Base. Read more
Source§

impl Inherits<Object> for ScriptLanguageExtension

Source§

const IS_SAME_CLASS: bool = false

True iff Self == Base. Read more
Source§

impl Inherits<Object> for Time

Source§

const IS_SAME_CLASS: bool = false

True iff Self == Base. Read more
Source§

impl Inherits<Object> for Viewport

Source§

const IS_SAME_CLASS: bool = false

True iff Self == Base. Read more
Source§

impl Inherits<Object> for Window

Source§

const IS_SAME_CLASS: bool = false

True iff Self == Base. Read more
Source§

impl WithSignals for Object

Source§

type SignalCollection<'c, C: WithSignals> = SignalsOfObject<'c, C>

The associated struct listing all signals of this class. Read more
Source§

impl GodotDefault for Object

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Inherits<T> for T
where T: GodotClass,

Source§

const IS_SAME_CLASS: bool = const IS_SAME_CLASS: bool = true;

True iff Self == Base. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> NewAlloc for T
where T: GodotDefault<Memory = MemManual> + Bounds,

Source§

fn new_alloc() -> Gd<T>

Return a new, manually-managed Gd containing a default-constructed instance. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> UniformObjectDeref<DeclEngine> for T
where T: GodotClass<Declarer = DeclEngine>,

Source§

type TargetRef<'a> = Gd<T>

Source§

type TargetMut<'a> = Gd<T>

Source§

fn object_as_ref<'a>( gd: &'a Gd<T>, ) -> <T as UniformObjectDeref<DeclEngine>>::TargetRef<'a>

Source§

fn object_as_mut<'a>( gd: &'a mut Gd<T>, ) -> <T as UniformObjectDeref<DeclEngine>>::TargetMut<'a>