Object
Base class for all non-built-in types.
Every class which is not a built-in type inherits from this class.
You can construct Objects from scripting languages, using in GDScript, new Object
in C#, or the “Construct Object” node in VisualScript.
Objects do not manage memory. If a class inherits from Object, you will have to delete instances of it manually. To do so, call the method from your script or delete the instance from C++.
Some classes that extend Object add memory management. This is the case of Reference, which counts references and deletes itself automatically when no longer referenced. , another fundamental type, deletes all its children when freed from memory.
Objects export properties, which are mainly useful for storage and editing, but not really so much in programming. Properties are exported in _get_property_list and handled in and _set. However, scripting languages and C++ have simpler means to export them.
Property membership can be tested directly in GDScript using in
:
The in
operator will evaluate to true
as long as the key exists, even if the value is null
.
Objects also receive notifications. Notifications are a simple way to notify the object about different events, so they can all be handled together. See .
Note: Unlike references to a Reference, references to an Object stored in a variable can become invalid without warning. Therefore, it’s recommended to use for data classes instead of Object
.
Note: Due to a bug, you can’t create a “plain” Object using Object.new()
. Instead, use ClassDB.instance("Object")
. This bug only applies to Object itself, not any of its descendents like Reference.
Tutorials
Signals
- script_changed ( )
Emitted whenever the object’s script is changed.
enum ConnectFlags:
CONNECT_DEFERRED = 1 —- Connects a signal in deferred mode. This way, signal emissions are stored in a queue, then set on idle time.
CONNECT_PERSIST = 2 —- Persisting connections are saved when the object is serialized to file.
CONNECT_ONESHOT = 4 —- One-shot connections disconnect themselves after emission.
CONNECT_REFERENCE_COUNTED = 8 —- Connect a signal as reference-counted. This means that a given signal can be connected several times to the same target, and will only be fully disconnected once no references are left.
Constants
NOTIFICATION_POSTINITIALIZE = 0 —- Called right when the object is initialized. Not available in script.
NOTIFICATION_PREDELETE = 1 —- Called before the object is about to be deleted.
- _get ( String property ) virtual
Virtual method which can be overridden to customize the return value of .
Returns the given property. Returns null
if the property
does not exist.
- Array _get_property_list ( ) virtual
Virtual method which can be overridden to customize the return value of .
Returns the object’s property list as an Array of dictionaries.
Each property’s must contain at least name: String
and type: int
(see Variant.Type) entries. Optionally, it can also include hint: int
(see ), hint_string: String
, and usage: int
(see PropertyUsageFlags).
- void _init ( ) virtual
Called when the object is initialized in memory. Can be defined to take in parameters, that are passed in when constructing.
Note: If is defined with required parameters, then explicit construction is the only valid means of creating an Object of the class. If any other means (such as PackedScene.instance) is used, then initialization will fail.
- void _notification ( what ) virtual
Called whenever the object receives a notification, which is identified in what
by a constant. The base Object
has two constants NOTIFICATION_POSTINITIALIZE and , but subclasses such as Node define a lot more notifications which are also received by this method.
- _set ( String property, value ) virtual
Virtual method which can be overridden to customize the return value of set.
Sets a property. Returns true
if the property
exists.
- _to_string ( ) virtual
Virtual method which can be overridden to customize the return value of to_string, and thus the object’s representation where it is converted to a string, e.g. with print(obj)
.
Returns a representing the object. If not overridden, defaults to "[ClassName:RID]"
.
- void add_user_signal ( String signal, arguments=[ ] )
Adds a user-defined signal
. Arguments are optional, but can be added as an Array of dictionaries, each containing name: String
and type: int
(see ) entries.
- Variant call ( method, … ) vararg
Calls the method
on the object and returns the result. This method supports a variable number of arguments, so parameters are passed as a comma separated list. Example:
- void call_deferred ( String method, … ) vararg
Calls the method
on the object during idle time. This method supports a variable number of arguments, so parameters are passed as a comma separated list. Example:
Note: In C#, the method name must be specified as snake_case if it is defined by a built-in Godot node. This doesn’t apply to user-defined methods where you should use the same convention as in the C# source (typically PascalCase).
- callv ( String method, arg_array )
Calls the method
on the object and returns the result. Contrarily to call, this method does not support a variable number of arguments but expects all parameters to be via a single .
callv("set", [ "position", Vector2(42.0, 0.0) ])
- bool can_translate_messages ( ) const
Returns true
if the object can translate strings. See and tr.
Connects a signal
to a method
on a target
object. Pass optional binds
to the call as an of parameters. These parameters will be passed to the method after any parameter used in the call to emit_signal. Use to set deferred or one-shot connections. See constants.
A signal
can only be connected once to a method
. It will print an error if already connected, unless the signal was connected with CONNECT_REFERENCE_COUNTED. To avoid this, first, use to check for existing connections.
If the target
is destroyed in the game’s lifecycle, the connection will be lost.
Examples:
An example of the relationship between binds
passed to connect and parameters used when calling :
connect("hit", self, "_on_Player_hit", [ weapon_type, damage ]) # weapon_type and damage are passed last
emit_signal("hit", "Dark lord", 5) # "Dark lord" and 5 are passed first
func _on_Player_hit(hit_by, level, weapon_type, damage):
print("Hit by %s (lvl %d) with weapon %s for %d damage" % [hit_by, level, weapon_type, damage])
Disconnects a signal
from a method
on the given target
.
If you try to disconnect a connection that does not exist, the method will print an error. Use to ensure that the connection exists.
- void emit_signal ( String signal, … ) vararg
Emits the given signal
. The signal must exist, so it should be a built-in signal of this class or one of its parent classes, or a user-defined signal. This method supports a variable number of arguments, so parameters are passed as a comma separated list. Example:
- void free ( )
Deletes the object from memory immediately. For s, you may want to use Node.queue_free to queue the node for safe deletion at the end of the current frame.
Important: If you have a variable pointing to an object, it will not be assigned to null
once the object is freed. Instead, it will point to a previously freed instance and you should validate it with before attempting to call its methods or access its properties.
- Variant get ( property ) const
Returns the Variant value of the given property
. If the property
doesn’t exist, this will return null
.
Note: In C#, the property name must be specified as snake_case if it is defined by a built-in Godot node. This doesn’t apply to user-defined properties where you should use the same convention as in the C# source (typically PascalCase).
- get_class ( ) const
Returns the object’s class as a String. See also .
Note: get_class does not take class_name
declarations into account. If the object has a class_name
defined, the base class name will be returned instead.
- get_incoming_connections ( ) const
Returns an Array of dictionaries with information about signals that are connected to the object.
Each contains three String entries:
source
is a reference to the signal emitter.signal_name
is the name of the connected signal.method_name
is the name of the method to which the signal is connected.
- Variant get_indexed ( property ) const
Gets the object’s property indexed by the given NodePath. The node path should be relative to the current object and can use the colon character (:
) to access nested properties. Examples: "position:x"
or "material:next_pass:blend_mode"
.
Note: Even though the method takes argument, it doesn’t support actual paths to Nodes in the scene tree, only colon-separated sub-property paths. For the purpose of nodes, use instead.
- int get_instance_id ( ) const
Returns the object’s unique instance ID.
This ID can be saved in , and can be used to retrieve the object instance with @GDScript.instance_from_id.
- get_meta ( String name, default=null ) const
Returns the object’s metadata entry for the given name
.
Throws error if the entry does not exist, unless default
is not null
(in which case the default value will be returned).
- PoolStringArray get_meta_list ( ) const
Returns the object’s metadata as a .
- Array get_method_list ( ) const
Returns the object’s methods and their signatures as an .
- Array get_property_list ( ) const
Returns the object’s property list as an of dictionaries.
Each property’s Dictionary contain at least name: String
and type: int
(see ) entries. Optionally, it can also include hint: int
(see PropertyHint), hint_string: String
, and usage: int
(see ).
Returns the object’s Script instance, or null
if none is assigned.
- get_signal_connection_list ( String signal ) const
Returns an of connections for the given signal
.
- Array get_signal_list ( ) const
Returns the list of signals as an of dictionaries.
- bool has_meta ( name ) const
Returns if a metadata entry is found with the given name
.
- bool has_method ( method ) const
Returns true
if the object contains the given method
.
- bool has_signal ( signal ) const
Returns true
if the given signal
exists.
- bool has_user_signal ( signal ) const
Returns true
if the given user-defined signal
exists. Only signals added using add_user_signal are taken into account.
- is_blocking_signals ( ) const
Returns true
if signal emission blocking is enabled.
- bool is_class ( class ) const
Returns true
if the object inherits from the given class
. See also get_class.
Note: does not take class_name
declarations into account. If the object has a class_name
defined, is_class will return false
for that name.
Returns true
if a connection exists for a given signal
, target
, and method
.
- is_queued_for_deletion ( ) const
Returns true
if the Node.queue_free method was called for the object.
- void notification ( what, bool reversed=false )
Send a given notification to the object, which will also trigger a call to the method of all classes that the object inherits from.
If reversed
is true
, _notification is called first on the object’s own class, and then up to its successive parent classes. If reversed
is false
, is called first on the highest ancestor (Object
itself), and then down to its successive inheriting classes.
- void property_list_changed_notify ( )
Notify the editor that the property list has changed, so that editor plugins can take the new values into account. Does nothing on export builds.
- void remove_meta ( String name )
Removes a given entry from the object’s metadata. See also .
- void set ( String property, value )
Assigns a new value to the given property. If the property
does not exist or the given value’s type doesn’t match, nothing will happen.
Note: In C#, the property name must be specified as snake_case if it is defined by a built-in Godot node. This doesn’t apply to user-defined properties where you should use the same convention as in the C# source (typically PascalCase).
- void set_block_signals ( bool enable )
If set to true
, signal emission is blocked.
- void set_deferred ( property, Variant value )
Assigns a new value to the given property, after the current frame’s physics step. This is equivalent to calling via call_deferred, i.e. call_deferred("set", property, value)
.
Note: In C#, the property name must be specified as snake_case if it is defined by a built-in Godot node. This doesn’t apply to user-defined properties where you should use the same convention as in the C# source (typically PascalCase).
- void set_indexed ( property, Variant value )
Assigns a new value to the property identified by the . The node path should be relative to the current object and can use the colon character (:
) to access nested properties. Example:
set_indexed("position", Vector2(42, 0))
set_indexed("position:y", -10)
print(position) # (42, -10)
- void set_message_translation ( bool enable )
Defines whether the object can translate strings (with calls to ). Enabled by default.
- void set_meta ( String name, value )
Adds, changes or removes a given entry in the object’s metadata. Metadata are serialized and can take any Variant value.
To remove a given entry from the object’s metadata, use . Metadata is also removed if its value is set to null
. This means you can also use set_meta("name", null)
to remove metadata for "name"
.
- void set_script ( Reference script )
Assigns a script to the object. Each object can have a single script assigned to it, which are used to extend its functionality.
If the object already had a script, the previous script instance will be freed and its variables and state will be lost. The new script’s method will be called.
- String to_string ( )
Returns a representing the object. If not overridden, defaults to "[ClassName:RID]"
.
Override the method _to_string to customize the representation.
Translates a message using translation catalogs configured in the Project Settings.
Only works if message translation is enabled (which it is by default), otherwise it returns the unchanged. See set_message_translation.