Struct Executor

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

Executor resource to run scripts.

Implementations§

Source§

impl Executor

Source

pub fn insert_module(&mut self, module: Module) -> Result<()>

Insert a new module in the rune context Usage:

fn load_module(
    mut executor: ResMut<bevy_scripting_rune::Executor>,
)
{
    let module = rune::Module::with_crate("hello").unwrap();
    // Check rune documentation or our example on how to setup a rune module.
    executor.insert_module(module).unwrap();
}
Examples found in repository?
examples/example.rs (line 45)
33fn setup(
34    asset_server: Res<AssetServer>,
35    mut my_resource: ResMut<MyResource>,
36    mut executor: ResMut<bevy_scripting_rune::Executor>,
37) {
38    // Setup the module
39    let mut module = rune::Module::with_crate("example").unwrap();
40    module.ty::<MyStructure>().unwrap();
41    module.function_meta(MyStructure::update_value).unwrap();
42    module.function_meta(MyStructure::get_value).unwrap();
43
44    // Insert the module in the executor
45    executor.insert_module(module).unwrap();
46
47    // Load script asset
48    my_resource.script_handle = asset_server.load("example.rn");
49}
Source

pub fn load_script(&mut self, script: &Script) -> Result<u32>

Load a script, either from a loaded asset or inlined.

Usage:

fn run_script(
    mut executor: ResMut<bevy_scripting_rune::Executor>,
)
{
    executor.load_script(&Script::memory("pub fn plus(a, b) { a + b }").unwrap());
    executor.build().unwrap();
    let r = executor.call::<i32>("plus", (38, 4)).unwrap();
}
Examples found in repository?
examples/example.rs (line 63)
52fn compile_script(
53    mut ev_asset: EventReader<AssetEvent<bevy_scripting_rune::Script>>,
54    script_assets: Res<Assets<bevy_scripting_rune::Script>>,
55    mut executor: ResMut<bevy_scripting_rune::Executor>,
56    mut next_state: ResMut<NextState<States>>,
57) {
58    for ev in ev_asset.read() {
59        match ev {
60            AssetEvent::LoadedWithDependencies { id } => {
61                // Retrieve the script and compile it
62                let script = script_assets.get(*id).unwrap();
63                executor.load_script(script).unwrap();
64                executor.build().unwrap();
65                // Move to the running state
66                next_state.set(States::Running);
67            }
68            _ => {}
69        }
70    }
71}
Source

pub fn unload_script(&mut self, script_id: &u32) -> Result<()>

Unload a script that was loaded with load_script, using the id that was returned after the call to load_script.

Source

pub fn build(&mut self) -> Result<()>

Build the virtual machine context. It needs to be called after adding a module or loading a script.

Examples found in repository?
examples/example.rs (line 64)
52fn compile_script(
53    mut ev_asset: EventReader<AssetEvent<bevy_scripting_rune::Script>>,
54    script_assets: Res<Assets<bevy_scripting_rune::Script>>,
55    mut executor: ResMut<bevy_scripting_rune::Executor>,
56    mut next_state: ResMut<NextState<States>>,
57) {
58    for ev in ev_asset.read() {
59        match ev {
60            AssetEvent::LoadedWithDependencies { id } => {
61                // Retrieve the script and compile it
62                let script = script_assets.get(*id).unwrap();
63                executor.load_script(script).unwrap();
64                executor.build().unwrap();
65                // Move to the running state
66                next_state.set(States::Running);
67            }
68            _ => {}
69        }
70    }
71}
Source

pub fn call<T>(&self, name: impl AsRef<str>, args: impl Args) -> Result<T>
where T: FromValue,

Call a rune function.

Usage:

fn run_script(
    mut executor: ResMut<bevy_scripting_rune::Executor>,
)
{
    let r = executor.call::<i32>("plus", (38, 4)).unwrap();
}
Examples found in repository?
examples/example.rs (lines 76-84)
73fn run(my_resource: ResMut<MyResource>, executor: Res<bevy_scripting_rune::Executor>) {
74    // Call the update_value function from the script
75    executor
76        .call::<()>(
77            "update_value",
78            (
79                MyStructure {
80                    value: my_resource.value.clone(),
81                },
82                4.0,
83            ),
84        )
85        .unwrap();
86    // Display the result
87    println!("my_structure.value = {}", my_resource.value.lock().unwrap());
88}

Trait Implementations§

Source§

impl Default for Executor

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Resource for Executor
where Self: Send + Sync + 'static,

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, U> AsBindGroupShaderType<U> for T
where U: ShaderType, &'a T: for<'a> Into<U>,

Source§

fn as_bind_group_shader_type(&self, _images: &RenderAssets<GpuImage>) -> U

Return the T ShaderType for self. When used in AsBindGroup derives, it is safe to assume that all images in self exist.
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> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Converts Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Converts Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Converts &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Converts &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSend for T
where T: Any + Send,

Source§

fn into_any_send(self: Box<T>) -> Box<dyn Any + Send>

Converts Box<Trait> (where Trait: DowncastSend) to Box<dyn Any + Send>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<S> FromSample<S> for S

Source§

fn from_sample_(s: S) -> S

Source§

impl<T> FromWorld for T
where T: Default,

Source§

fn from_world(_world: &mut World) -> T

Creates Self using default().

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T, U> ToSample<U> for T
where U: FromSample<T>,

Source§

fn to_sample_(self) -> U

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> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ConditionalSend for T
where T: Send,

Source§

impl<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,

Source§

impl<T> Settings for T
where T: 'static + Send + Sync,

Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,