Skip to main content

ExternObject

Trait ExternObject 

Source
pub trait ExternObject: Send + Sync {
    // Required methods
    fn type_name(&self) -> &str;
    fn get(&self, field: &str) -> Result<RuntimeValue, String>;
    fn set(&mut self, field: &str, value: RuntimeValue) -> Result<(), String>;

    // Provided methods
    fn display(&self) -> String { ... }
    fn fields(&self) -> Vec<String> { ... }
    fn cast(&self, target: &str) -> Result<RuntimeValue, String> { ... }
}
Expand description

A live game object exposed to Urd scripts.

Implement this trait for any host-side type (Godot node, ECS entity handle, asset reference, …) that you want scripts to interact with transparently.

§Contract

  • get / set should be cheap — they run inline during expression evaluation. Avoid blocking I/O.
  • Field names are stringly-typed on purpose — the set of fields can be dynamic (e.g. ECS components added at runtime).
  • Returning Err(message) from any method surfaces as a VmError at the script level; the message is shown to the script author.

Required Methods§

Source

fn type_name(&self) -> &str

The type name of this external object (e.g. "Node3D", "CharacterBody3D").

Used for error messages, debug output, and as the discriminator in match / pattern contexts if you choose to support that later.

Source

fn get(&self, field: &str) -> Result<RuntimeValue, String>

Read a field by name.

Return Ok(value) on success or Err(message) if the field does not exist or cannot be read.

Source

fn set(&mut self, field: &str, value: RuntimeValue) -> Result<(), String>

Write a field by name.

Return Ok(()) on success or Err(message) if the field does not exist, is read-only, or the value type is incompatible.

Provided Methods§

Source

fn display(&self) -> String

Human-readable string representation, used when the value appears in string interpolation ("Hello {player}") or to_string() method calls.

Source

fn fields(&self) -> Vec<String>

List the names of all readable fields.

Primarily useful for editor auto-complete and introspection from scripts. The default implementation returns an empty list.

Source

fn cast(&self, target: &str) -> Result<RuntimeValue, String>

Try to cast / convert this object into a plain RuntimeValue.

target is the type name the script asked for (e.g. "map", "str", "int"). Return Err if the cast is not supported.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§