Skip to content

Commit 6aca38a

Browse files
authored
Merge pull request #110 from GsLogiMaker:feature/query-variables
Implment query variables and setting term sources
2 parents d8bf884 + ec0a63a commit 6aca38a

17 files changed

+285
-59
lines changed

cpp/src/base_iterator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"Iterator is already exhausted" \
1616
); \
1717
} \
18-
if (index == iterator.count) { \
18+
if (index >= iterator.count) { \
1919
index = 0; \
2020
set_done(!ECS_NEXT(&iterator)); \
2121
if (is_done()) { \

cpp/src/doc_classes/GFComponent.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
2-
<class name="GFComponent" inherits="GFRegisterableEntity"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/godotengine/godot/master/doc/class.xsd">
2+
<class name="GFComponent" inherits="GFRegisterableEntity" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/godotengine/godot/master/doc/class.xsd">
43
<brief_description>
54
A reference to a Glecs component from a [GFWorld] that is attatached to an entity.
65
</brief_description>

cpp/src/doc_classes/GFObserverBuilder.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
2-
<class name="GFObserverBuilder" inherits="GFQuerylikeBuilder" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/godotengine/godot/master/doc/class.xsd">
2+
<class name="GFObserverBuilder" inherits="GFQuerylikeBuilder"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/godotengine/godot/master/doc/class.xsd">
34
<brief_description>
45
A builder for observers.
56
</brief_description>
@@ -123,6 +124,13 @@
123124
Overrides [method GFEntityBuilder.set_target_entity].
124125
</description>
125126
</method>
127+
<method name="src">
128+
<return type="GFObserverBuilder" />
129+
<param index="0" name="entity" type="Variant" />
130+
<description>
131+
Overrides [method GFQuerylikeBuilder.src].
132+
</description>
133+
</method>
126134
<method name="up">
127135
<return type="GFObserverBuilder" />
128136
<param index="0" name="traversal" type="Variant" default="0" />

cpp/src/doc_classes/GFQueryBuilder.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
2-
<class name="GFQueryBuilder" inherits="GFQuerylikeBuilder" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/godotengine/godot/master/doc/class.xsd">
2+
<class name="GFQueryBuilder" inherits="GFQuerylikeBuilder"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/godotengine/godot/master/doc/class.xsd">
34
<brief_description>
45
A builder for queries.
56
</brief_description>
@@ -95,6 +96,13 @@
9596
Overrides [method GFEntityBuilder.set_target_entity].
9697
</description>
9798
</method>
99+
<method name="src">
100+
<return type="GFQueryBuilder" />
101+
<param index="0" name="entity" type="Variant" />
102+
<description>
103+
Overrides [GFQuerylikeBuilder.src].
104+
</description>
105+
</method>
98106
<method name="up">
99107
<return type="GFQueryBuilder" />
100108
<param index="0" name="traversal" type="Variant" default="0" />

cpp/src/doc_classes/GFQuerylikeBuilder.xml

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
2-
<class name="GFQuerylikeBuilder" inherits="GFEntityBuilder" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/godotengine/godot/master/doc/class.xsd">
2+
<class name="GFQuerylikeBuilder" inherits="GFEntityBuilder"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/godotengine/godot/master/doc/class.xsd">
34
<brief_description>
45
The abstract base class for different query-like builders.
56
</brief_description>
@@ -78,6 +79,28 @@
7879
The term added is identified by a [Variant] coerced to an ID. To learn more about [Variant] coercion see [method GFWorld.coerce_id].
7980
</description>
8081
</method>
82+
<method name="src">
83+
<return type="GFQuerylikeBuilder" />
84+
<param index="0" name="entity" type="Variant" />
85+
<description>
86+
Sets the source of the most recently added term. Setting the source allows for matching terms against a specific entity. [url=https://www.flecs.dev/flecs/md_docs_2Queries.html#source]More about Flecs sources[/url].
87+
Example:
88+
[codeblock]
89+
var Velocity = GFComponentBuilder.new() \
90+
.add_member("velocity", TYPE_VECTOR2) \
91+
.build()
92+
var ship = GFEntity.new() \
93+
.add(Velocity)
94+
var asteroid = GFEntity.new() \
95+
.add(Velocity)
96+
97+
# Query only for Velocity of the ship, not the asteroid
98+
var query = GFQueryBuilder.new() \
99+
.with(Velocity).src(ship) \
100+
.build()
101+
[/codeblock]
102+
</description>
103+
</method>
81104
<method name="up">
82105
<return type="GFQuerylikeBuilder" />
83106
<param index="0" name="traversal" type="Variant" default="0" />

cpp/src/doc_classes/GFSystemBuilder.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
2-
<class name="GFSystemBuilder" inherits="GFQuerylikeBuilder" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/godotengine/godot/master/doc/class.xsd">
2+
<class name="GFSystemBuilder" inherits="GFQuerylikeBuilder"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/godotengine/godot/master/doc/class.xsd">
34
<brief_description>
45
A builder for systems.
56
</brief_description>
@@ -98,6 +99,13 @@
9899
Overrides [method GFEntityBuilder.set_target_entity].
99100
</description>
100101
</method>
102+
<method name="src">
103+
<return type="GFSystemBuilder" />
104+
<param index="0" name="entity" type="Variant" />
105+
<description>
106+
Overrides [method GFQuerylikeBuilder.src].
107+
</description>
108+
</method>
101109
<method name="up">
102110
<return type="GFSystemBuilder" />
103111
<param index="0" name="traversal" type="Variant" default="0" />

cpp/src/observer_builder.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Ref<GFObserverBuilder> GFObserverBuilder::new_in_world(GFWorld* world) {
1717
}
1818

1919
void GFObserverBuilder::for_each(const Callable callable) {
20+
QUERYLIKE_BUILD_START
21+
2022
ecs_entity_t obs_id = GFEntityBuilder::build_id();
2123
// Prevent creating new entity if builder is rebuilt
2224
set_target_entity(obs_id);
@@ -31,6 +33,8 @@ void GFObserverBuilder::for_each(const Callable callable) {
3133
};
3234
ecs_observer_init(get_world()->raw(), &desc);
3335
}
36+
37+
QUERYLIKE_BUILD_END
3438
}
3539

3640
Ref<GFObserverBuilder> GFObserverBuilder::set_event(int index, const Variant event) {

cpp/src/query_builder.cpp

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

33
#include "query_builder.h"
44
#include "godot_cpp/variant/callable.hpp"
5+
#include "querylike_builder.h"
56
#include "world.h"
67
#include "query.h"
78

@@ -18,6 +19,8 @@ Ref<GFQueryBuilder> GFQueryBuilder::new_in_world(GFWorld* world) {
1819
}
1920

2021
Ref<GFQuery> GFQueryBuilder::build() {
22+
QUERYLIKE_BUILD_START
23+
2124
query_desc.entity = GFEntityBuilder::build_id();
2225

2326
setup_ctx(Callable());
@@ -26,6 +29,8 @@ Ref<GFQuery> GFQueryBuilder::build() {
2629
&query_desc
2730
);
2831

32+
QUERYLIKE_BUILD_END
33+
2934
return Ref(memnew(GFQuery(get_world(), query)));
3035
}
3136

cpp/src/query_iteration_context.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include "godot_cpp/variant/callable.hpp"
88
#include "godot_cpp/variant/variant.hpp"
99
#include "tag.h"
10-
#include "utils.h"
1110
#include "world.h"
1211

1312
#include <stdlib.h>
@@ -21,10 +20,7 @@ using namespace godot;
2120

2221
ecs_entity_t get_compmonent_of_term(const ecs_term_t* term) {
2322
if (term->id == 0) {
24-
ERR(0,
25-
"Could not get component of term\n",
26-
"Term ID is 0"
27-
);
23+
return ecs_pair(term->first.id, term->second.id);
2824
}
2925
return term->id;
3026
}
@@ -104,6 +100,8 @@ void QueryIterationContext::update_component_entities(ecs_iter_t* it, int entity
104100
// Object is null, skip
105101
continue;
106102
}
103+
104+
107105
if (!added_entity->is_class(GFComponent::get_class_static())) {
108106
// Added ID is not a component, skip
109107
continue;
@@ -121,18 +119,18 @@ void QueryIterationContext::update_component_entities(ecs_iter_t* it, int entity
121119
}
122120

123121
void QueryIterationContext::update_component_terms(ecs_iter_t* it) {
124-
auto query = it->query;
125-
auto terms = it->query->terms;
122+
const ecs_term_t* terms = it->query->terms;
126123

127124
int i_arg = 0;
128-
for (int term_i=0; term_i != query->term_count; term_i++) {
125+
for (int term_i=0; term_i != it->query->term_count; term_i++) {
129126
ecs_entity_t entity_id = ecs_field_src(it, term_i);
130127
if (entity_id == 0) {
131128
entity_id = it->entities[0];
132129
}
133130

134131
const ecs_term_t* term = &terms[term_i];
135132
Ref<GFEntity> comp_ref = comp_ref_per_term[term_i];
133+
comp_ref->set_id(it->ids[i_arg]);
136134

137135
switch (term->oper) {
138136
case ecs_oper_kind_t::EcsAnd:

cpp/src/query_iterator.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11

22
#include "query_iterator.h"
3+
#include "component.h"
4+
#include "entity.h"
35
#include "query_iteration_context.h"
46
#include "godot_cpp/variant/array.hpp"
57
#include "godot_cpp/variant/variant.hpp"
68
#include "querylike_builder.h"
9+
#include "tag.h"
710

811
#include <stdint.h>
912
#include <flecs.h>
@@ -28,11 +31,29 @@ Variant GFQueryIterator::_iter_get(Variant arg) {
2831
QueryIterationContext* ctx = static_cast<QueryIterationContext*>(
2932
iterator.query->binding_ctx
3033
);
31-
return ctx->comp_ref_args;
34+
Array result = Array();
35+
for (int i=0; i != ctx->comp_ref_args.size(); i++) {
36+
Ref<GFEntity> entity = ctx->comp_ref_args[i];
37+
if (entity->get_class() == GFComponent::get_class_static()) {
38+
Ref<GFComponent> comp = ctx->comp_ref_args[i];
39+
result.append(memnew(GFComponent(
40+
comp->get_source_id(),
41+
comp->get_id(),
42+
comp->get_world()
43+
)));
44+
} else if (entity->get_class() == GFTag::get_class_static()) {
45+
Ref<GFTag> tag = ctx->comp_ref_args[i];
46+
result.append(memnew(GFTag(
47+
tag->get_id(),
48+
tag->get_world()
49+
)));
50+
}
51+
}
52+
return result;
3253
}
3354

3455
// --------------------------------------
35-
// --- Unexposed
56+
// --- Unexposed ---
3657
// --------------------------------------
3758

3859
bool GFQueryIterator::next() {

0 commit comments

Comments
 (0)