Skip to content

Commit 33b34f3

Browse files
committed
manual wip
1 parent 0f1012e commit 33b34f3

1 file changed

Lines changed: 125 additions & 0 deletions

File tree

MANUAL.md

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# Manual
2+
3+
## Identifiers
4+
5+
Identifier is a packed 40-bit integer number. The first 20 bits are the index, and the last 20 bits are the version. To create a new identifier, use the `evolved.id` function.
6+
7+
```lua
8+
---@param count? integer
9+
---@return evolved.id ... ids
10+
function evolved.id(count) end
11+
```
12+
13+
The `count` parameter is optional and defaults to `1`. The function returns one or more identifiers, depending on the `count` parameter. Maximum number of alive identifiers is `2^20-1` (1048575). After that, the function will throw an error `"| evolved.lua | id index overflow"`.
14+
15+
Identifiers can be recycled. When an identifier is no longer needed, use the `evolved.destroy` function to destroy it. This will free up the identifier for reuse.
16+
17+
```lua
18+
---@param ... evolved.id ids
19+
function evolved.destroy(...) end
20+
```
21+
22+
The `destroy` function takes one or more identifiers as arguments. Destroyed identifiers will be added to the recycler free list. It is safe to destroy identifiers that are not alive (the function will just ignore them).
23+
24+
After destroying an identifier, it can be reused by calling the `evolved.id` function again. The new identifier will have the same index as the destroyed one, but a different version. The version is incremented each time an identifier is destroyed. This mechanism allows us to reuse indices and know if an identifier is alive or not.
25+
26+
The `evolved.alive` function set can be used to check if identifiers are alive or not.
27+
28+
```lua
29+
---@param id evolved.id
30+
---@return boolean
31+
function evolved.alive(id) end
32+
33+
---@param ... evolved.id ids
34+
---@return boolean
35+
function evolved.alive_all(...) end
36+
37+
---@param ... evolved.id ids
38+
---@return boolean
39+
function evolved.alive_any(...) end
40+
```
41+
42+
Sometimes (for debugging purposes for example), it is necessary to extract the index and version from an identifier (or pack them back). The `evolved.pack` and `evolved.unpack` functions can be used for this purpose.
43+
44+
```lua
45+
---@param index integer
46+
---@param version integer
47+
---@return evolved.id id
48+
function evolved.pack(index, version) end
49+
50+
---@param id evolved.id
51+
---@return integer index
52+
---@return integer version
53+
function evolved.unpack(id) end
54+
```
55+
56+
Here is an little example of how to use identifiers:
57+
58+
```lua
59+
local evolved = require 'evolved'
60+
61+
local id = evolved.id() -- create a new identifier
62+
assert(evolved.alive(id)) -- check if the identifier is alive
63+
64+
local index, version = evolved.unpack(id) -- unpack the identifier
65+
assert(evolved.pack(index, version) == id) -- pack it back
66+
67+
evolved.destroy(id) -- destroy the identifier
68+
assert(not evolved.alive(id)) -- check if the identifier is not alive now
69+
```
70+
71+
## Entities, Fragments, and Components
72+
73+
First of all, we need to understand that entities and fragments are just identifiers. The differences between them are purely semantic. Entities are used to represent objects in the world, while fragments are used to represent types of components that can be attached to entities. Components, in the other hand, are any data that is attached to entities through fragments.
74+
75+
Here is a simple example of how to attach a component to an entity:
76+
77+
```lua
78+
local evolved = require 'evolved'
79+
80+
local entity, fragment = evolved.id(2)
81+
82+
evolved.set(entity, fragment, 100)
83+
assert(evolved.get(entity, fragment) == 100)
84+
```
85+
86+
Yeah, I know, it's not very clear yet. But don't worry, we'll get there. In the next example, I'm going to name the entity and fragment, so it will be easier to understand what's going on here.
87+
88+
```lua
89+
local evolved = require 'evolved'
90+
91+
local player = evolved.id()
92+
93+
local health = evolved.id()
94+
local stamina = evolved.id()
95+
96+
evolved.set(player, health, 100)
97+
evolved.set(player, stamina, 50)
98+
99+
assert(evolved.get(player, health) == 100)
100+
assert(evolved.get(player, stamina) == 50)
101+
```
102+
103+
We have created an entity called `player` and two fragments called `health` and `stamina`. We have attached the components `100` and `50` to the entity through our fragments. After that, we can retrieve the components using the `evolved.get` function.
104+
105+
We'll cover the `evolved.set` and `evolved.get` functions in more detail later in the section about modifying operations. For now, let's just say that they are used to setting and getting components from entities through fragments.
106+
107+
The main thing to understand here is that we can attach any data to any identifiers using another identifiers. And yes, since fragments are just identifiers, we can use them as entities too! This very useful for marking fragments with some metadata, for example.
108+
109+
```lua
110+
local evolved = require 'evolved'
111+
112+
local serializable = evolved.id()
113+
114+
local position = evolved.id()
115+
evolved.set(position, serializable, true)
116+
117+
local velocity = evolved.id()
118+
evolved.set(velocity, serializable, true)
119+
120+
local player = evolved.id()
121+
evolved.set(player, position, {x = 0, y = 0})
122+
evolved.set(player, velocity, {x = 0, y = 0})
123+
```
124+
125+
In this example, we have created a fragment called `serializable` and marked the fragments `position` and `velocity` as serializable. After that, we can write a function that will serialize entities. And this function will serialize only fragments that are marked as serializable. This is a very powerful feature of the library, and it allows us to create very flexible systems. Btw, fragments of fragments are usually called `traits`.

0 commit comments

Comments
 (0)