The structure is that I have a gadget with synced state, and a global function in its unsynced part that can retrieve the state. I use a widget to watch the console and parse messages. When the widget retrieves the state by calling the global function from the gadget, it returns what appears to be an empty table.
This is driving me crazy, I appreciate anyone who can help or give me some advice on how to further debug it.
Code is below. I tried to keep the same structure as I had but make it as simple as possible.
A blank project with just this widget and gadget should work, run and type TestArray into the console.
Widget code:
Code: Select all
function widget:GetInfo()
return {
name = "Test Widget",
desc = "Test",
author = "Author",
date = "2012-02-14",
license = "Public Domain",
layer = -1,
enabled = true,
}
end
function widget:AddConsoleLine(msg, priority)
if msg:find("TestArray") then
local array = Script.LuaRules.getState("array")
Spring.Echo("WIDGET check array")
Spring.Echo("type(array) = " .. type(array))
Spring.Echo("type(array[1] = " .. type(array[1]))
end
end
Code: Select all
function gadget:GetInfo()
return {
name = "Test Gadget",
desc = "Test",
author = "Author",
date = "2012-02-07",
license = "Public Domain",
layer = 255,
enabled = true
}
end
if (gadgetHandler:IsSyncedCode()) then
-- SYNCED
function gadget:Initialize()
Spring.Echo("Initializing synced gadget")
_G.state = {
array = { 1, 2, 3 }
}
end
else
-- UNSYNCED
function getStateUnsynced(name)
Spring.Echo("GADGET check array")
Spring.Echo("type(SYNCED.state.array) = " .. type(SYNCED.state.array))
Spring.Echo("type(SYNCED.state.array[1] = " .. type(SYNCED.state.array[1]))
return SYNCED.state.array
end
function gadget:Initialize()
Spring.Echo("Initializing unsynced gadget")
gadgetHandler:RegisterGlobal("getState", getStateUnsynced)
end
end
Spring output:
Code: Select all
[f=0000141] <invalid> TestArray
[f=0000141] GADGET check array
[f=0000141] type(SYNCED.state.array) = table
[f=0000141] type(SYNCED.state.array[1] = number
[f=0000141] WIDGET check array
[f=0000141] type(array) = table
[f=0000141] type(array[1] = nil
[f=0000395] User exited
Thanks in advance to anyone who takes the time to help us out!