pub struct Executor { /* private fields */ }
Expand description
Executor resource to run scripts.
Implementations§
Source§impl Executor
impl Executor
Sourcepub fn insert_module(&mut self, module: Module) -> Result<()>
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}
Sourcepub fn load_script(&mut self, script: &Script) -> Result<u32>
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}
Sourcepub fn unload_script(&mut self, script_id: &u32) -> Result<()>
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
.
Sourcepub fn build(&mut self) -> Result<()>
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}
Sourcepub fn call<T>(&self, name: impl AsRef<str>, args: impl Args) -> Result<T>where
T: FromValue,
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§
Auto Trait Implementations§
impl Freeze for Executor
impl !RefUnwindSafe for Executor
impl Send for Executor
impl Sync for Executor
impl Unpin for Executor
impl !UnwindSafe for Executor
Blanket Implementations§
Source§impl<T, U> AsBindGroupShaderType<U> for T
impl<T, U> AsBindGroupShaderType<U> for T
Source§fn as_bind_group_shader_type(&self, _images: &RenderAssets<GpuImage>) -> U
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
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>
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)
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)
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
impl<T> DowncastSend for T
Source§impl<S> FromSample<S> for S
impl<S> FromSample<S> for S
fn from_sample_(s: S) -> S
Source§impl<T> FromWorld for Twhere
T: Default,
impl<T> FromWorld for Twhere
T: Default,
Source§fn from_world(_world: &mut World) -> T
fn from_world(_world: &mut World) -> T
Creates Self
using default()
.
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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