pub struct Script { /* private fields */ }
Expand description

core class Script inherits Resource (reference-counted).

Official documentation

See the documentation of this class in the Godot engine’s official documentation. The method descriptions are generated from it and typically contain code samples in GDScript, not Rust.

Memory management

The lifetime of this object is automatically managed through reference counting.

Class hierarchy

Script inherits methods from:

Safety

All types in the Godot API have interior mutability in Rust parlance. To enforce that the official thread-safety guidelines are followed, the typestate pattern is used in the Ref and TRef smart pointers, and the Instance API. The typestate Ownership in these types tracks whether ownership is unique, shared, or exclusive to the current thread. For more information, see the type-level documentation on Ref.

Implementations§

source§

impl Script

source

pub fn can_instance(&self) -> bool

Returns true if the script can be instanced.

source

pub fn get_base_script(&self) -> Option<Ref<Script, Shared>>

Returns the script directly inherited by this script.

source

pub fn get_instance_base_type(&self) -> GodotString

Returns the script’s base type.

source

pub fn get_property_default_value(
    &self,
    property: impl Into<GodotString>
) -> Variant

Returns the default value of the specified property.

source

pub fn get_script_constant_map(&self) -> Dictionary

Returns a dictionary containing constant names and their values.

source

pub fn get_script_method_list(&self) -> VariantArray

Returns the list of methods in this Script.

source

pub fn get_script_property_list(&self) -> VariantArray

Returns the list of properties in this Script.

source

pub fn get_script_signal_list(&self) -> VariantArray

Returns the list of user signals defined in this Script.

source

pub fn source_code(&self) -> GodotString

The script source code or an empty string if source code is not available. When set, does not reload the class implementation automatically.

source

pub fn has_script_signal(&self, signal_name: impl Into<GodotString>) -> bool

Returns true if the script, or a base class, defines a signal with the given name.

source

pub fn has_source_code(&self) -> bool

Returns true if the script contains non-empty source code.

source

pub fn instance_has(&self, base_object: impl AsArg<Object>) -> bool

Returns true if base_object is an instance of this script.

source

pub fn is_tool(&self) -> bool

Returns true if the script is a tool script. A tool script can run in the editor.

source

pub fn reload(&self, keep_state: bool) -> GodotResult

Reloads the script’s class implementation. Returns an error code.

Default Arguments
  • keep_state - false
source

pub fn set_source_code(&self, source: impl Into<GodotString>)

The script source code or an empty string if source code is not available. When set, does not reload the class implementation automatically.

Methods from Deref<Target = Resource>§

source

pub fn duplicate(&self, subresources: bool) -> Option<Ref<Resource, Shared>>

Duplicates the resource, returning a new resource with the exported members copied. Note: To duplicate the resource the constructor is called without arguments. This method will error when the constructor doesn’t have default values. By default, sub-resources are shared between resource copies for efficiency. This can be changed by passing true to the subresources argument which will copy the subresources. Note: If subresources is true, this method will only perform a shallow copy. Nested resources within subresources will not be duplicated and will still be shared. Note: When duplicating a resource, only exported properties are copied. Other properties will be set to their default value in the new resource.

Default Arguments
  • subresources - false
source

pub fn emit_changed(&self)

Sample code is GDScript unless otherwise noted.

Emits the changed signal. If external objects which depend on this resource should be updated, this method must be called manually whenever the state of this resource has changed (such as modification of properties). The method is equivalent to:

emit_signal("changed")

Note: This method is called automatically for built-in resources.

source

pub fn get_local_scene(&self) -> Option<Ref<Node, Shared>>

If [resource_local_to_scene][Self::resource_local_to_scene] is enabled and the resource was loaded from a PackedScene instantiation, returns the local scene where this resource’s unique copy is in use. Otherwise, returns null.

source

pub fn name(&self) -> GodotString

The name of the resource. This is an optional identifier. If [resource_name][Self::resource_name] is not empty, its value will be displayed to represent the current resource in the editor inspector. For built-in scripts, the [resource_name][Self::resource_name] will be displayed as the tab name in the script editor.

source

pub fn path(&self) -> GodotString

The path to the resource. In case it has its own file, it will return its filepath. If it’s tied to the scene, it will return the scene’s path, followed by the resource’s index.

