Skip to content

Add dynamic query examples #120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 9 additions & 0 deletions assets/scripts/dynamic_queries.lua
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions assets/scripts/dynamic_queries.rhai
Original file line number Diff line number Diff line change
@@ -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);
}
}
64 changes: 64 additions & 0 deletions examples/lua/dynamic_queries.rs
Original file line number Diff line number Diff line change
@@ -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::<ComponentA>()
.register_type::<ComponentB>()
.register_type::<ComponentC>()
.add_script_host::<LuaScriptHost<()>>(PostUpdate)
.add_script_handler::<LuaScriptHost<()>, 0, 0>(PostUpdate)
.add_api_provider::<LuaScriptHost<()>>(Box::new(LuaBevyAPIProvider))
.add_api_provider::<LuaScriptHost<()>>(Box::new(LuaCoreBevyAPIProvider))
.add_systems(Startup, (setup, apply_deferred, run).chain())
.run();
}

fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
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::<LuaFile> {
scripts: vec![Script::new(path.into(), handle)],
});
}

fn run(mut events: PriorityEventWriter<LuaEvent<()>>) {
events.send(
LuaEvent {
hook_name: "on_event".into(),
args: (),
recipients: Recipients::All,
},
0,
);
}
63 changes: 63 additions & 0 deletions examples/rhai/dynamic_queries.rs
Original file line number Diff line number Diff line change
@@ -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::<ComponentA>()
.register_type::<ComponentB>()
.register_type::<ComponentC>()
.add_script_host::<RhaiScriptHost<()>>(PostUpdate)
.add_script_handler::<RhaiScriptHost<()>, 0, 0>(PostUpdate)
.add_api_provider::<RhaiScriptHost<()>>(Box::new(RhaiBevyAPIProvider))
.add_systems(Startup, (setup, apply_deferred, run).chain())
.run();
}

fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
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::<RhaiFile> {
scripts: vec![Script::new(path.into(), handle)],
});
}

fn run(mut events: PriorityEventWriter<RhaiEvent<()>>) {
events.send(
RhaiEvent {
hook_name: "on_event".into(),
args: (),
recipients: Recipients::All,
},
0,
);
}
Loading