Skip to content

Commit c3760c4

Browse files
committed
update README
1 parent a2a4fc5 commit c3760c4

2 files changed

Lines changed: 91 additions & 11 deletions

File tree

README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
- [Structural Changes](#structural-changes)
3636
- [Spawning Entities](#spawning-entities)
3737
- [Entity Builders](#entity-builders)
38+
- [Multi-Entity Spawning](#multi-entity-spawning)
3839
- [Access Operations](#access-operations)
3940
- [Iterating Over Fragments](#iterating-over-fragments)
4041
- [Modifying Operations](#modifying-operations)
@@ -476,6 +477,54 @@ local enemy = evolved.builder()
476477

477478
Builders can be reused, so you can create a builder with a specific set of fragments and components and then use it to spawn multiple entities with the same fragments and components.
478479

480+
#### Multi-Entity Spawning
481+
482+
When you need to spawn multiple entities with identical fragments, use `multi_spawn` and `multi_clone` to optimize performance and reduce overhead.
483+
484+
```lua
485+
---@param entity_count integer
486+
---@param component_table? evolved.component_table
487+
---@param component_mapper? evolved.component_mapper
488+
---@return evolved.entity[] entity_list
489+
---@return integer entity_count
490+
function evolved.multi_spawn(entity_count, component_table, component_mapper) end
491+
492+
---@param entity_count integer
493+
---@param prefab evolved.entity
494+
---@param component_table? evolved.component_table
495+
---@param component_mapper? evolved.component_mapper
496+
---@return evolved.entity[] entity_list
497+
---@return integer entity_count
498+
function evolved.multi_clone(entity_count, prefab, component_table, component_mapper) end
499+
```
500+
501+
These functions behave like their single-entity counterparts, but they allow you to spawn or clone multiple entities in one call. This approach minimizes the overhead of repeated function calls and structural changes, improving performance when handling large numbers of entities.
502+
503+
Typically, when spawning multiple entities, they share the same set of fragments, but their components can differ. You can achieve this by providing a `component_mapper` function, which receives the chunk and the range of places for the newly spawned entities. This avoids many `evolved.set` calls after spawning, which can be costly when creating many entities.
504+
505+
Here is a small example of using `evolved.multi_spawn` with a `component_mapper`:
506+
507+
```lua
508+
local evolved = require 'evolved'
509+
510+
local position_x, position_y = evolved.id(2)
511+
512+
evolved.multi_spawn(1000, {
513+
[position_x] = 0,
514+
[position_y] = 0,
515+
}, function(chunk, b_place, e_place)
516+
local x_components = chunk:components(position_x)
517+
local y_components = chunk:components(position_y)
518+
519+
for i = b_place, e_place do
520+
x_components[i] = math.random(-100, 100)
521+
y_components[i] = math.random(-100, 100)
522+
end
523+
end)
524+
```
525+
526+
Of course, you can use `evolved.multi_clone` in the same way. Builders can also be used for multi-entity spawning and cloning by calling the corresponding methods on the builder object.
527+
479528
### Access Operations
480529

481530
The library provides all the necessary functions to access entities and their components. I'm not going to cover all the accessor functions here, because they are pretty straightforward and self-explanatory. You can check the [API Reference](#api-reference) for all of them. Here are some of the most important ones:
@@ -1519,6 +1568,7 @@ builder_mt:destruction_policy :: id -> builder
15191568
### vX.Y.Z
15201569

15211570
- Added the new [`evolved.REALLOC`](#evolvedrealloc) and [`evolved.COMPMOVE`](#evolvedcompmove) fragment traits that allow customizing component storages
1571+
- Added `component_mapper` argument to the spawning and cloning functions that allows filling components in chunks during the operation
15221572

15231573
### v1.7.0
15241574

evolved.d.tl

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,30 @@
3131
end
3232

3333
interface Builder
34-
build: function(self: Builder, prefab?: Entity): Entity
35-
multi_build: function(self: Builder, entity_count: integer, prefab?: Entity): { Entity }, integer
34+
build: function(self: Builder,
35+
prefab?: Entity,
36+
component_mapper?: function(Chunk, integer)): Entity
3637

37-
spawn: function(self: Builder): Entity
38-
multi_spawn: function(self: Builder, entity_count: integer): { Entity }, integer
38+
multi_build: function(self: Builder,
39+
entity_count: integer,
40+
prefab?: Entity,
41+
component_mapper?: function(Chunk, integer, integer)): { Entity }, integer
3942

40-
clone: function(self: Builder, prefab: Entity): Entity
41-
multi_clone: function(self: Builder, entity_count: integer, prefab: Entity): { Entity }, integer
43+
spawn: function(self: Builder,
44+
component_mapper?: function(Chunk, integer)): Entity
45+
46+
multi_spawn: function(self: Builder,
47+
entity_count: integer,
48+
component_mapper?: function(Chunk, integer, integer)): { Entity }, integer
49+
50+
clone: function(self: Builder,
51+
prefab: Entity,
52+
component_mapper?: function(Chunk, integer)): Entity
53+
54+
multi_clone: function(self: Builder,
55+
entity_count: integer,
56+
prefab: Entity,
57+
component_mapper?: function(Chunk, integer, integer)): { Entity }, integer
4258

4359
has: function(self: Builder, fragment: Fragment): boolean
4460
has_all: function(self: Builder, ...: Fragment): boolean
@@ -139,11 +155,25 @@
139155
commit: function(): boolean
140156
cancel: function(): boolean
141157

142-
spawn: function(components?: { Fragment: any }): Entity
143-
multi_spawn: function(entity_count: integer, components?: { Fragment: any }): { Entity }, integer
144-
145-
clone: function(prefab: Entity, components?: { Fragment: any }): Entity
146-
multi_clone: function(entity_count: integer, prefab: Entity, components?: { Fragment: any }): { Entity }, integer
158+
spawn: function(
159+
component_table?: { Fragment: any },
160+
component_mapper?: function(Chunk, integer)): Entity
161+
162+
multi_spawn: function(
163+
entity_count: integer,
164+
component_table?: { Fragment: any },
165+
component_mapper?: function(Chunk, integer, integer)): { Entity }, integer
166+
167+
clone: function(
168+
prefab: Entity,
169+
component_table?: { Fragment: any },
170+
component_mapper?: function(Chunk, integer)): Entity
171+
172+
multi_clone: function(
173+
entity_count: integer,
174+
prefab: Entity,
175+
component_table?: { Fragment: any },
176+
component_mapper?: function(Chunk, integer, integer)): { Entity }, integer
147177

148178
alive: function(entity: Entity): boolean
149179
alive_all: function(...: Entity): boolean

0 commit comments

Comments
 (0)