Skip to content

Commit 610c497

Browse files
authored
Merge pull request #104 from GsLogiMaker:refactor/world-singleton
Refactor world singleton and default worlds
2 parents 2071d7a + 04f223a commit 610c497

35 files changed

+387
-241
lines changed

cpp/src/component.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ namespace godot {
2828
{}
2929
GFComponent():
3030
source_entity_id(0),
31-
GFRegisterableEntity(0, GFWorld::singleton())
31+
GFRegisterableEntity(0, nullptr)
3232
{}
3333
~GFComponent() {};
3434

cpp/src/component_builder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace godot {
2525
member_names(Array())
2626
{}
2727
GFComponentBuilder():
28-
GFEntityBuilder(GFWorld::singleton())
28+
GFEntityBuilder(nullptr)
2929
{}
3030
~GFComponentBuilder();
3131

cpp/src/doc_classes/GFComponent.xml

Lines changed: 2 additions & 4 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>
@@ -86,8 +85,7 @@
8685
<method name="emit">
8786
<return type="GFComponent" />
8887
<param index="0" name="entity" type="Variant" />
89-
<param index="1" name="components" type="Array" />
90-
<param index="2" name="event_members" type="Array" default="[]" />
88+
<param index="1" name="event_members" type="Array" default="[]" />
9189
<description>
9290
Overrides [method GFEntity.emit].
9391
</description>

cpp/src/doc_classes/GFEntity.xml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
2-
<class name="GFEntity" inherits="RefCounted"
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="GFEntity" inherits="RefCounted" 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 an entity from a [GFWorld].
65
</brief_description>
@@ -148,8 +147,7 @@
148147
<method name="emit">
149148
<return type="GFEntity" />
150149
<param index="0" name="entity" type="Variant" />
151-
<param index="1" name="components" type="Array" />
152-
<param index="2" name="event_members" type="Array" default="[]" />
150+
<param index="1" name="event_members" type="Array" default="[]" />
153151
<description>
154152
Triggers this entity to emit as an event to be cought by observers.
155153
TODO: Add code example.

cpp/src/doc_classes/GFModule.xml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
2-
<class name="GFModule" 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="GFModule" 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
</brief_description>
65
<description>
@@ -70,8 +69,7 @@
7069
<method name="emit">
7170
<return type="GFModule" />
7271
<param index="0" name="entity" type="Variant" />
73-
<param index="1" name="components" type="Array" />
74-
<param index="2" name="event_members" type="Array" default="[]" />
72+
<param index="1" name="event_members" type="Array" default="[]" />
7573
<description>
7674
Overrides [method GFEntity.emit].
7775
</description>

cpp/src/doc_classes/GFPair.xml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
2-
<class name="GFPair" inherits="GFEntity"
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="GFPair" inherits="GFEntity" 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 pair of entity IDs.
65
</brief_description>
@@ -72,8 +71,7 @@
7271
<method name="emit">
7372
<return type="GFPair" />
7473
<param index="0" name="entity" type="Variant" />
75-
<param index="1" name="components" type="Array" />
76-
<param index="2" name="event_members" type="Array" default="[]" />
74+
<param index="1" name="event_members" type="Array" default="[]" />
7775
<description>
7876
Overrides [method GFEntity.emit].
7977
</description>

cpp/src/doc_classes/GFRegisterableEntity.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@
7575
<method name="emit">
7676
<return type="GFRegisterableEntity" />
7777
<param index="0" name="entity" type="Variant" />
78-
<param index="1" name="components" type="Array" />
79-
<param index="2" name="event_members" type="Array" default="[]" />
78+
<param index="1" name="event_members" type="Array" default="[]" />
8079
<description>
8180
</description>
8281
</method>

cpp/src/doc_classes/GFTag.xml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
2-
<class name="GFTag" 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="GFTag" 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 an entity with no data.
65
</brief_description>
@@ -73,8 +72,7 @@
7372
<method name="emit">
7473
<return type="GFTag" />
7574
<param index="0" name="entity" type="Variant" />
76-
<param index="1" name="components" type="Array" />
77-
<param index="2" name="event_members" type="Array" default="[]" />
75+
<param index="1" name="event_members" type="Array" default="[]" />
7876
<description>
7977
Overrides [method GFEntity.emit].
8078
</description>

cpp/src/doc_classes/GFWorld.xml

Lines changed: 46 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="GFWorld" inherits="Object" 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"
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
An entity component system world.
56
</brief_description>
@@ -51,6 +52,13 @@
5152
[/codeblock]
5253
</description>
5354
</method>
55+
<method name="get_default_world" qualifiers="static">
56+
<return type="GFWorld" />
57+
<description>
58+
Returns the current default world. If no default world is set then this method will return the world from [method get_singleton]. See [method set_default_world] for more information on default worlds.
59+
[b]NOTE:[/b] Default worlds are unique per thread. This method will only return worlds set in the same thread.
60+
</description>
61+
</method>
5462
<method name="get_raw" qualifiers="const">
5563
<return type="int" />
5664
<description>
@@ -63,7 +71,18 @@
6371

6472
ecs_new(raw); // Create a new entity
6573
[/codeblock]
66-
WARNING: Do not call finish, or otherwise destruct the `ecs_world_t`. The world object is managed by [GFWorld] and freeing it separate from the [GFWorld] will cause undefined behavior or crashes.
74+
[b]WARNING:[/b] Do not call finish, or otherwise destruct the [code]ecs_world_t[/code]. The world object is managed by [GFWorld] and freeing it separate from the [GFWorld] will cause undefined behavior or crashes.
75+
</description>
76+
</method>
77+
<method name="get_singleton" qualifiers="static">
78+
<return type="GFWorld" />
79+
<description>
80+
Returns the global world of the program.
81+
Example:
82+
[codeblock]
83+
var world = GFWorld.get_singleton()
84+
GFEntity.new_in_world(world)
85+
[/codeblock]
6786
</description>
6887
</method>
6988
<method name="lookup">
@@ -139,6 +158,31 @@
139158
[/codeblock]
140159
</description>
141160
</method>
161+
<method name="set_default_world" qualifiers="static">
162+
<return type="void" />
163+
<param index="0" name="world" type="GFWorld" />
164+
<description>
165+
Sets the default world that methods like [method GFEntity.new] use to determine which world to create objects in.
166+
This method can be used as a means of switching the context (world) in which ECS operations take place. For example:
167+
[codeblock]
168+
var custom_default_world = GFWorld.new()
169+
170+
var old_default_world = GFWorld.get_default_world()
171+
GFWorld.set_default_world(custom_default_world)
172+
173+
# The following line creates an entity in
174+
# `custom_default_world` when it otherwise
175+
# would've been created in `old_default_world`.
176+
var entity = GFEntity.new()
177+
178+
# It is a good practice to restore the default
179+
# world to whatever it was before you set it.
180+
GFWorld.set_default_world(old_default_world)
181+
[/codeblock]
182+
Changing the default world can be used as an alternative to [code]new_in_world[/code] methods such as [method GFSystemBuilder.new_in_world]. This method is called internally before [method GFRegisterableEntity._register] is called so that entities created during registration are created in the same world as the registered entity. This is particularly helpful when registering [GFModule]s where you may be creating many entities, systems, and observers. If [method set_default_world] wasn't called before registering modules then you would need to use [code]new_in_world[/code] methods instead of [code]new[/code] if you wanted your module to work in worlds other than the global returned from [method get_singleton].
183+
[b]NOTE:[/b] Default worlds are unique per thread. This method will only affect the thread it was called in.
184+
</description>
185+
</method>
142186
<method name="start_rest_api" qualifiers="const">
143187
<return type="void" />
144188
<description>

cpp/src/entity.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,6 @@ Ref<GFEntity> GFEntity::emit(
184184
ecs_entity_t target_id = w->coerce_id(target_entity);
185185

186186
auto type = ecs_get_type(w->raw(), target_id);
187-
for (int i=0; i != type->count; i++) {
188-
UtilityFunctions::prints("TYPE", w->id_to_text(type->array[i]));
189-
}
190187

191188
// Emit
192189
ecs_event_desc_t desc = {

0 commit comments

Comments
 (0)