Skip to content

Commit f4d2125

Browse files
committed
change query signature slightly
1 parent c42963a commit f4d2125

File tree

7 files changed

+74
-25
lines changed

7 files changed

+74
-25
lines changed

assets/scripts/game_of_life.lua

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,9 @@ world.info("Lua: The game_of_life.lua script just got loaded")
66
math.randomseed(os.time())
77

88
function fetch_life_state()
9-
-- find the entity with life state
10-
local life_state = nil
11-
12-
for i,result in pairs(world.query({LifeState}):build()) do
13-
life_state = result:components()[1]
14-
break
15-
end
16-
return life_state
9+
-- find the first entity with life state
10+
local i,v = next(world.query():component(LifeState):build())
11+
return v:components()[1]
1712
end
1813

1914
function on_script_loaded()
@@ -53,7 +48,28 @@ function on_click(x,y)
5348
local cell_y = math.floor(y / cell_height)
5449

5550
local index = (cell_y * dimension_x) + cell_x
56-
cells[index] = 255
51+
52+
-- toggle a bunch of cells around if they exist
53+
local cell_offsets = {
54+
{0,0},
55+
{1,0},
56+
{0,1},
57+
{1,1},
58+
{-1,0},
59+
{0,-1},
60+
{-1,-1},
61+
{1,-1},
62+
{-1,1}
63+
}
64+
65+
for _,offset in pairs(cell_offsets) do
66+
local offset_x = offset[1]
67+
local offset_y = offset[2]
68+
local new_index = index + offset_x + offset_y * dimension_x
69+
if new_index > 0 and new_index <= (dimension_x * dimension_y) then
70+
cells[new_index] = 255
71+
end
72+
end
5773
end
5874

5975
function on_update()

crates/bevy_mod_scripting_core/src/bindings/query.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,30 @@ impl ScriptQueryBuilder {
9090
self.components.extend(components);
9191
self
9292
}
93+
pub fn component(&mut self, component: ScriptTypeRegistration) -> &mut Self {
94+
self.components.push(component);
95+
self
96+
}
9397

94-
pub fn with(&mut self, with: Vec<ScriptTypeRegistration>) -> &mut Self {
98+
pub fn with_components(&mut self, with: Vec<ScriptTypeRegistration>) -> &mut Self {
9599
self.with.extend(with);
96100
self
97101
}
98102

99-
pub fn without(&mut self, without: Vec<ScriptTypeRegistration>) -> &mut Self {
103+
pub fn with_component(&mut self, with: ScriptTypeRegistration) -> &mut Self {
104+
self.with.push(with);
105+
self
106+
}
107+
108+
pub fn without_components(&mut self, without: Vec<ScriptTypeRegistration>) -> &mut Self {
100109
self.without.extend(without);
101110
self
102111
}
112+
113+
pub fn without_component(&mut self, without: ScriptTypeRegistration) -> &mut Self {
114+
self.without.push(without);
115+
self
116+
}
103117
}
104118

105119
#[derive(Clone, Reflect)]

crates/bevy_mod_scripting_functions/src/core.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,8 @@ pub fn register_world_functions(reg: &mut World) -> Result<(), FunctionRegistrat
138138
})
139139
.register(
140140
"query",
141-
|components: Vec<Val<ScriptTypeRegistration>>| {
142-
let mut query_builder = ScriptQueryBuilder::default();
143-
query_builder.components(components.into_iter().map(|v| v.into_inner()).collect());
141+
|| {
142+
let query_builder = ScriptQueryBuilder::default();
144143
Ok(Val(query_builder))
145144
},
146145
)
@@ -363,19 +362,24 @@ pub fn register_script_query_builder_functions(
363362
registry: &mut World,
364363
) -> Result<(), FunctionRegistrationError> {
365364
NamespaceBuilder::<ScriptQueryBuilder>::new(registry)
365+
.register("component", |s: Val<ScriptQueryBuilder>, components: Val<ScriptTypeRegistration>| {
366+
let mut builder = s.into_inner();
367+
builder.component(components.into_inner());
368+
Val(builder)
369+
})
366370
.register(
367371
"with",
368372
|s: Val<ScriptQueryBuilder>, with: Val<ScriptTypeRegistration>| {
369373
let mut builder = s.into_inner();
370-
builder.with(vec![with.into_inner()]);
374+
builder.with_component(with.into_inner());
371375
Val(builder)
372376
},
373377
)
374378
.register(
375379
"without",
376380
|s: Val<ScriptQueryBuilder>, without: Val<ScriptTypeRegistration>| {
377381
let mut builder = s.into_inner();
378-
builder.without(vec![without.into_inner()]);
382+
builder.without_component(without.into_inner());
379383
Val(builder)
380384
},
381385
)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
local component_a = world.get_type_by_name("TestComponent")
22

3-
for i,result in pairs(world.query({component_a}):with(component_a):without(component_a):build()) do
3+
for i,result in pairs(world.query():component(component_a):without(component_a):build()) do
44
assert(false, "This should not be reached")
55
end

crates/languages/bevy_mod_scripting_lua/tests/data/query/query_returns_all_entities_matching.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ world.add_default_component(entity_c, component_with)
1313
world.add_default_component(entity_b, component_without)
1414

1515
local found_entities = {}
16-
for i,result in pairs(world.query({component_with}):with(component_with):without(component_without):build()) do
16+
for i,result in pairs(world.query():component(component_with):without(component_without):build()) do
1717
table.insert(found_entities, result:entity())
1818
end
1919

docs/src/ScriptingReference/script-query-builder.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,27 @@
22

33
The query builder is used to build queries for entities with specific components. Can be used to interact with arbitrary entities in the world.
44

5+
## component
6+
7+
Adds a component to the query, this will be accessible in the query results under the index corresponding to the index of this component in the query.
8+
9+
Arguments:
10+
11+
| Argument | Type | Description |
12+
| --- | --- | --- |
13+
| `s` | `ScriptQueryBuilder` | The query builder |
14+
| `component` | `ScriptTypeRegistration` | The component to query for |
15+
16+
Returns:
17+
18+
| Return | Description |
19+
| --- | --- |
20+
| `ScriptQueryBuilder` | The updated query builder |
21+
22+
```lua
23+
query:component(MyType):component(MyOtherType)
24+
```
25+
526
## with
627

728
Arguments:

docs/src/ScriptingReference/world.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -284,20 +284,14 @@ end
284284

285285
### query
286286

287-
Arguments:
288-
289-
| Argument | Type | Description |
290-
| --- | --- | --- |
291-
| `components` | `Vec<ScriptTypeRegistration>` | The components to query for |
292-
293287
Returns:
294288

295289
| Return | Description |
296290
| --- | --- |
297291
| `ScriptQueryBuilder` | The query builder |
298292

299293
```lua
300-
query = world.query({MyType})
294+
local queryBuilder = world.query()
301295
```
302296

303297
### exit

0 commit comments

Comments
 (0)