Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

不是太理解 in out 这些参数的含义。 #4

Closed
Gowa2017 opened this issue Jan 15, 2022 · 6 comments
Closed

不是太理解 in out 这些参数的含义。 #4

Gowa2017 opened this issue Jan 15, 2022 · 6 comments

Comments

@Gowa2017
Copy link

local ecs = require("ecs")

---@type World
local w   = ecs.world()

w:register{ name = "a", type = "int" }
w:register{ name = "area" }

w:new{ a = 8 ,area = true}
w:new{ a = 1}

local function dump(a, str)
  for k, v in pairs(a) do
    print(str, k, v)
  end
end

for v in w:select "a:in" do
    dump(v, 'in')
end

for v in w:select "a:out" do
    dump(v, 'out')
end

这个有什么内在含义,为何会有不同的结果。

@Gowa2017 Gowa2017 changed the title 不是太名字 in out 这些参数的含义。 不是太理解 in out 这些参数的含义。 Jan 15, 2022
@cloudwu
Copy link
Owner

cloudwu commented Jan 15, 2022

in 用来读,out 用来写。你 select out 后读的值是未定义的。

@Gowa2017
Copy link
Author

感谢。我看了一下可以附加的选项有

表示用途的:
in
out 写
update 读与写

表示条件的:
? 可选的组
exist 存在
absent 缺失

new 又有什么含义呢。

@Gowa2017
Copy link
Author

另,在您的blog 里面看到可打 tag, 似乎未看到有api

@cloudwu
Copy link
Owner

cloudwu commented Jan 17, 2022

new 用来创建新的 component ,但是有限制:在 select 之前,该类 component 一个都不存在,select 过程中,依次创建出来。
使用和 output 类似。

tag 就是一种特殊的 component ,所以可以用 out 写入,看作是一个 boolean 。可以用 exist absent 在 select 中筛选。

@Gowa2017
Copy link
Author

抱歉,对 C 不熟。
我明白了 Tag 是一个特殊的,只是一个 boolean 值的 Component。我在您在 test2.lua 中的例子看到:

local ecs = require "ecs"

local w = ecs.world()

w:register {
	name = "value",
	type = "int",
}

w:register {
	name = "mark"
}

w:new {
	value = 1,
	mark = true,
}

w:new {
	mark = true,
}

for v in w:select "mark" do
	w:sync("value?in", v)
	print(v.value)
end

for v in w:select "mark" do
	print(pcall(w.sync, w, "value:in", v))
end

而我用 w:select 'mark:exists' 所得到的应只是一个 {component pool index, component id} 这样的结构,设计上是否就是让 TAG 返回索引,然后用 sync 来将数据读取出来。

_sync 与 _read 的差别是什么?(对 C 真不熟悉)

@cloudwu
Copy link
Owner

cloudwu commented Jan 19, 2022

你如果想 select 包含 mark 这个 tag 且读出 value 字段 ,应该用 w:select "value:in mark" 。mark 和 mark:exist 是等价的。
sync 同时读写,读出 in ,并把 out 写入 C 结构。

sync 是一个补充手段,一般用 select 就可以完成绝大部分功能。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants