From ab8d61bf581a0a906fbf66a3f907a8becadbaa7d Mon Sep 17 00:00:00 2001 From: Dansion Date: Wed, 10 Jul 2024 17:36:07 +0200 Subject: [PATCH 1/7] Added scriptiing api --- src/lib.rs | 2 ++ src/plugin_builder.rs | 55 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 src/plugin_builder.rs diff --git a/src/lib.rs b/src/lib.rs index a5e2217d..fb932fc2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -210,11 +210,13 @@ mod assets; mod callback; mod components; +mod plugin_builder; mod promise; mod systems; pub mod runtimes; +pub use crate::plugin_builder::ScriptingApiBuilder; pub use crate::components::Script; use assets::GetExtensions; use promise::Promise; diff --git a/src/plugin_builder.rs b/src/plugin_builder.rs new file mode 100644 index 00000000..b123560c --- /dev/null +++ b/src/plugin_builder.rs @@ -0,0 +1,55 @@ +use std::{marker::PhantomData, sync::{Arc, Mutex}}; +use bevy::{app::App, prelude::World}; + +use crate::{callback::{Callback, IntoCallbackSystem}, Callbacks, Runtime, ScriptingRuntimeBuilder}; + +pub struct ApiBuilder<'a, R: Runtime> { + _phantom_data: PhantomData, + world: &'a mut World, +} + +impl<'a, R: Runtime> ApiBuilder<'a, R> { + fn new(world: &'a mut World) -> Self { + Self { + _phantom_data: PhantomData, + world, + } + } + + /// Registers a function for calling from within a script. + /// Provided function needs to be a valid bevy system and its + /// arguments and return value need to be convertible to runtime + /// value types. + pub fn add_function( + self, + name: String, + fun: impl IntoCallbackSystem, + ) -> Self { + let system = fun.into_callback_system(self.world); + + let mut callbacks_resource = self.world.resource_mut::>(); + + callbacks_resource.uninitialized_callbacks.push(Callback { + name, + system: Arc::new(Mutex::new(system)), + calls: Arc::new(Mutex::new(vec![])), + }); + + self + } +} + +pub trait ScriptingApiBuilder { + fn add_scripting_api(&mut self, f: impl Fn(ScriptingRuntimeBuilder)) -> &mut Self; +} + +impl ScriptingApiBuilder for App { + fn add_scripting_api(&mut self, f: impl Fn(ScriptingRuntimeBuilder)) -> &mut Self { + let runtime = ScriptingRuntimeBuilder::::new(&mut self.world); + + f(runtime); + + self + } +} + From fdfe21ac45e24a0d0a4c362d9b9b84adf06759f3 Mon Sep 17 00:00:00 2001 From: Dansion Date: Wed, 10 Jul 2024 17:36:07 +0200 Subject: [PATCH 2/7] Added scriptiing api --- src/lib.rs | 2 ++ src/plugin_builder.rs | 55 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 src/plugin_builder.rs diff --git a/src/lib.rs b/src/lib.rs index efa1af58..33a40e52 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -215,11 +215,13 @@ mod assets; mod callback; mod components; +mod plugin_builder; mod promise; mod systems; pub mod runtimes; +pub use crate::plugin_builder::ScriptingApiBuilder; pub use crate::components::Script; use assets::GetExtensions; use promise::Promise; diff --git a/src/plugin_builder.rs b/src/plugin_builder.rs new file mode 100644 index 00000000..b123560c --- /dev/null +++ b/src/plugin_builder.rs @@ -0,0 +1,55 @@ +use std::{marker::PhantomData, sync::{Arc, Mutex}}; +use bevy::{app::App, prelude::World}; + +use crate::{callback::{Callback, IntoCallbackSystem}, Callbacks, Runtime, ScriptingRuntimeBuilder}; + +pub struct ApiBuilder<'a, R: Runtime> { + _phantom_data: PhantomData, + world: &'a mut World, +} + +impl<'a, R: Runtime> ApiBuilder<'a, R> { + fn new(world: &'a mut World) -> Self { + Self { + _phantom_data: PhantomData, + world, + } + } + + /// Registers a function for calling from within a script. + /// Provided function needs to be a valid bevy system and its + /// arguments and return value need to be convertible to runtime + /// value types. + pub fn add_function( + self, + name: String, + fun: impl IntoCallbackSystem, + ) -> Self { + let system = fun.into_callback_system(self.world); + + let mut callbacks_resource = self.world.resource_mut::>(); + + callbacks_resource.uninitialized_callbacks.push(Callback { + name, + system: Arc::new(Mutex::new(system)), + calls: Arc::new(Mutex::new(vec![])), + }); + + self + } +} + +pub trait ScriptingApiBuilder { + fn add_scripting_api(&mut self, f: impl Fn(ScriptingRuntimeBuilder)) -> &mut Self; +} + +impl ScriptingApiBuilder for App { + fn add_scripting_api(&mut self, f: impl Fn(ScriptingRuntimeBuilder)) -> &mut Self { + let runtime = ScriptingRuntimeBuilder::::new(&mut self.world); + + f(runtime); + + self + } +} + From db4fcc253b0e959109ac4e122f01f2c1b12ea26f Mon Sep 17 00:00:00 2001 From: Dansion Date: Thu, 11 Jul 2024 23:11:45 +0200 Subject: [PATCH 3/7] Work in progress - Merged add_scripting_api into the BuildScriptingRuntime - Added an example - Working on documentation --- Cargo.toml | 4 ++ .../lua/multiple_plugins_plugin_a.lua | 1 + .../lua/multiple_plugins_plugin_b.lua | 1 + examples/lua/multiple_plugins.rs | 69 +++++++++++++++++++ src/lib.rs | 17 ++++- src/plugin_builder.rs | 55 --------------- 6 files changed, 90 insertions(+), 57 deletions(-) create mode 100644 assets/examples/lua/multiple_plugins_plugin_a.lua create mode 100644 assets/examples/lua/multiple_plugins_plugin_b.lua create mode 100644 examples/lua/multiple_plugins.rs delete mode 100644 src/plugin_builder.rs diff --git a/Cargo.toml b/Cargo.toml index d0c03c11..3c97f9e6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -93,6 +93,10 @@ path = "examples/lua/function_params.rs" name = "hello_world_lua" path = "examples/lua/hello_world.rs" +[[example]] +name = "multiple_plugins" +path = "examples/lua/multiple_plugins.rs" + [[example]] name = "non_closure_system_lua" path = "examples/lua/non_closure_system.rs" diff --git a/assets/examples/lua/multiple_plugins_plugin_a.lua b/assets/examples/lua/multiple_plugins_plugin_a.lua new file mode 100644 index 00000000..7befdb5c --- /dev/null +++ b/assets/examples/lua/multiple_plugins_plugin_a.lua @@ -0,0 +1 @@ +hello_from_plugin_a() \ No newline at end of file diff --git a/assets/examples/lua/multiple_plugins_plugin_b.lua b/assets/examples/lua/multiple_plugins_plugin_b.lua new file mode 100644 index 00000000..85a63e95 --- /dev/null +++ b/assets/examples/lua/multiple_plugins_plugin_b.lua @@ -0,0 +1 @@ +hello_from_plugin_b_with_parameters("hello", 42) \ No newline at end of file diff --git a/examples/lua/multiple_plugins.rs b/examples/lua/multiple_plugins.rs new file mode 100644 index 00000000..956cd4d5 --- /dev/null +++ b/examples/lua/multiple_plugins.rs @@ -0,0 +1,69 @@ +use bevy::prelude::*; +use bevy_scriptum::prelude::*; +use bevy_scriptum::runtimes::lua::prelude::*; + +// Plugin A +struct PluginA; +impl Plugin for PluginA { + fn build(&self, app: &mut App) { + app + .add_scripting_api::(|runtime| { + runtime.add_function( + String::from("hello_from_plugin_a"), + || { + info!("Hello from Plugin A"); + } + ); + }) + .add_systems(Startup, plugin_a_startup); + } +} + +fn plugin_a_startup(mut commands: Commands, assets_server: Res) { + commands.spawn(Script::::new( + assets_server.load("examples/lua/multiple_plugins_plugin_a.lua"), + )); +} + +// Plugin B +struct PluginB; +impl Plugin for PluginB { + fn build(&self, app: &mut App) { + app + .add_scripting_api::(|runtime| { + runtime.add_function(String::from("hello_from_plugin_b_with_parameters"), hello_from_b); + }) + .add_systems(Startup, plugin_b_startup); + } +} + +fn plugin_b_startup(mut commands: Commands, assets_server: Res) { + commands.spawn(Script::::new( + assets_server.load("examples/lua/multiple_plugins_plugin_b.lua"), + )); +} + +fn hello_from_b(In((text, x)) : In<(String, i32)>) { + info!("{} from Plugin B: {}", text, x); +} + +// Main +fn main() { + App::new() + .add_plugins(DefaultPlugins) + .add_scripting::(|runtime| { + runtime.add_function(String::from("hello_bevy"), || { + info!("hello bevy, called from script"); + }); + }) + .add_systems(Startup, main_startup) + .add_plugins(PluginA) + .add_plugins(PluginB) + .run(); +} + +fn main_startup(mut commands: Commands, assets_server: Res) { + commands.spawn(Script::::new( + assets_server.load("examples/lua/hello_world.lua"), + )); +} diff --git a/src/lib.rs b/src/lib.rs index 33a40e52..6af9a1e3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -215,13 +215,11 @@ mod assets; mod callback; mod components; -mod plugin_builder; mod promise; mod systems; pub mod runtimes; -pub use crate::plugin_builder::ScriptingApiBuilder; pub use crate::components::Script; use assets::GetExtensions; use promise::Promise; @@ -333,6 +331,9 @@ pub trait BuildScriptingRuntime { /// Returns a "runtime" type than can be used to setup scripting runtime( /// add scripting functions etc.). fn add_scripting(&mut self, f: impl Fn(ScriptingRuntimeBuilder)) -> &mut Self; + + /// Returns a "runtime" type that can be used to add additional scripting functions from plugins etc. + fn add_scripting_api(&mut self, f: impl Fn(ScriptingRuntimeBuilder)) -> &mut Self; } pub struct ScriptingRuntimeBuilder<'a, R: Runtime> { @@ -404,6 +405,18 @@ impl BuildScriptingRuntime for App { self } + + /// Adds a way to add additional accesspoints to the scripting runtime. For example from plugins to add + /// for example additional lua functions to the runtime. + /// + /// Be careful with calling this though, make sure that the `add_scripting` call is already called before calling this function. + fn add_scripting_api(&mut self, f: impl Fn(ScriptingRuntimeBuilder)) -> &mut Self { + let runtime = ScriptingRuntimeBuilder::::new(self.world_mut()); + + f(runtime); + + self + } } /// A resource that stores all the callbacks that were registered using [AddScriptFunctionAppExt::add_function]. diff --git a/src/plugin_builder.rs b/src/plugin_builder.rs deleted file mode 100644 index b123560c..00000000 --- a/src/plugin_builder.rs +++ /dev/null @@ -1,55 +0,0 @@ -use std::{marker::PhantomData, sync::{Arc, Mutex}}; -use bevy::{app::App, prelude::World}; - -use crate::{callback::{Callback, IntoCallbackSystem}, Callbacks, Runtime, ScriptingRuntimeBuilder}; - -pub struct ApiBuilder<'a, R: Runtime> { - _phantom_data: PhantomData, - world: &'a mut World, -} - -impl<'a, R: Runtime> ApiBuilder<'a, R> { - fn new(world: &'a mut World) -> Self { - Self { - _phantom_data: PhantomData, - world, - } - } - - /// Registers a function for calling from within a script. - /// Provided function needs to be a valid bevy system and its - /// arguments and return value need to be convertible to runtime - /// value types. - pub fn add_function( - self, - name: String, - fun: impl IntoCallbackSystem, - ) -> Self { - let system = fun.into_callback_system(self.world); - - let mut callbacks_resource = self.world.resource_mut::>(); - - callbacks_resource.uninitialized_callbacks.push(Callback { - name, - system: Arc::new(Mutex::new(system)), - calls: Arc::new(Mutex::new(vec![])), - }); - - self - } -} - -pub trait ScriptingApiBuilder { - fn add_scripting_api(&mut self, f: impl Fn(ScriptingRuntimeBuilder)) -> &mut Self; -} - -impl ScriptingApiBuilder for App { - fn add_scripting_api(&mut self, f: impl Fn(ScriptingRuntimeBuilder)) -> &mut Self { - let runtime = ScriptingRuntimeBuilder::::new(&mut self.world); - - f(runtime); - - self - } -} - From 951cb6df88eaf86405499c720db0940d2fe70d11 Mon Sep 17 00:00:00 2001 From: Dansion Date: Thu, 11 Jul 2024 23:16:54 +0200 Subject: [PATCH 4/7] Cleaned up rebased code --- examples/lua/multiple_plugins.rs | 32 +++++++++---------- src/lib.rs | 19 +++++++---- src/plugin_builder.rs | 55 -------------------------------- 3 files changed, 27 insertions(+), 79 deletions(-) delete mode 100644 src/plugin_builder.rs diff --git a/examples/lua/multiple_plugins.rs b/examples/lua/multiple_plugins.rs index 956cd4d5..979bbb89 100644 --- a/examples/lua/multiple_plugins.rs +++ b/examples/lua/multiple_plugins.rs @@ -6,16 +6,12 @@ use bevy_scriptum::runtimes::lua::prelude::*; struct PluginA; impl Plugin for PluginA { fn build(&self, app: &mut App) { - app - .add_scripting_api::(|runtime| { - runtime.add_function( - String::from("hello_from_plugin_a"), - || { - info!("Hello from Plugin A"); - } - ); - }) - .add_systems(Startup, plugin_a_startup); + app.add_scripting_api::(|runtime| { + runtime.add_function(String::from("hello_from_plugin_a"), || { + info!("Hello from Plugin A"); + }); + }) + .add_systems(Startup, plugin_a_startup); } } @@ -23,17 +19,19 @@ fn plugin_a_startup(mut commands: Commands, assets_server: Res) { commands.spawn(Script::::new( assets_server.load("examples/lua/multiple_plugins_plugin_a.lua"), )); -} +} // Plugin B struct PluginB; impl Plugin for PluginB { fn build(&self, app: &mut App) { - app - .add_scripting_api::(|runtime| { - runtime.add_function(String::from("hello_from_plugin_b_with_parameters"), hello_from_b); - }) - .add_systems(Startup, plugin_b_startup); + app.add_scripting_api::(|runtime| { + runtime.add_function( + String::from("hello_from_plugin_b_with_parameters"), + hello_from_b, + ); + }) + .add_systems(Startup, plugin_b_startup); } } @@ -43,7 +41,7 @@ fn plugin_b_startup(mut commands: Commands, assets_server: Res) { )); } -fn hello_from_b(In((text, x)) : In<(String, i32)>) { +fn hello_from_b(In((text, x)): In<(String, i32)>) { info!("{} from Plugin B: {}", text, x); } diff --git a/src/lib.rs b/src/lib.rs index 1d93647c..2b4db781 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -215,14 +215,13 @@ mod assets; mod callback; mod components; -mod plugin_builder; mod promise; mod systems; pub mod runtimes; -pub use crate::plugin_builder::ScriptingApiBuilder; pub use crate::components::Script; +pub use crate::plugin_builder::ScriptingApiBuilder; use assets::GetExtensions; use promise::Promise; @@ -335,7 +334,10 @@ pub trait BuildScriptingRuntime { fn add_scripting(&mut self, f: impl Fn(ScriptingRuntimeBuilder)) -> &mut Self; /// Returns a "runtime" type that can be used to add additional scripting functions from plugins etc. - fn add_scripting_api(&mut self, f: impl Fn(ScriptingRuntimeBuilder)) -> &mut Self; + fn add_scripting_api( + &mut self, + f: impl Fn(ScriptingRuntimeBuilder), + ) -> &mut Self; } pub struct ScriptingRuntimeBuilder<'a, R: Runtime> { @@ -408,13 +410,16 @@ impl BuildScriptingRuntime for App { self } - /// Adds a way to add additional accesspoints to the scripting runtime. For example from plugins to add + /// Adds a way to add additional accesspoints to the scripting runtime. For example from plugins to add /// for example additional lua functions to the runtime. - /// + /// /// Be careful with calling this though, make sure that the `add_scripting` call is already called before calling this function. - fn add_scripting_api(&mut self, f: impl Fn(ScriptingRuntimeBuilder)) -> &mut Self { + fn add_scripting_api( + &mut self, + f: impl Fn(ScriptingRuntimeBuilder), + ) -> &mut Self { let runtime = ScriptingRuntimeBuilder::::new(self.world_mut()); - + f(runtime); self diff --git a/src/plugin_builder.rs b/src/plugin_builder.rs deleted file mode 100644 index b123560c..00000000 --- a/src/plugin_builder.rs +++ /dev/null @@ -1,55 +0,0 @@ -use std::{marker::PhantomData, sync::{Arc, Mutex}}; -use bevy::{app::App, prelude::World}; - -use crate::{callback::{Callback, IntoCallbackSystem}, Callbacks, Runtime, ScriptingRuntimeBuilder}; - -pub struct ApiBuilder<'a, R: Runtime> { - _phantom_data: PhantomData, - world: &'a mut World, -} - -impl<'a, R: Runtime> ApiBuilder<'a, R> { - fn new(world: &'a mut World) -> Self { - Self { - _phantom_data: PhantomData, - world, - } - } - - /// Registers a function for calling from within a script. - /// Provided function needs to be a valid bevy system and its - /// arguments and return value need to be convertible to runtime - /// value types. - pub fn add_function( - self, - name: String, - fun: impl IntoCallbackSystem, - ) -> Self { - let system = fun.into_callback_system(self.world); - - let mut callbacks_resource = self.world.resource_mut::>(); - - callbacks_resource.uninitialized_callbacks.push(Callback { - name, - system: Arc::new(Mutex::new(system)), - calls: Arc::new(Mutex::new(vec![])), - }); - - self - } -} - -pub trait ScriptingApiBuilder { - fn add_scripting_api(&mut self, f: impl Fn(ScriptingRuntimeBuilder)) -> &mut Self; -} - -impl ScriptingApiBuilder for App { - fn add_scripting_api(&mut self, f: impl Fn(ScriptingRuntimeBuilder)) -> &mut Self { - let runtime = ScriptingRuntimeBuilder::::new(&mut self.world); - - f(runtime); - - self - } -} - From 548f6ea7731086c3e0b3712f7b318a61bbc4e7ae Mon Sep 17 00:00:00 2001 From: Dansion Date: Thu, 11 Jul 2024 23:18:37 +0200 Subject: [PATCH 5/7] fixed non-existing mod declaration --- src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 2b4db781..3e085782 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -221,7 +221,6 @@ mod systems; pub mod runtimes; pub use crate::components::Script; -pub use crate::plugin_builder::ScriptingApiBuilder; use assets::GetExtensions; use promise::Promise; From 5f0c22192b33f74aeb8d6d6b17dfbefa8eef027d Mon Sep 17 00:00:00 2001 From: Dansion Date: Fri, 12 Jul 2024 12:52:16 +0200 Subject: [PATCH 6/7] Finalized documentation and examples --- Cargo.toml | 6 +- .../rhai/multiple_plugins_plugin_a.rhai | 1 + .../rhai/multiple_plugins_plugin_b.rhai | 1 + book/src/introduction.md | 30 ++++++++ examples/rhai/multiple_plugins.rs | 68 +++++++++++++++++++ src/lib.rs | 32 ++++++++- 6 files changed, 136 insertions(+), 2 deletions(-) create mode 100644 assets/examples/rhai/multiple_plugins_plugin_a.rhai create mode 100644 assets/examples/rhai/multiple_plugins_plugin_b.rhai create mode 100644 examples/rhai/multiple_plugins.rs diff --git a/Cargo.toml b/Cargo.toml index 3c97f9e6..9ab19bdb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -53,6 +53,10 @@ path = "examples/rhai/function_params.rs" name = "hello_world_rhai" path = "examples/rhai/hello_world.rs" +[[example]] +name = "multiple_plugins_rhai" +path = "examples/rhai/multiple_plugins.rs" + [[example]] name = "non_closure_system_rhai" path = "examples/rhai/non_closure_system.rs" @@ -94,7 +98,7 @@ name = "hello_world_lua" path = "examples/lua/hello_world.rs" [[example]] -name = "multiple_plugins" +name = "multiple_plugins_lua" path = "examples/lua/multiple_plugins.rs" [[example]] diff --git a/assets/examples/rhai/multiple_plugins_plugin_a.rhai b/assets/examples/rhai/multiple_plugins_plugin_a.rhai new file mode 100644 index 00000000..9d7bfe80 --- /dev/null +++ b/assets/examples/rhai/multiple_plugins_plugin_a.rhai @@ -0,0 +1 @@ +hello_from_plugin_a(); \ No newline at end of file diff --git a/assets/examples/rhai/multiple_plugins_plugin_b.rhai b/assets/examples/rhai/multiple_plugins_plugin_b.rhai new file mode 100644 index 00000000..dcb9e339 --- /dev/null +++ b/assets/examples/rhai/multiple_plugins_plugin_b.rhai @@ -0,0 +1 @@ +hello_from_plugin_b_with_parameters("hello", 42); \ No newline at end of file diff --git a/book/src/introduction.md b/book/src/introduction.md index 05babec4..eade7af6 100644 --- a/book/src/introduction.md +++ b/book/src/introduction.md @@ -88,6 +88,36 @@ which you can then call in your script like this: fun_with_string_param("Hello world!") ``` +It is also possible to split the definition of your callback functions up over multiple plugins. This enables you to split up your code by subject and keep the main initialization light and clean. +This can be accomplished by using `add_scripting_api`. Be careful though, `add_scripting` has to be called before adding plugins. +```rust +use bevy::prelude::*; +use bevy_scriptum::prelude::*; +use bevy_scriptum::runtimes::lua::prelude::*; + +struct MyPlugin; +impl Plugin for MyPlugin { + fn build(&self, app: &mut App) { + app.add_scripting_api::(|runtime| { + runtime.add_function(String::from("hello_from_my_plugin"), || { + info!("Hello from MyPlugin"); + }); + }); + } +} + +// Main +fn main() { + App::new() + .add_plugins(DefaultPlugins) + .add_scripting::(|_| { + // nice and clean + }) + .add_plugins(MyPlugin) + .run(); +} +``` + ### Usage Add the following to your `Cargo.toml`: diff --git a/examples/rhai/multiple_plugins.rs b/examples/rhai/multiple_plugins.rs new file mode 100644 index 00000000..612334d3 --- /dev/null +++ b/examples/rhai/multiple_plugins.rs @@ -0,0 +1,68 @@ +use bevy::prelude::*; +use bevy_scriptum::prelude::*; +use bevy_scriptum::runtimes::rhai::prelude::*; +use rhai::ImmutableString; + +// Plugin A +struct PluginA; +impl Plugin for PluginA { + fn build(&self, app: &mut App) { + app.add_scripting_api::(|runtime| { + runtime.add_function(String::from("hello_from_plugin_a"), || { + info!("Hello from Plugin A"); + }); + }) + .add_systems(Startup, plugin_a_startup); + } +} + +fn plugin_a_startup(mut commands: Commands, assets_server: Res) { + commands.spawn(Script::::new( + assets_server.load("examples/rhai/multiple_plugins_plugin_a.rhai"), + )); +} + +// Plugin B +struct PluginB; +impl Plugin for PluginB { + fn build(&self, app: &mut App) { + app.add_scripting_api::(|runtime| { + runtime.add_function( + String::from("hello_from_plugin_b_with_parameters"), + hello_from_b, + ); + }) + .add_systems(Startup, plugin_b_startup); + } +} + +fn plugin_b_startup(mut commands: Commands, assets_server: Res) { + commands.spawn(Script::::new( + assets_server.load("examples/rhai/multiple_plugins_plugin_b.rhai"), + )); +} + +fn hello_from_b(In((text, x)): In<(ImmutableString, i64)>) { + info!("{} from Plugin B: {}", text, x); +} + +// Main +fn main() { + App::new() + .add_plugins(DefaultPlugins) + .add_scripting::(|runtime| { + runtime.add_function(String::from("hello_bevy"), || { + info!("hello bevy, called from script"); + }); + }) + .add_systems(Startup, main_startup) + .add_plugins(PluginA) + .add_plugins(PluginB) + .run(); +} + +fn main_startup(mut commands: Commands, assets_server: Res) { + commands.spawn(Script::::new( + assets_server.load("examples/rhai/hello_world.rhai"), + )); +} diff --git a/src/lib.rs b/src/lib.rs index 3e085782..c83e65a8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -82,7 +82,37 @@ //! ```lua //! fun_with_string_param("Hello world!") //! ``` -//! +//!It is also possible to split the definition of your callback functions up over multiple plugins. This enables you to split up your code by subject and keep the main initialization light and clean. +//!This can be accomplished by using `add_scripting_api`. Be careful though, `add_scripting` has to be called before adding plugins. +//!```rust +//!use bevy::prelude::*; +//!use bevy_scriptum::prelude::*; +//!use bevy_scriptum::runtimes::lua::prelude::*; +//! +//!struct MyPlugin; +//!impl Plugin for MyPlugin { +//! fn build(&self, app: &mut App) { +//! app.add_scripting_api::(|runtime| { +//! runtime.add_function(String::from("hello_from_my_plugin"), || { +//! info!("Hello from MyPlugin"); +//! }); +//! }); +//! } +//!} +//! +//!// Main +//!fn main() { +//! App::new() +//! .add_plugins(DefaultPlugins) +//! .add_scripting::(|_| { +//! // nice and clean +//! }) +//! .add_plugins(MyPlugin) +//! .run(); +//!} +//!``` +//! +//! //! ## Usage //! //! Add the following to your `Cargo.toml`: From 03c98c2c7c6b8505fdd2d7856997d42604f139c5 Mon Sep 17 00:00:00 2001 From: Dansion Date: Sat, 13 Jul 2024 14:48:24 +0200 Subject: [PATCH 7/7] Fixed doctest --- src/lib.rs | 55 ++++++++++++++++++++++++++---------------------------- 1 file changed, 26 insertions(+), 29 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index c83e65a8..259b7ba7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -82,35 +82,32 @@ //! ```lua //! fun_with_string_param("Hello world!") //! ``` -//!It is also possible to split the definition of your callback functions up over multiple plugins. This enables you to split up your code by subject and keep the main initialization light and clean. -//!This can be accomplished by using `add_scripting_api`. Be careful though, `add_scripting` has to be called before adding plugins. -//!```rust -//!use bevy::prelude::*; -//!use bevy_scriptum::prelude::*; -//!use bevy_scriptum::runtimes::lua::prelude::*; -//! -//!struct MyPlugin; -//!impl Plugin for MyPlugin { -//! fn build(&self, app: &mut App) { -//! app.add_scripting_api::(|runtime| { -//! runtime.add_function(String::from("hello_from_my_plugin"), || { -//! info!("Hello from MyPlugin"); -//! }); -//! }); -//! } -//!} -//! -//!// Main -//!fn main() { -//! App::new() -//! .add_plugins(DefaultPlugins) -//! .add_scripting::(|_| { -//! // nice and clean -//! }) -//! .add_plugins(MyPlugin) -//! .run(); -//!} -//!``` +//! It is also possible to split the definition of your callback functions up over multiple plugins. This enables you to split up your code by subject and keep the main initialization light and clean. +//! This can be accomplished by using `add_scripting_api`. Be careful though, `add_scripting` has to be called before adding plugins. +//! ```rust +//! use bevy::prelude::*; +//! use bevy_scriptum::prelude::*; +//! use bevy_scriptum::runtimes::lua::prelude::*; +//! +//! struct MyPlugin; +//! impl Plugin for MyPlugin { +//! fn build(&self, app: &mut App) { +//! app.add_scripting_api::(|runtime| { +//! runtime.add_function(String::from("hello_from_my_plugin"), || { +//! info!("Hello from MyPlugin"); +//! }); +//! }); +//! } +//! } +//! +//! App::new() +//! .add_plugins(DefaultPlugins) +//! .add_scripting::(|_| { +//! // nice and clean +//! }) +//! .add_plugins(MyPlugin) +//! .run(); +//! ``` //! //! //! ## Usage