From 41e07802bf51bf7c46f8f7e21d161ecc9ba9cb27 Mon Sep 17 00:00:00 2001 From: Joakker Date: Sun, 5 May 2024 17:36:51 -0400 Subject: [PATCH] Add dynamic query examples --- Cargo.toml | 10 +++++ assets/scripts/dynamic_queries.lua | 9 ++++ assets/scripts/dynamic_queries.rhai | 11 +++++ examples/lua/dynamic_queries.rs | 64 +++++++++++++++++++++++++++++ examples/rhai/dynamic_queries.rs | 63 ++++++++++++++++++++++++++++ 5 files changed, 157 insertions(+) create mode 100644 assets/scripts/dynamic_queries.lua create mode 100644 assets/scripts/dynamic_queries.rhai create mode 100644 examples/lua/dynamic_queries.rs create mode 100644 examples/rhai/dynamic_queries.rs diff --git a/Cargo.toml b/Cargo.toml index b1ae0117..aea1be69 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -136,6 +136,16 @@ name = "complex_game_loop_lua" path = "examples/lua/complex_game_loop.rs" required-features = ["lua54"] +[[example]] +name = "dynamic_queries_lua" +path = "examples/lua/dynamic_queries.rs" +required-features = ["lua54", "lua_script_api"] + +[[example]] +name = "dynamic_queries_rhai" +path = "examples/rhai/dynamic_queries.rs" +required-features = ["rhai", "rhai_script_api"] + [[example]] name = "game_of_life_lua" path = "examples/lua/game_of_life.rs" diff --git a/assets/scripts/dynamic_queries.lua b/assets/scripts/dynamic_queries.lua new file mode 100644 index 00000000..8128df28 --- /dev/null +++ b/assets/scripts/dynamic_queries.lua @@ -0,0 +1,9 @@ +function on_event() + local component_a = world:get_type_by_name("ComponentA") + local component_b = world:get_type_by_name("ComponentB") + local component_c = world:get_type_by_name("ComponentC") + + for entity, _ in world:query(component_a):with(component_b):without(component_c):iter() do + print(entity) + end +end diff --git a/assets/scripts/dynamic_queries.rhai b/assets/scripts/dynamic_queries.rhai new file mode 100644 index 00000000..73701d7b --- /dev/null +++ b/assets/scripts/dynamic_queries.rhai @@ -0,0 +1,11 @@ +fn on_event() { + let component_a = world.get_type_by_name("ComponentA"); + let component_b = world.get_type_by_name("ComponentB"); + let component_c = world.get_type_by_name("ComponentC"); + + // Use with_components/without_components, as the word `with` is + // reserved in rhai + for results in world.query([component_a]).with_components([component_b]).without_components([component_c]) { + print(results.Entity); + } +} diff --git a/examples/lua/dynamic_queries.rs b/examples/lua/dynamic_queries.rs new file mode 100644 index 00000000..9a07eb64 --- /dev/null +++ b/examples/lua/dynamic_queries.rs @@ -0,0 +1,64 @@ +use bevy::prelude::*; +use bevy_mod_scripting::prelude::*; + +#[derive(Component, Reflect)] +#[reflect(Component)] +pub struct ComponentA; + +#[derive(Component, Reflect)] +#[reflect(Component)] +pub struct ComponentB; + +#[derive(Component, Reflect)] +#[reflect(Component)] +pub struct ComponentC; + +fn main() { + let mut app = App::new(); + + app.add_plugins((DefaultPlugins, ScriptingPlugin)) + .register_type::() + .register_type::() + .register_type::() + .add_script_host::>(PostUpdate) + .add_script_handler::, 0, 0>(PostUpdate) + .add_api_provider::>(Box::new(LuaBevyAPIProvider)) + .add_api_provider::>(Box::new(LuaCoreBevyAPIProvider)) + .add_systems(Startup, (setup, apply_deferred, run).chain()) + .run(); +} + +fn setup(mut commands: Commands, asset_server: Res) { + commands.spawn((ComponentA,)); + commands.spawn((ComponentA, ComponentB)); + commands.spawn((ComponentA, ComponentC)); + commands.spawn((ComponentA, ComponentB, ComponentC)); + + commands.spawn((ComponentB,)); + commands.spawn((ComponentB, ComponentC)); + commands.spawn((ComponentB, ComponentA)); + commands.spawn((ComponentB, ComponentA, ComponentC)); + + commands.spawn((ComponentC,)); + commands.spawn((ComponentC, ComponentA)); + commands.spawn((ComponentC, ComponentB)); + commands.spawn((ComponentC, ComponentA, ComponentB)); + + let path = "scripts/dynamic_queries.lua"; + let handle = asset_server.load(path); + + commands.spawn(ScriptCollection:: { + scripts: vec![Script::new(path.into(), handle)], + }); +} + +fn run(mut events: PriorityEventWriter>) { + events.send( + LuaEvent { + hook_name: "on_event".into(), + args: (), + recipients: Recipients::All, + }, + 0, + ); +} diff --git a/examples/rhai/dynamic_queries.rs b/examples/rhai/dynamic_queries.rs new file mode 100644 index 00000000..887215d4 --- /dev/null +++ b/examples/rhai/dynamic_queries.rs @@ -0,0 +1,63 @@ +use bevy::prelude::*; +use bevy_mod_scripting::prelude::*; + +#[derive(Component, Reflect)] +#[reflect(Component)] +pub struct ComponentA; + +#[derive(Component, Reflect)] +#[reflect(Component)] +pub struct ComponentB; + +#[derive(Component, Reflect)] +#[reflect(Component)] +pub struct ComponentC; + +fn main() { + let mut app = App::new(); + + app.add_plugins((DefaultPlugins, ScriptingPlugin)) + .register_type::() + .register_type::() + .register_type::() + .add_script_host::>(PostUpdate) + .add_script_handler::, 0, 0>(PostUpdate) + .add_api_provider::>(Box::new(RhaiBevyAPIProvider)) + .add_systems(Startup, (setup, apply_deferred, run).chain()) + .run(); +} + +fn setup(mut commands: Commands, asset_server: Res) { + commands.spawn((ComponentA,)); + commands.spawn((ComponentA, ComponentB)); + commands.spawn((ComponentA, ComponentC)); + commands.spawn((ComponentA, ComponentB, ComponentC)); + + commands.spawn((ComponentB,)); + commands.spawn((ComponentB, ComponentC)); + commands.spawn((ComponentB, ComponentA)); + commands.spawn((ComponentB, ComponentA, ComponentC)); + + commands.spawn((ComponentC,)); + commands.spawn((ComponentC, ComponentA)); + commands.spawn((ComponentC, ComponentB)); + commands.spawn((ComponentC, ComponentA, ComponentB)); + + let path = "scripts/dynamic_queries.rhai"; + let handle = asset_server.load(path); + + commands.spawn(ScriptCollection:: { + scripts: vec![Script::new(path.into(), handle)], + }); +} + +fn run(mut events: PriorityEventWriter>) { + events.send( + RhaiEvent { + hook_name: "on_event".into(), + args: (), + recipients: Recipients::All, + }, + 0, + ); +}