Skip to content

Commit d8bf884

Browse files
authored
Merge pull request #106 from GsLogiMaker:feature/set-no-notify
Add `setm_no_notify` method
2 parents 610c497 + 796db46 commit d8bf884

File tree

5 files changed

+49
-26
lines changed

5 files changed

+49
-26
lines changed

cpp/src/component.cpp

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,6 @@ Ref<GFComponent> GFComponent::from_id(
3434
) {
3535
world = GFWorld::world_or_singleton(world);
3636

37-
const EcsComponent* comp_data = GFComponent::get_component_ptr(world, comp);
38-
// if (comp_data == nullptr) {
39-
// ERR(nullptr,
40-
// "Could not instantiate ", get_class_static(), "\n",
41-
// " Entity ", world->id_to_text(comp), " is not a component"
42-
// );
43-
// }
4437
Ref<GFComponent> comp_ref = memnew(GFComponent(entity, comp, world));
4538
return setup_template<GFComponent>(comp_ref);
4639
}
@@ -51,13 +44,6 @@ Ref<GFComponent> GFComponent::from_id_no_source(
5144
) {
5245
world = GFWorld::world_or_singleton(world);
5346

54-
// const EcsComponent* comp_data = GFComponent::get_component_ptr(world, comp);
55-
// if (comp_data == nullptr) {
56-
// ERR(nullptr,
57-
// "Could not instantiate ", get_class_static(), "\n",
58-
// " Entity ", world->id_to_text(comp), " is not a component"
59-
// );
60-
// }
6147
Ref<GFComponent> comp_ref = Ref(memnew(GFComponent(0, comp, world)));
6248
comp_ref->update_script();
6349
return comp_ref;
@@ -78,27 +64,34 @@ void GFComponent::_register_internal() {
7864
GFRegisterableEntity::_register_internal();
7965
}
8066

81-
void GFComponent::setm(const String member, const Variant value) const {
67+
bool GFComponent::setm(const String member, const Variant value) const {
68+
if (!setm_no_notify(member, value)) {
69+
return false;
70+
}
71+
ecs_modified_id(get_world()->raw(), get_source_id(), get_id());
72+
return true;
73+
}
74+
75+
bool GFComponent::setm_no_notify(const String member, const Variant value) const {
8276
ecs_world_t* raw = get_world()->raw();
8377

8478
// Get member data
8579
const EcsMember* member_data = get_member_data(member);
8680
if (member_data == nullptr) {
8781
// Member data is null. This should never happen.
88-
ERR(/**/,
82+
ERR(false,
8983
"Member metadata is null"
9084
);
9185
}
9286
void* member_ptr = get_member_ptr_mut_at(member_data->offset);
9387
if (member_ptr == nullptr) {
94-
ERR(/**/,
88+
ERR(false,
9589
"Member pointer is null"
9690
);
9791
}
9892

99-
// Return member
10093
Utils::set_type_from_variant(value, member_data->type, raw, member_ptr);
101-
ecs_modified_id(get_world()->raw(), get_source_id(), get_id());
94+
return true;
10295
}
10396

10497
Variant GFComponent::getm(const String member) const {
@@ -412,11 +405,12 @@ void GFComponent::_bind_methods() {
412405
GDVIRTUAL_BIND(_build, "b");
413406
godot::ClassDB::bind_method(D_METHOD("_register_internal"), &GFComponent::_register_internal);
414407

415-
godot::ClassDB::bind_static_method(GFComponent::get_class_static(), D_METHOD("from", "component", "world"), &GFComponent::from, nullptr);
416-
godot::ClassDB::bind_static_method(GFComponent::get_class_static(), D_METHOD("from_id", "id", "world"), &GFComponent::from_id, nullptr);
408+
godot::ClassDB::bind_static_method(get_class_static(), D_METHOD("from", "component", "world"), &GFComponent::from, nullptr);
409+
godot::ClassDB::bind_static_method(get_class_static(), D_METHOD("from_id", "id", "world"), &GFComponent::from_id, nullptr);
417410

418411
godot::ClassDB::bind_method(D_METHOD("getm", "member"), &GFComponent::getm);
419412
godot::ClassDB::bind_method(D_METHOD("setm", "member", "value"), &GFComponent::setm);
413+
godot::ClassDB::bind_method(D_METHOD("setm_no_notify", "member", "value"), &GFComponent::setm_no_notify);
420414

421415
godot::ClassDB::bind_method(D_METHOD("get_source_entity"), &GFComponent::get_source_entity);
422416
godot::ClassDB::bind_method(D_METHOD("get_source_id"), &GFComponent::get_source_id);

cpp/src/component.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ namespace godot {
4545
static Ref<GFComponent> from_id_no_source(ecs_entity_t comp, GFWorld* world);
4646

4747
Variant getm(const String) const;
48-
void setm(const String, const Variant) const;
48+
bool setm(const String, const Variant) const;
49+
bool setm_no_notify(const String, const Variant) const;
4950

5051
Ref<GFEntity> get_source_entity() const;
5152
ecs_entity_t get_source_id() const;

cpp/src/doc_classes/GFComponent.xml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
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">
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">
34
<brief_description>
45
A reference to a Glecs component from a [GFWorld] that is attatached to an entity.
56
</brief_description>
@@ -215,7 +216,7 @@
215216
</description>
216217
</method>
217218
<method name="setm" qualifiers="const">
218-
<return type="void" />
219+
<return type="bool" />
219220
<param index="0" name="member" type="String" />
220221
<param index="1" name="value" type="Variant" />
221222
<description>
@@ -229,6 +230,14 @@
229230
[/codeblock]
230231
</description>
231232
</method>
233+
<method name="setm_no_notify" qualifiers="const">
234+
<return type="bool" />
235+
<param index="0" name="member" type="String" />
236+
<param index="1" name="value" type="Variant" />
237+
<description>
238+
Sets the value of a component's member without triggering the [code]OnSet[/code] event. For most cases [method setm] should be preferred.
239+
</description>
240+
</method>
232241
<method name="setv">
233242
<return type="GFComponent" />
234243
<param index="0" name="component" type="Variant" />

cpp/src/doc_classes/GFWorld.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="GFWorld" inherits="Object"
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="GFWorld" inherits="Object" 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
An entity component system world.
65
</brief_description>

unittests/test_components.gd

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,26 @@ func test_components_in_relationships():
103103
w.free()
104104

105105

106+
func test_setm_no_notify():
107+
var e:= GFEntity.new() \
108+
.add(Foo)
109+
110+
var data:= {i = 0}
111+
GFObserverBuilder.new() \
112+
.set_events("/root/flecs/core/OnSet") \
113+
.with(Foo) \
114+
.for_each(func(c): data.i += 1)
115+
116+
var foo:Foo = e.get(Foo)
117+
foo.setm_no_notify("value", Vector2(23, 17))
118+
assert_eq(foo.getm("value"), Vector2(23, 17))
119+
assert_eq(data.i, 0)
120+
121+
foo.setm("value", Vector2.ONE)
122+
assert_eq(foo.getm("value"), Vector2.ONE)
123+
assert_eq(data.i, 1)
124+
125+
106126
class Targets extends GFRegisterableEntity: pass
107127

108128

0 commit comments

Comments
 (0)