source

pub fn get_rid(&self) -> Rid

Returns the RID of the resource (or an empty RID). Many resources (such as Texture, Mesh, etc) are high-level abstractions of resources stored in a server, so this function will return the original RID.

source

pub fn is_local_to_scene(&self) -> bool

If true, the resource will be made unique in each instance of its local scene. It can thus be modified in a scene instance without impacting other instances of that same scene.

source

pub fn set_local_to_scene(&self, enable: bool)

If true, the resource will be made unique in each instance of its local scene. It can thus be modified in a scene instance without impacting other instances of that same scene.

source

pub fn set_name(&self, name: impl Into<GodotString>)

The name of the resource. This is an optional identifier. If [resource_name][Self::resource_name] is not empty, its value will be displayed to represent the current resource in the editor inspector. For built-in scripts, the [resource_name][Self::resource_name] will be displayed as the tab name in the script editor.

source

pub fn set_path(&self, path: impl Into<GodotString>)

The path to the resource. In case it has its own file, it will return its filepath. If it’s tied to the scene, it will return the scene’s path, followed by the resource’s index.

source

pub fn setup_local_to_scene(&self)

This method is called when a resource with [resource_local_to_scene][Self::resource_local_to_scene] enabled is loaded from a PackedScene instantiation. Its behavior can be customized by overriding [_setup_local_to_scene][Self::_setup_local_to_scene] from script. For most resources, this method performs no base logic. ViewportTexture performs custom logic to properly set the proxy texture and flags in the local viewport.

source

pub fn take_over_path(&self, path: impl Into<GodotString>)

Sets the path of the resource, potentially overriding an existing cache entry for this path. This differs from setting [resource_path][Self::resource_path], as the latter would error out if another resource was already cached for the given path.

Methods from Deref<Target = Object>§

source

pub fn add_user_signal(
    &self,
    signal: impl Into<GodotString>,
    arguments: VariantArray
)

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 [enum Variant.Type]) entries.

Default Arguments
  • arguments - [ ]
source

pub unsafe fn call(
    &self,
    method: impl Into<GodotString>,
    varargs: &[Variant]
) -> Variant

Sample code is GDScript unless otherwise noted.

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:

call("set", "position", Vector2(42.0, 0.0))

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).

Safety

This function bypasses Rust’s static type checks (aliasing, thread boundaries, calls to free(), …).

source

pub unsafe fn call_deferred(
    &self,
    method: impl Into<GodotString>,
    varargs: &[Variant]
) -> Variant

Sample code is GDScript unless otherwise noted.

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:

call_deferred("set", "position", Vector2(42.0, 0.0))

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).

Safety

This function bypasses Rust’s static type checks (aliasing, thread boundaries, calls to free(), …).

source

pub unsafe fn callv(
    &self,
    method: impl Into<GodotString>,
    arg_array: VariantArray
) -> Variant

Sample code is GDScript unless otherwise noted.

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 Array.

callv("set", [ "position", Vector2(42.0, 0.0) ])
Safety

This function bypasses Rust’s static type checks (aliasing, thread boundaries, calls to free(), …).

source

pub fn can_translate_messages(&self) -> bool

Returns true if the object can translate strings. See set_message_translation and tr.

source

pub fn connect(
    &self,
    signal: impl Into<GodotString>,
    target: impl AsArg<Object>,
    method: impl Into<GodotString>,
    binds: VariantArray,
    flags: i64
) -> GodotResult

Sample code is GDScript unless otherwise noted.

Connects a signal to a method on a target object. Pass optional binds to the call as an Array of parameters. These parameters will be passed to the method after any parameter used in the call to emit_signal. Use flags to set deferred or one-shot connections. See ConnectFlags 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 is_connected to check for existing connections. If the target is destroyed in the game’s lifecycle, the connection will be lost. Examples:

connect("pressed", self, "_on_Button_pressed") # BaseButton signal
connect("text_entered", self, "_on_LineEdit_text_entered") # LineEdit signal
connect("hit", self, "_on_Player_hit", [ weapon_type, damage ]) # User-defined signal

An example of the relationship between binds passed to connect and parameters used when calling emit_signal:

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])
Default Arguments
  • binds - [ ]
  • flags - 0
