2012-05-18 27 views
1

小例子,是真正從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值表就足夠了嗎?

編輯:哦,我忘了補充說,問題的主要部分是以另一種方式進行溝通 - 從主人到奴隸。

+1

如果downvoters評論*爲什麼*他們低估了這個問題,我真的很高興。 –

回答

2

stable庫將這些值存儲在名爲_state_persistent_table_的主狀態的全局表中。顯然這意味着隱藏和私密。

如果您對此感到不舒服,stable只是在內部使用remotedostring(),您自己也不難做這樣的事情。

對於master-> slave,使用類似的技術,slave:dostring()就足夠了。

+0

我想我只是將調用包含到'remotedostring'和'slave:dostring'中去成爲一些函數。非常感謝 ;) –