Skip to content

Commit b38594f

Browse files
committed
Merge branch 'feature/lookup' into dev
2 parents caa8cc5 + 655c0ae commit b38594f

5 files changed

Lines changed: 419 additions & 4 deletions

File tree

README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
- [Internal Fragments](#internal-fragments)
5454
- [Shared Components](#shared-components)
5555
- [Fragment Requirements](#fragment-requirements)
56+
- [Id Names](#id-names)
5657
- [Destruction Policies](#destruction-policies)
5758
- [Custom Component Storages](#custom-component-storages)
5859
- [Garbage Collection](#garbage-collection)
@@ -64,6 +65,7 @@
6465
- [Chunk](#chunk)
6566
- [Builder](#builder)
6667
- [Changelog](#changelog)
68+
- [vX.Y.Z](#vxyz)
6769
- [v1.9.0](#v190)
6870
- [v1.8.0](#v180)
6971
- [v1.7.0](#v170)
@@ -1161,6 +1163,41 @@ local enemy = evolved.builder()
11611163
assert(evolved.has_all(enemy, position, velocity))
11621164
```
11631165

1166+
#### Id Names
1167+
1168+
The library provides a way to assign names to any id using the [`evolved.NAME`](#evolvedname) fragment. This is useful for debugging and development purposes, as it allows you to identify entities or fragments by their names instead of their identifiers. The name of an entity can be retrieved using the [`evolved.name`](#evolvedname-1) function.
1169+
1170+
```lua
1171+
local evolved = require 'evolved'
1172+
1173+
local player = evolved.builder()
1174+
:name('Player')
1175+
:build()
1176+
1177+
assert(evolved.name(player) == 'Player')
1178+
```
1179+
1180+
Names are not unique, so multiple entities can have the same name. Also, the name of an entity can be changed at any time by setting a new name using the [`evolved.NAME`](#evolvedname) fragment as a usual component.
1181+
1182+
You can find entities by their names using the [`evolved.lookup`](#evolvedlookup) and [`evolved.multi_lookup`](#evolvedmulti_lookup) functions. The [`evolved.lookup`](#evolvedlookup) function returns the first entity with the specified name, while the [`evolved.multi_lookup`](#evolvedmulti_lookup) function returns a list of all entities with the specified name.
1183+
1184+
```lua
1185+
local evolved = require 'evolved'
1186+
1187+
local player1 = evolved.builder()
1188+
:name('Player')
1189+
:build()
1190+
1191+
local player2 = evolved.builder()
1192+
:name('Player')
1193+
:build()
1194+
1195+
assert(evolved.lookup('Player') == player1)
1196+
1197+
local player_list, player_count = evolved.multi_lookup('Player')
1198+
assert(player_count == 2 and player_list[1] == player1 and player_list[2] == player2)
1199+
```
1200+
11641201
#### Destruction Policies
11651202

11661203
Typically, fragments remain alive for the entire lifetime of the program. However, in some cases, you might want to destroy fragments when they are no longer needed. For example, you can use some runtime entities as fragments for other entities. In this case, you might want to destroy such fragments even while they are still attached to other entities. Since entities cannot have destroyed fragments, a destruction policy must be applied to resolve this. By default, the library will remove the destroyed fragment from all entities that have it.
@@ -1496,6 +1533,9 @@ execute :: query -> {execute_state? -> chunk?, entity[]?, integer?}, execute_sta
14961533
14971534
locate :: entity -> chunk?, integer
14981535
1536+
lookup :: string -> entity?
1537+
multi_lookup :: string -> entity[], integer
1538+
14991539
process :: system... -> ()
15001540
process_with :: system, ... -> ()
15011541
@@ -1585,6 +1625,10 @@ builder_mt:destruction_policy :: id -> builder
15851625

15861626
## Changelog
15871627

1628+
### vX.Y.Z
1629+
1630+
- Added the new [`evolved.lookup`](#evolvedlookup) and [`evolved.multi_lookup`](#evolvedmulti_lookup) functions that allow finding ids by their names
1631+
15881632
### v1.9.0
15891633

15901634
- Performance improvements of the [`evolved.destroy`](#evolveddestroy) and [`evolved.batch_destroy`](#evolvedbatch_destroy) functions
@@ -2001,6 +2045,25 @@ function evolved.execute(query) end
20012045
function evolved.locate(entity) end
20022046
```
20032047

2048+
### `evolved.lookup`
2049+
2050+
```lua
2051+
---@param name string
2052+
---@return evolved.entity? entity
2053+
---@nodiscard
2054+
function evolved.lookup(name) end
2055+
```
2056+
2057+
### `evolved.multi_lookup`
2058+
2059+
```lua
2060+
---@param name string
2061+
---@return evolved.entity[] entity_list
2062+
---@return integer entity_count
2063+
---@nodiscard
2064+
function evolved.multi_lookup(name) end
2065+
```
2066+
20042067
### `evolved.process`
20052068

20062069
```lua

develop/all.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ require 'develop.testing.clone_tests'
44
require 'develop.testing.depth_tests'
55
require 'develop.testing.destroy_tests'
66
require 'develop.testing.locate_tests'
7+
require 'develop.testing.lookup_tests'
78
require 'develop.testing.main_tests'
89
require 'develop.testing.mappers_tests'
910
require 'develop.testing.multi_spawn_tests'

develop/testing/lookup_tests.lua

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
local evo = require 'evolved'
2+
3+
evo.debug_mode(true)
4+
5+
do
6+
local e1, e2, e3 = evo.id(3)
7+
8+
do
9+
assert(evo.lookup('lookup_hello') == nil)
10+
assert(evo.lookup('lookup_world') == nil)
11+
12+
do
13+
local entity_list, entity_count = evo.multi_lookup('lookup_hello')
14+
assert(entity_list and #entity_list == 0 and entity_count == 0)
15+
end
16+
17+
do
18+
local entity_list, entity_count = evo.multi_lookup('lookup_world')
19+
assert(entity_list and #entity_list == 0 and entity_count == 0)
20+
end
21+
end
22+
23+
evo.set(e1, evo.NAME, 'lookup_hello')
24+
25+
do
26+
assert(evo.lookup('lookup_hello') == e1)
27+
assert(evo.lookup('lookup_world') == nil)
28+
29+
do
30+
local entity_list, entity_count = evo.multi_lookup('lookup_hello')
31+
assert(entity_list and #entity_list == 1 and entity_count == 1)
32+
assert(entity_list[1] == e1)
33+
end
34+
35+
do
36+
local entity_list, entity_count = evo.multi_lookup('lookup_world')
37+
assert(entity_list and #entity_list == 0 and entity_count == 0)
38+
end
39+
end
40+
41+
evo.set(e2, evo.NAME, 'lookup_hello')
42+
evo.set(e3, evo.NAME, 'lookup_hello')
43+
44+
do
45+
assert(evo.lookup('lookup_hello') == e1)
46+
assert(evo.lookup('lookup_world') == nil)
47+
48+
do
49+
local entity_list, entity_count = evo.multi_lookup('lookup_hello')
50+
assert(entity_list and #entity_list == 3 and entity_count == 3)
51+
assert(entity_list[1] == e1 and entity_list[2] == e2 and entity_list[3] == e3)
52+
end
53+
end
54+
55+
evo.set(e2, evo.NAME, 'lookup_world')
56+
57+
do
58+
assert(evo.lookup('lookup_hello') == e1)
59+
assert(evo.lookup('lookup_world') == e2)
60+
61+
do
62+
local entity_list, entity_count = evo.multi_lookup('lookup_hello')
63+
assert(entity_list and #entity_list == 2 and entity_count == 2)
64+
assert(entity_list[1] == e1 and entity_list[2] == e3)
65+
end
66+
67+
do
68+
local entity_list, entity_count = evo.multi_lookup('lookup_world')
69+
assert(entity_list and #entity_list == 1 and entity_count == 1)
70+
assert(entity_list[1] == e2)
71+
end
72+
end
73+
74+
evo.set(e3, evo.NAME, 'lookup_world')
75+
76+
do
77+
assert(evo.lookup('lookup_hello') == e1)
78+
assert(evo.lookup('lookup_world') == e2)
79+
80+
do
81+
local entity_list, entity_count = evo.multi_lookup('lookup_hello')
82+
assert(entity_list and #entity_list == 1 and entity_count == 1)
83+
assert(entity_list[1] == e1)
84+
end
85+
86+
do
87+
local entity_list, entity_count = evo.multi_lookup('lookup_world')
88+
assert(entity_list and #entity_list == 2 and entity_count == 2)
89+
assert(entity_list[1] == e2 or entity_list[1] == e3)
90+
end
91+
end
92+
93+
evo.remove(e1, evo.NAME)
94+
95+
do
96+
assert(evo.lookup('lookup_hello') == nil)
97+
assert(evo.lookup('lookup_world') == e2)
98+
99+
do
100+
local entity_list, entity_count = evo.multi_lookup('lookup_hello')
101+
assert(entity_list and #entity_list == 0 and entity_count == 0)
102+
end
103+
104+
do
105+
local entity_list, entity_count = evo.multi_lookup('lookup_world')
106+
assert(entity_list and #entity_list == 2 and entity_count == 2)
107+
assert(entity_list[1] == e2 or entity_list[1] == e3)
108+
end
109+
end
110+
end
111+
112+
do
113+
local e1, e2, e3 = evo.id(3)
114+
115+
evo.set(e1, evo.NAME, 'lookup_e')
116+
117+
do
118+
local entity_list, entity_count = evo.multi_lookup('lookup_e')
119+
assert(entity_list and #entity_list == 1 and entity_count == 1)
120+
assert(entity_list[1] == e1)
121+
end
122+
123+
evo.set(e2, evo.NAME, 'lookup_e')
124+
125+
do
126+
local entity_list, entity_count = evo.multi_lookup('lookup_e')
127+
assert(entity_list and #entity_list == 2 and entity_count == 2)
128+
assert(entity_list[1] == e1 and entity_list[2] == e2)
129+
end
130+
131+
evo.set(e3, evo.NAME, 'lookup_e')
132+
133+
do
134+
local entity_list, entity_count = evo.multi_lookup('lookup_e')
135+
assert(entity_list and #entity_list == 3 and entity_count == 3)
136+
assert(entity_list[1] == e1 and entity_list[2] == e2 and entity_list[3] == e3)
137+
end
138+
139+
evo.clear(e1, e2, e3)
140+
141+
do
142+
local entity_list, entity_count = evo.multi_lookup('lookup_e')
143+
assert(entity_list and #entity_list == 0 and entity_count == 0)
144+
end
145+
146+
evo.set(e3, evo.NAME, 'lookup_e')
147+
148+
do
149+
local entity_list, entity_count = evo.multi_lookup('lookup_e')
150+
assert(entity_list and #entity_list == 1 and entity_count == 1)
151+
assert(entity_list[1] == e3)
152+
end
153+
154+
evo.set(e2, evo.NAME, 'lookup_e')
155+
156+
do
157+
local entity_list, entity_count = evo.multi_lookup('lookup_e')
158+
assert(entity_list and #entity_list == 2 and entity_count == 2)
159+
assert(entity_list[1] == e3 and entity_list[2] == e2)
160+
end
161+
162+
evo.set(e1, evo.NAME, 'lookup_e')
163+
164+
do
165+
local entity_list, entity_count = evo.multi_lookup('lookup_e')
166+
assert(entity_list and #entity_list == 3 and entity_count == 3)
167+
assert(entity_list[1] == e3 and entity_list[2] == e2 and entity_list[3] == e1)
168+
end
169+
170+
evo.destroy(e3, e2, e1)
171+
172+
do
173+
local entity_list, entity_count = evo.multi_lookup('lookup_e')
174+
assert(entity_list and #entity_list == 0 and entity_count == 0)
175+
end
176+
end

evolved.d.tl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,9 @@
208208

209209
locate: function(entity: Entity): Chunk | nil, integer
210210

211+
lookup: function(name: string): Entity | nil
212+
multi_lookup: function(name: string): { Entity }, integer
213+
211214
process: function(...: System)
212215
process_with: function(system: System, ...: any)
213216

0 commit comments

Comments
 (0)