source

pub fn disconnect(
    &self,
    signal: impl Into<GodotString>,
    target: impl AsArg<Object>,
    method: impl Into<GodotString>
)

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 is_connected to ensure that the connection exists.

source

pub fn emit_signal(
    &self,
    signal: impl Into<GodotString>,
    varargs: &[Variant]
) -> Variant

Sample code is GDScript unless otherwise noted.

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:

emit_signal("hit", weapon_type, damage)
emit_signal("game_over")
source

pub fn get(&self, property: impl Into<GodotString>) -> Variant

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).

source

pub fn get_class(&self) -> GodotString

Returns the object’s class as a String. See also is_class. 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.

source

pub fn get_incoming_connections(&self) -> VariantArray

Returns an Array of dictionaries with information about signals that are connected to the object. Each Dictionary 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.
source

pub fn get_indexed(&self, property: impl Into<NodePath>) -> Variant

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 NodePath 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 Node.get_node_and_resource instead.

source

pub fn get_instance_id(&self) -> i64

Returns the object’s unique instance ID. This ID can be saved in EncodedObjectAsID, and can be used to retrieve the object instance with [method @GDScript.instance_from_id].

source

pub fn get_meta(
    &self,
    name: impl Into<GodotString>,
    default: impl OwnedToVariant
) -> Variant

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).

Default Arguments
  • default - null
source

pub fn get_meta_list(&self) -> PoolArray<GodotString>

Returns the object’s metadata as a PoolStringArray.

source

pub fn get_method_list(&self) -> VariantArray

Returns the object’s methods and their signatures as an Array.

source

pub fn get_property_list(&self) -> VariantArray

Returns the object’s property list as an Array of dictionaries. Each property’s Dictionary contain at least name: String and type: int (see [enum Variant.Type]) entries. Optionally, it can also include hint: int (see [PropertyHint][PropertyHint]), hint_string: String, and usage: int (see [PropertyUsageFlags][PropertyUsageFlags]).

source

pub fn get_script(&self) -> Option<Ref<Reference, Shared>>

Returns the object’s Script instance, or null if none is assigned.

source

pub fn get_signal_connection_list(
    &self,
    signal: impl Into<GodotString>
) -> VariantArray

Returns an Array of connections for the given signal.

source

pub fn get_signal_list(&self) -> VariantArray

Returns the list of signals as an Array of dictionaries.

source

pub fn has_meta(&self, name: impl Into<GodotString>) -> bool

Returns true if a metadata entry is found with the given name.

source

pub fn has_method(&self, method: impl Into<GodotString>) -> bool

Returns true if the object contains the given method.

source

pub fn has_signal(&self, signal: impl Into<GodotString>) -> bool

Returns true if the given signal exists.

source

pub fn has_user_signal(&self, signal: impl Into<GodotString>) -> bool

Returns true if the given user-defined signal exists. Only signals added using add_user_signal are taken into account.

source

pub fn is_blocking_signals(&self) -> bool

Returns true if signal emission blocking is enabled.

source

pub fn is_class(&self, class: impl Into<GodotString>) -> bool

Returns true if the object inherits from the given class. See also get_class. Note: is_class does not take class_name declarations into account. If the object has a class_name defined, is_class will return false for that name.

source

pub fn is_connected(
    &self,
    signal: impl Into<GodotString>,
    target: impl AsArg<Object>,
    method: impl Into<GodotString>
) -> bool

Returns true if a connection exists for a given signal, target, and method.

source

pub fn is_queued_for_deletion(&self) -> bool

Returns true if the Node.queue_free method was called for the object.

source

pub fn notification(&self, what: i64, reversed: bool)

Send a given notification to the object, which will also trigger a call to the [_notification][Self::_notification] method of all classes that the object inherits from. If reversed is true, [_notification][Self::_notification] is called first on the object’s own class, and then up to its successive parent classes. If reversed is false, [_notification][Self::_notification] is called first on the highest ancestor (Object itself), and then down to its successive inheriting classes.

Default Arguments
  • reversed - false
source

pub fn property_list_changed_notify(&self)

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.

source

pub fn remove_meta(&self, name: impl Into<GodotString>)

