Skip to content

Commit d025dbd

Browse files
Joakkermakspll
andauthored
Add dynamic query examples (#120)
Co-authored-by: Maksymilian Mozolewski <[email protected]>
1 parent 26f9869 commit d025dbd

File tree

5 files changed

+157
-0
lines changed

5 files changed

+157
-0
lines changed

Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,16 @@ name = "complex_game_loop_lua"
136136
path = "examples/lua/complex_game_loop.rs"
137137
required-features = ["lua54"]
138138

139+
[[example]]
140+
name = "dynamic_queries_lua"
141+
path = "examples/lua/dynamic_queries.rs"
142+
required-features = ["lua54", "lua_script_api"]
143+
144+
[[example]]
145+
name = "dynamic_queries_rhai"
146+
path = "examples/rhai/dynamic_queries.rs"
147+
required-features = ["rhai", "rhai_script_api"]
148+
139149
[[example]]
140150
name = "game_of_life_lua"
141151
path = "examples/lua/game_of_life.rs"

assets/scripts/dynamic_queries.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function on_event()
2+
local component_a = world:get_type_by_name("ComponentA")
3+
local component_b = world:get_type_by_name("ComponentB")
4+
local component_c = world:get_type_by_name("ComponentC")
5+
6+
for entity, _ in world:query(component_a):with(component_b):without(component_c):iter() do
7+
print(entity)
8+
end
9+
end

assets/scripts/dynamic_queries.rhai

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
fn on_event() {
2+
let component_a = world.get_type_by_name("ComponentA");
3+
let component_b = world.get_type_by_name("ComponentB");
4+
let component_c = world.get_type_by_name("ComponentC");
5+
6+
// Use with_components/without_components, as the word `with` is
7+
// reserved in rhai
8+
for results in world.query([component_a]).with_components([component_b]).without_components([component_c]) {
9+
print(results.Entity);
10+
}
11+
}

examples/lua/dynamic_queries.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
use bevy::prelude::*;
2+
use bevy_mod_scripting::prelude::*;
3+
4+
#[derive(Component, Reflect)]
5+
#[reflect(Component)]
6+
pub struct ComponentA;
7+
8+
#[derive(Component, Reflect)]
9+
#[reflect(Component)]
10+
pub struct ComponentB;
11+
12+
#[derive(Component, Reflect)]
13+
#[reflect(Component)]
14+
pub struct ComponentC;
15+
16+
fn main() {
17+
let mut app = App::new();
18+
19+
app.add_plugins((DefaultPlugins, ScriptingPlugin))
20+
.register_type::<ComponentA>()
21+
.register_type::<ComponentB>()
22+
.register_type::<ComponentC>()
23+
.add_script_host::<LuaScriptHost<()>>(PostUpdate)
24+
.add_script_handler::<LuaScriptHost<()>, 0, 0>(PostUpdate)
25+
.add_api_provider::<LuaScriptHost<()>>(Box::new(LuaBevyAPIProvider))
26+
.add_api_provider::<LuaScriptHost<()>>(Box::new(LuaCoreBevyAPIProvider))
27+
.add_systems(Startup, (setup, apply_deferred, run).chain())
28+
.run();
29+
}
30+
31+
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
32+
commands.spawn((ComponentA,));
33+
commands.spawn((ComponentA, ComponentB));
34+
commands.spawn((ComponentA, ComponentC));
35+
commands.spawn((ComponentA, ComponentB, ComponentC));
36+
37+
commands.spawn((ComponentB,));
38+
commands.spawn((ComponentB, ComponentC));
39+
commands.spawn((ComponentB, ComponentA));
40+
commands.spawn((ComponentB, ComponentA, ComponentC));
41+
42+
commands.spawn((ComponentC,));
43+
commands.spawn((ComponentC, ComponentA));
44+
commands.spawn((ComponentC, ComponentB));
45+
commands.spawn((ComponentC, ComponentA, ComponentB));
46+
47+
let path = "scripts/dynamic_queries.lua";
48+
let handle = asset_server.load(path);
49+
50+
commands.spawn(ScriptCollection::<LuaFile> {
51+
scripts: vec![Script::new(path.into(), handle)],
52+
});
53+
}
54+
55+
fn run(mut events: PriorityEventWriter<LuaEvent<()>>) {
56+
events.send(
57+
LuaEvent {
58+
hook_name: "on_event".into(),
59+
args: (),
60+
recipients: Recipients::All,
61+
},
62+
0,
63+
);
64+
}

examples/rhai/dynamic_queries.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
use bevy::prelude::*;
2+
use bevy_mod_scripting::prelude::*;
3+
4+
#[derive(Component, Reflect)]
5+
#[reflect(Component)]
6+
pub struct ComponentA;
7+
8+
#[derive(Component, Reflect)]
9+
#[reflect(Component)]
10+
pub struct ComponentB;
11+
12+
#[derive(Component, Reflect)]
13+
#[reflect(Component)]
14+
pub struct ComponentC;
15+
16+
fn main() {
17+
let mut app = App::new();
18+
19+
app.add_plugins((DefaultPlugins, ScriptingPlugin))
20+
.register_type::<ComponentA>()
21+
.register_type::<ComponentB>()
22+
.register_type::<ComponentC>()
23+
.add_script_host::<RhaiScriptHost<()>>(PostUpdate)
24+
.add_script_handler::<RhaiScriptHost<()>, 0, 0>(PostUpdate)
25+
.add_api_provider::<RhaiScriptHost<()>>(Box::new(RhaiBevyAPIProvider))
26+
.add_systems(Startup, (setup, apply_deferred, run).chain())
27+
.run();
28+
}
29+
30+
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
31+
commands.spawn((ComponentA,));
32+
commands.spawn((ComponentA, ComponentB));
33+
commands.spawn((ComponentA, ComponentC));
34+
commands.spawn((ComponentA, ComponentB, ComponentC));
35+
36+
commands.spawn((ComponentB,));
37+
commands.spawn((ComponentB, ComponentC));
38+
commands.spawn((ComponentB, ComponentA));
39+
commands.spawn((ComponentB, ComponentA, ComponentC));
40+
41+
commands.spawn((ComponentC,));
42+
commands.spawn((ComponentC, ComponentA));
43+
commands.spawn((ComponentC, ComponentB));
44+
commands.spawn((ComponentC, ComponentA, ComponentB));
45+
46+
let path = "scripts/dynamic_queries.rhai";
47+
let handle = asset_server.load(path);
48+
49+
commands.spawn(ScriptCollection::<RhaiFile> {
50+
scripts: vec![Script::new(path.into(), handle)],
51+
});
52+
}
53+
54+
fn run(mut events: PriorityEventWriter<RhaiEvent<()>>) {
55+
events.send(
56+
RhaiEvent {
57+
hook_name: "on_event".into(),
58+
args: (),
59+
recipients: Recipients::All,
60+
},
61+
0,
62+
);
63+
}

0 commit comments

Comments
 (0)