小例子,是真正從Rings main pageLuaRings - 如何訪問在主狀態從屬狀態(由stable.set)設置的值?
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
值表就足夠了嗎?
編輯:哦,我忘了補充說,問題的主要部分是以另一種方式進行溝通 - 從主人到奴隸。
如果downvoters評論*爲什麼*他們低估了這個問題,我真的很高興。 –