如何在主状态中访问在从状态中设置的值(通过stable.set)?

最小示例实际上是来源于Rings主页的第二个示例。

require"rings"

local init_cmd = [[
require"stable"]]

local count_cmd = [[
count = stable.get"shared_counter" or 0
stable.set ("shared_counter", count + 1)
return count
]]

S = rings.new () -- new state
assert(S:dostring (init_cmd))
print (S:dostring (count_cmd)) -- true, 0
print (S:dostring (count_cmd)) -- true, 1
S:close ()

S = rings.new () -- another new state
assert (S:dostring (init_cmd))
print (S:dostring (count_cmd)) -- true, 2
S:close ()

然而,我无法获取shared_counter的值。print(shared_counter)输出nil

我尝试使用stable.get(),但它说stable仅可在从属状态中使用。 最后我尝试了

remotedostring("shared_counter = "..count)

它起作用了,但我不确定这是否是正确的方法。我想直接访问stable值表就足够了?

编辑:哦,我忘记要补充的是,问题的主要部分是另一种通信方式——从主状态到从属状态。

原文链接 https://stackoverflow.com/questions/10651738

点赞
stackoverflow用户15996
stackoverflow用户15996

stable 库将数值存储在主状态下名为 _state_persistent_table_ 的全局表中。尽管显然这是隐藏和私有的。

如果您对此感到不舒服,stable 内部只是使用了 remotedostring(),您也可以像这样做。

对于主->从,使用类似技术,slave:dostring() 应该就足够了。

2012-05-19 12:07:51