Removes a given entry from the object’s metadata. See also set_meta.

source

pub fn set(&self, property: impl Into<GodotString>, value: impl OwnedToVariant)

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).

source

pub fn set_block_signals(&self, enable: bool)

If set to true, signal emission is blocked.

source

pub fn set_deferred(
    &self,
    property: impl Into<GodotString>,
    value: impl OwnedToVariant
)

Assigns a new value to the given property, after the current frame’s physics step. This is equivalent to calling set 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).

source

pub fn set_indexed(
    &self,
    property: impl Into<NodePath>,
    value: impl OwnedToVariant
)

Sample code is GDScript unless otherwise noted.

Assigns a new value to the property identified by the NodePath. 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)
source

pub fn set_message_translation(&self, enable: bool)

Defines whether the object can translate strings (with calls to tr). Enabled by default.

source

pub fn set_meta(&self, name: impl Into<GodotString>, value: impl OwnedToVariant)

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 remove_meta. 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".

source

pub fn set_script(&self, script: impl AsArg<Reference>)

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 [_init][Self::_init] method will be called.

source

pub fn to_string(&self) -> GodotString

Returns a String representing the object. If not overridden, defaults to "[ClassName:RID]". Override the method [_to_string][Self::_to_string] to customize the String representation.

source

pub fn tr(&self, message: impl Into<GodotString>) -> GodotString

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 message unchanged. See set_message_translation.

Trait Implementations§

source§

impl Debug for Script

source§

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

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

impl Deref for Script

§

type Target = Resource

The resulting type after dereferencing.
source§

fn deref(&self) -> &Resource

Dereferences the value.
source§

impl DerefMut for Script

source§

fn deref_mut(&mut self) -> &mut Resource

Mutably dereferences the value.
source§

impl GodotObject for Script

§

type Memory = RefCounted

The memory management kind of this type. This modifies the behavior of the Ref smart pointer. See its type-level documentation for more information.
source§

fn class_name() -> &'static str

source§

fn null() -> Null<Self>

Creates an explicitly null reference of Self as a method argument. This makes type inference easier for the compiler compared to Option.
source§

fn cast<T>(&self) -> Option<&T>where
    T: GodotObject + SubClass<Self>,

Performs a dynamic reference downcast to target type. Read more
source§

fn upcast<T>(&self) -> &Twhere
    T: GodotObject,
    Self: SubClass<T>,

Performs a static reference upcast to a supertype that is guaranteed to be valid. Read more
source§

unsafe fn assume_shared(&self) -> Ref<Self, Shared>where
    Self: Sized,

Creates a persistent reference to the same Godot object with shared thread access. Read more
source§

unsafe fn assume_thread_local(&self) -> Ref<Self, ThreadLocal>where
    Self: Sized + GodotObject<Memory = RefCounted>,

Creates a persistent reference to the same Godot object with thread-local thread access. Read more
source§

unsafe fn assume_unique(&self) -> Ref<Self, Unique>where
    Self: Sized,

Creates a persistent reference to the same Godot object with unique access. Read more
source§

unsafe fn try_from_instance_id<'a>(id: i64) -> Option<TRef<'a, Self, Shared>>

Recovers a instance ID previously returned by Object::get_instance_id if the object is still alive. See also TRef::try_from_instance_id. Read more
source§

unsafe fn from_instance_id<'a>(id: i64) -> TRef<'a, Self, Shared>

Recovers a instance ID previously returned by Object::get_instance_id if the object is still alive, and panics otherwise. This does NOT guarantee that the resulting reference is safe to use. Read more
source§

impl Sealed for Script

source§

impl SubClass<Object> for Script

source§

impl SubClass<Reference> for Script

source§

impl SubClass<Resource> for Script

source§

impl SubClass<Script> for GDScript

source§

impl SubClass<Script> for NativeScript

source§

impl SubClass<Script> for PluginScript

source§

impl SubClass<Script> for VisualScript

Auto Trait Implementations§

§

impl RefUnwindSafe for Script

§

impl !Send for Script

§

impl !Sync for Script

§

impl Unpin for Script

§

impl UnwindSafe for Script

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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, U> TryFrom<U> for Twhere
    U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

impl<T> SubClass<T> for Twhere
    T: GodotObject,