-
Notifications
You must be signed in to change notification settings - Fork 32
/
test14.lua
81 lines (62 loc) · 1.45 KB
/
test14.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
local ecs = require "ecs"
local function new_world()
local w = ecs.world()
w:register {
name = "value",
type = "int",
}
w:register {
name = "tag"
}
w:register {
name = "string",
type = "lua",
}
return w
end
local w = new_world()
w:new {
value = 1,
string = "Hello",
}
w:new {
value = 2,
tag = true,
string = "World",
}
for v in w:select "value:in eid:in tag string:in" do
print(v.value, v.eid, v.string)
end
local writer = ecs.writer "temp.bin"
writer:write(w, w:component_id "eid")
writer:write(w, w:component_id "value")
writer:write(w, w:component_id "tag")
local meta = writer:close()
local function print_section(s)
print("offset =", s.offset)
print("stride =", s.stride)
print("n = ", s.n)
end
print_section(meta[1])
print_section(meta[2])
print_section(meta[3])
w:clearall()
local reader = ecs.reader "temp.bin"
local maxid = w:read_component(reader, "eid", meta[1].offset, meta[1].stride, meta[1].n)
local value_n = w:read_component(reader, "value", meta[2].offset, meta[2].stride, meta[2].n)
local tag_n = w:read_component(reader, "tag", meta[3].offset, meta[3].stride, meta[3].n)
reader:close()
assert(maxid >= value_n)
assert(maxid >= tag_n)
w:new {
value = 3,
tag = true,
}
for v in w:select "value:in eid:in tag string?in" do
print(v.value, v.eid, v.string)
end
-- You can use generate_eid instead of reading eid from file
w:generate_eid()
for v in w:select "value:in eid:in tag" do
print(v.value, v.eid)
end