Skip to content

Commit

Permalink
Merge pull request #48 from GsLogiMaker/feature/refactor-entity-setters
Browse files Browse the repository at this point in the history
Refactor get/set/add methods in `GFEntity` and `GFComponent`
  • Loading branch information
GsLogiMaker authored Jan 10, 2025
2 parents 637d634 + e770c2c commit cd47644
Show file tree
Hide file tree
Showing 24 changed files with 377 additions and 207 deletions.
9 changes: 7 additions & 2 deletions cpp/src/component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ void GFComponent::_register_internal() {
}

void GFComponent::setm(String member, Variant value) {
setm_no_notify(member, value);

ecs_modified_id(get_world()->raw(), get_source_id(), get_id());
}

void GFComponent::setm_no_notify(String member, Variant value) {
ecs_world_t* raw = get_world()->raw();

// Get member data
Expand All @@ -83,9 +89,7 @@ void GFComponent::setm(String member, Variant value) {
);
}

// Return member
Utils::set_type_from_variant(value, member_data->type, raw, member_ptr);
ecs_modified_id(get_world()->raw(), get_source_id(), get_id());
}

Variant GFComponent::getm(String member) {
Expand Down Expand Up @@ -375,6 +379,7 @@ void GFComponent::_bind_methods() {

godot::ClassDB::bind_method(D_METHOD("getm", "member"), &GFComponent::getm);
godot::ClassDB::bind_method(D_METHOD("setm", "member", "value"), &GFComponent::setm);
godot::ClassDB::bind_method(D_METHOD("setm_no_notify", "member", "value"), &GFComponent::setm_no_notify);

godot::ClassDB::bind_method(D_METHOD("get_source_entity"), &GFComponent::get_source_entity);
godot::ClassDB::bind_method(D_METHOD("get_source_id"), &GFComponent::get_source_id);
Expand Down
1 change: 1 addition & 0 deletions cpp/src/component.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ namespace godot {

Variant getm(String);
void setm(String, Variant);
void setm_no_notify(String, Variant);

Ref<GFEntity> get_source_entity();
ecs_entity_t get_source_id();
Expand Down
31 changes: 23 additions & 8 deletions cpp/src/doc_classes/GFComponent.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@
[codeblock]
var Vector2C = GFEntity.from("glecs/meta/Vector2")
var entity = GFEntity.new()
entity.add_component(Vector2C)
var vec2 = entity.get_component(Vector2C)
entity.add(Vector2C)
var vec2 = entity.get(Vector2C)

vec2.get_source_entity().get_id() == Vector2C.get_id() # True
[/codeblock]
Expand All @@ -75,8 +75,8 @@
[codeblock]
var Vector2C = GFEntity.from("glecs/meta/Vector2")
var entity = GFEntity.new()
entity.add_component(Vector2C)
var vec2 = entity.get_component(Vector2C)
entity.add(Vector2C)
var vec2 = entity.get(Vector2C)

vec2.get_source_id() == Vector2C.get_id() # True
[/codeblock]
Expand All @@ -89,8 +89,8 @@
Returns the value of a component's member.
[codeblock]
var entity = GFEntity.new()
entity.add_component("glecs/meta/Vector2")
var vec2 = entity.get_component("glecs/meta/Vector2")
entity.add("glecs/meta/Vector2")
var vec2 = entity.get("glecs/meta/Vector2")

vec2.getm("x")
[/codeblock]
Expand All @@ -110,12 +110,27 @@
Sets the value of a component's member.
[codeblock]
var entity = GFEntity.new()
entity.add_component("glecs/meta/Vector2")
var vec2 = entity.get_component("glecs/meta/Vector2")
entity.add("glecs/meta/Vector2")
var vec2 = entity.get("glecs/meta/Vector2")

vec2.setm("x", 3.14)
[/codeblock]
</description>
</method>
<method name="setm_no_notify">
<return type="void" />
<param index="0" name="member" type="String" />
<param index="1" name="value" type="Variant" />
<description>
Sets the value of a component's member without notifying [code]OnSet[/code] observers. Generally, [method setm] should be preferred.
[codeblock]
var entity = GFEntity.new()
entity.add("glecs/meta/Vector2")
var vec2 = entity.get("glecs/meta/Vector2")

vec2.setm_no_nofify("x", 3.14)
[/codeblock]
</description>
</method>
</methods>
</class>
14 changes: 3 additions & 11 deletions cpp/src/doc_classes/GFComponentBuilder.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="GFComponentBuilder" inherits="RefCounted" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/godotengine/godot/master/doc/class.xsd">
<class name="GFComponentBuilder" inherits="GFEntityBuilder" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/godotengine/godot/master/doc/class.xsd">
<brief_description>
Builder for a new component type.
</brief_description>
Expand Down Expand Up @@ -30,10 +30,9 @@
Creates the new component from the settings of the builder.
</description>
</method>
<method name="is_built">
<return type="bool" />
<method name="get_member_count">
<return type="int" />
<description>
Returns [code]true[/code] if this builder has already been built.
</description>
</method>
<method name="new_in_world" qualifiers="static">
Expand All @@ -43,12 +42,5 @@
Returns a new builder in [param world].
</description>
</method>
<method name="set_name">
<return type="GFComponentBuilder" />
<param index="0" name="name" type="String" />
<description>
Sets the name of the new component.
</description>
</method>
</methods>
</class>
60 changes: 44 additions & 16 deletions cpp/src/doc_classes/GFEntity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@
<link title="More on Flecs Entities">https://www.flecs.dev/flecs/md_docs_2EntitiesComponents.html</link>
</tutorials>
<methods>
<method name="add_component" qualifiers="const vararg">
<method name="add" qualifiers="const vararg">
<return type="GFEntity" />
<param index="0" name="component" type="Variant" />
<description>
Adds component data of type [param component] to this entity.
The component's type ID is coerced from a [Variant]. To learn more about [Variant] coercion see [method GFWorld.coerce_id].
[codeblock]
var entity:= GFEntity.new()
entity.add_component(GFPosition2D, Vector2(10, 10))
entity.get_component(GFPosition2D).get_vec() == Vector2(10, 10) # true
entity.add(GFPosition2D, Vector2(10, 10))
entity.get(GFPosition2D).get_vec() == Vector2(10, 10) # true
[/codeblock]
This method returns [code]self[/code] for chaining.
</description>
Expand Down Expand Up @@ -65,6 +65,14 @@
[/codeblock]
</description>
</method>
<method name="emit">
<return type="GFEntity" />
<param index="0" name="event" type="Variant" />
<param index="1" name="components" type="Array" default="[]" />
<param index="2" name="event_members" type="Array" default="[]" />
<description>
</description>
</method>
<method name="from" qualifiers="static">
<return type="GFEntity" />
<param index="0" name="entity" type="Variant" />
Expand All @@ -90,16 +98,16 @@
[/codeblock]
</description>
</method>
<method name="get_component">
<method name="get">
<return type="GFComponent" />
<param index="0" name="component" type="Variant" />
<description>
Returns a reference to this entity's component's data of type [param component].
Returns [code]null[/code] if the entity does not have the [param component] attached or if [param component] is not a component. The component is identified by an ID coerced from a [Variant]. To learn more about [Variant] coercion see [method GFWorld.coerce_id].
[codeblock]
var entity:= GFEntity.new()
entity.add_component("glecs/meta/Array")
entity.get_component("glecs/meta/Array") != null # true
entity.add("glecs/meta/Array")
entity.get("glecs/meta/Array") != null # true
[/codeblock]
</description>
</method>
Expand Down Expand Up @@ -134,8 +142,8 @@
Returns [code]null[/code] if the entity does not have the [param component] attached or if [param component] is not a component. The component is identified by an ID coerced from a [Variant]. To learn more about [Variant] coercion see [method GFWorld.coerce_id].
[codeblock]
var entity:= GFEntity.new()
entity.add_component("glecs/meta/Array")
entity.get_component("glecs/meta/Array") != null # true
entity.add("glecs/meta/Array")
entity.get("glecs/meta/Array") != null # true
[/codeblock]
</description>
</method>
Expand Down Expand Up @@ -218,24 +226,23 @@
[/codeblock]
</description>
</method>
<method name="set_component" qualifiers="const vararg">
<method name="set" qualifiers="const vararg">
<return type="GFEntity" />
<param index="0" name="component" type="Variant" />
<description>
Sets component data of type [param component] in this entity.
The component's type ID is coerced from a [Variant]. To learn more about [Variant] coercion see [method GFWorld.coerce_id].
[codeblock]
var entity:= GFEntity.new()
entity.add_component(GFPosition2D)
entity.set_component(GFPosition2D, Vector2(10, 10))
entity.get_component(GFPosition2D).get_vec() == Vector2(10, 10) # true
entity.add(GFPosition2D)
entity.set(GFPosition2D, Vector2(10, 10))
entity.get(GFPosition2D).get_vec() == Vector2(10, 10) # true
[/codeblock]
If the component is not added yet, then this method will add
it automaticly.
If the component is not added yet, then this method will add it automaticly.
[codeblock]
var entity:= GFEntity.new()
entity.set_component(GFPosition2D, Vector2(10, 10))
entity.get_component(GFPosition2D).get_vec() == Vector2(10, 10) # true
entity.set(GFPosition2D, Vector2(10, 10))
entity.get(GFPosition2D).get_vec() == Vector2(10, 10) # true
[/codeblock]
This method returns [code]self[/code] for chaining.
</description>
Expand All @@ -253,6 +260,27 @@
This method returns [code]self[/code] for chaining.
</description>
</method>
<method name="set_no_notify" qualifiers="const vararg">
<return type="GFEntity" />
<param index="0" name="component" type="Variant" />
<description>
Sets component data of type [param component] in this entity without notifying [code]OnSet[/code] observers. Generally, [method set] should be preferred.
The component's type ID is coerced from a [Variant]. To learn more about [Variant] coercion see [method GFWorld.coerce_id].
[codeblock]
var entity:= GFEntity.new()
entity.add(GFPosition2D)
entity.set(GFPosition2D, Vector2(10, 10))
entity.get(GFPosition2D).get_vec() == Vector2(10, 10) # true
[/codeblock]
If the component is not added yet, then this method will add it automaticly. Note that [code]OnAdd[/code] observers will still by notified in this scenario.
[codeblock]
var entity:= GFEntity.new()
entity.set(GFPosition2D, Vector2(10, 10))
entity.get(GFPosition2D).get_vec() == Vector2(10, 10) # true
[/codeblock]
This method returns [code]self[/code] for chaining.
</description>
</method>
<method name="set_pair" qualifiers="const vararg">
<return type="GFEntity" />
<param index="0" name="first" type="Variant" />
Expand Down
58 changes: 58 additions & 0 deletions cpp/src/doc_classes/GFEntityBuilder.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="GFEntityBuilder" inherits="RefCounted" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/godotengine/godot/master/doc/class.xsd">
<brief_description>
</brief_description>
<description>
</description>
<tutorials>
</tutorials>
<methods>
<method name="add_entity">
<return type="GFEntityBuilder" />
<param index="0" name="entity" type="Variant" />
<description>
</description>
</method>
<method name="add_pair">
<return type="GFEntityBuilder" />
<param index="0" name="first" type="Variant" />
<param index="1" name="second" type="Variant" />
<description>
</description>
</method>
<method name="build">
<return type="GFEntity" />
<description>
</description>
</method>
<method name="get_world">
<return type="GFWorld" />
<description>
</description>
</method>
<method name="new_in_world" qualifiers="static">
<return type="GFEntityBuilder" />
<param index="0" name="world" type="GFWorld" />
<description>
</description>
</method>
<method name="set_name">
<return type="GFEntityBuilder" />
<param index="0" name="name" type="String" />
<description>
</description>
</method>
<method name="set_parent">
<return type="GFEntityBuilder" />
<param index="0" name="parent" type="Variant" />
<description>
</description>
</method>
<method name="set_target_entity">
<return type="GFEntityBuilder" />
<param index="0" name="entity" type="Variant" />
<description>
</description>
</method>
</methods>
</class>
3 changes: 1 addition & 2 deletions cpp/src/doc_classes/GFModule.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="GFModule" inherits="GFRegisterableEntity"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/godotengine/godot/master/doc/class.xsd">
<class name="GFModule" inherits="GFRegisterableEntity" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/godotengine/godot/master/doc/class.xsd">
<brief_description>
</brief_description>
<description>
Expand Down
3 changes: 1 addition & 2 deletions cpp/src/doc_classes/GFPair.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="GFPair" inherits="GFEntity"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/godotengine/godot/master/doc/class.xsd">
<class name="GFPair" inherits="GFEntity" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/godotengine/godot/master/doc/class.xsd">
<brief_description>
A reference to a pair of entity IDs.
</brief_description>
Expand Down
3 changes: 1 addition & 2 deletions cpp/src/doc_classes/GFQueryIterator.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="GFQueryIterator" inherits="RefCounted"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/godotengine/godot/master/doc/class.xsd">
<class name="GFQueryIterator" inherits="RefCounted" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/godotengine/godot/master/doc/class.xsd">
<brief_description>
An iterator over queried entities.
</brief_description>
Expand Down
21 changes: 19 additions & 2 deletions cpp/src/doc_classes/GFQuerylikeBuilder.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="GFQuerylikeBuilder" inherits="RefCounted"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/godotengine/godot/master/doc/class.xsd">
<class name="GFQuerylikeBuilder" inherits="RefCounted" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/godotengine/godot/master/doc/class.xsd">
<brief_description>
The abstract base class for different query-like builders.
</brief_description>
Expand Down Expand Up @@ -49,6 +48,18 @@
Marks the last added term write only.
</description>
</method>
<method name="cascade">
<return type="GFQuerylikeBuilder" />
<param index="0" name="traversal" type="Variant" default="0" />
<description>
</description>
</method>
<method name="descend">
<return type="GFQuerylikeBuilder" />
<param index="0" name="traversal" type="Variant" default="0" />
<description>
</description>
</method>
<method name="is_built">
<return type="bool" />
<description>
Expand All @@ -71,6 +82,12 @@
The term added is identified by a [Variant] coerced to an ID. To learn more about [Variant] coercion see [method GFWorld.coerce_id].
</description>
</method>
<method name="up">
<return type="GFQuerylikeBuilder" />
<param index="0" name="traversal" type="Variant" default="0" />
<description>
</description>
</method>
<method name="with">
<return type="GFQuerylikeBuilder" />
<param index="0" name="component" type="Variant" />
Expand Down
3 changes: 1 addition & 2 deletions cpp/src/doc_classes/GFWorld.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<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">
<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">
<brief_description>
An entity component system world.
</brief_description>
Expand Down
Loading

0 comments on commit cd47644

Please sign in to comment.