From 25e0d10250177ef320ec68383596773dc7c4a120 Mon Sep 17 00:00:00 2001 From: makspll Date: Sat, 1 Feb 2025 15:17:56 +0000 Subject: [PATCH 1/2] add more info about sending reflect types as payloads, streamline interface --- crates/bevy_mod_scripting_core/src/script.rs | 4 ++-- docs/src/Summary/running-scripts.md | 16 +++++++++++++--- examples/game_of_life.rs | 3 +-- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/crates/bevy_mod_scripting_core/src/script.rs b/crates/bevy_mod_scripting_core/src/script.rs index 81b1b3bf96..a4ffb9cc52 100644 --- a/crates/bevy_mod_scripting_core/src/script.rs +++ b/crates/bevy_mod_scripting_core/src/script.rs @@ -26,8 +26,8 @@ impl Deref for ScriptComponent { impl ScriptComponent { /// Creates a new [`ScriptComponent`] with the given ScriptID's - pub fn new(components: Vec) -> Self { - Self(components) + pub fn new, I: IntoIterator>(components: I) -> Self { + Self(components.into_iter().map(Into::into).collect()) } } diff --git a/docs/src/Summary/running-scripts.md b/docs/src/Summary/running-scripts.md index 13f08e0c61..1afb11acf5 100644 --- a/docs/src/Summary/running-scripts.md +++ b/docs/src/Summary/running-scripts.md @@ -22,19 +22,29 @@ Will print "hello from load time" when the script is loaded, and "hello from eve In order to trigger `on_event` you need to first define a label, then send an event containing the label: ```rust,ignore + +#[derive(Reflect)] +pub struct MyReflectType; + // define the label, you can define as many as you like here callback_labels!(OnEvent => "on_event"); // trigger the event -fn send_event(mut writer: EventWriter) { +fn send_event(mut writer: EventWriter, mut allocator: ResMut) { + + let allocator = allocator.write(); + let my_reflect_payload = ReflectReference::new_allocated(MyReflectType, &mut allocator); + writer.send(ScriptCallbackEvent::new_for_all( OnEvent, - vec![ScriptValue::Unit], + vec![my_reflect_payload.into()], )); } ``` -Note the second argument is the payload we are sending with the event, in this case we are sending an empty payload. +Note the second argument is the payload we are sending with the event, in this case we are sending an arbitrary reflect type `MyReflectType`. This can be any type you like, as long as it implements `Reflect`. + +Other variants of the `ScriptValue` enum are available for sending different types of data, such as `ScriptValue::Integer` for primtive, types. # Event Handlers diff --git a/examples/game_of_life.rs b/examples/game_of_life.rs index c8dde69bc3..5717a720f4 100644 --- a/examples/game_of_life.rs +++ b/examples/game_of_life.rs @@ -61,8 +61,7 @@ fn run_script_cmd( ); commands.spawn(ScriptComponent::new(vec![format!( "scripts/game_of_life.{language}" - ) - .into()])); + )])); } GameOfLifeCommand::Stop => { // we can simply drop the handle, or manually delete, I'll just drop the handle From f89b69f4c6d555d841192b8d024d95162af1610f Mon Sep 17 00:00:00 2001 From: makspll Date: Sat, 1 Feb 2025 15:30:21 +0000 Subject: [PATCH 2/2] add arg info to callback --- docs/src/Summary/running-scripts.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/src/Summary/running-scripts.md b/docs/src/Summary/running-scripts.md index 1afb11acf5..de174af077 100644 --- a/docs/src/Summary/running-scripts.md +++ b/docs/src/Summary/running-scripts.md @@ -13,8 +13,9 @@ Scripts can run logic either when loaded or when triggered by an event. For exam ```lua print("hello from load time") -function on_event() +function on_event(arg1) print("hello from event time") + print(arg1) end ```