Widget not receiving data from unsynced gadget

Widget not receiving data from unsynced gadget

Discuss Lua based Spring scripts (LuaUI widgets, mission scripts, gaia scripts, mod-rules scripts, scripted keybindings, etc...)

Moderator: Moderators

Post Reply
jjcole
Posts: 2
Joined: 14 Mar 2012, 08:32

Widget not receiving data from unsynced gadget

Post by jjcole »

I can't figure out what's going on here, any help is appreciated. I've created some simple examples to recreate the issue, code is below.

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
Gadget code:

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
After running in Spring and typing "TestArray" into the console:

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
The key being that array does contain items in the unsynced part of the gadget, but once the array is returned to the widget, it's empty.

Thanks in advance to anyone who takes the time to help us out!
gajop
Moderator
Posts: 3051
Joined: 05 Aug 2009, 20:42

Re: Widget not receiving data from unsynced gadget

Post by gajop »

widgets and gadgets live in separate memory spaces, you should be doing message passing if you want them to communicate, using:
http://springrts.com/wiki/Lua_UnsyncedC ... LuaMessage
https://github.com/spring/spring/blob/m ... .lua#L1884
and probably:
https://github.com/spring/spring/blob/m ... .lua#L1895
jjcole
Posts: 2
Joined: 14 Mar 2012, 08:32

Re: Widget not receiving data from unsynced gadget

Post by jjcole »

Thanks for clarifying. I've used the messaging for other things, I just didn't realize it was required in this case.

What's strange is that I do the same thing the other way around, my unsynced gadget calls Script.LuaUI.function() and the parameters are passed successfully.

Is it something to do with the object type? For example those ones have been using string parameters, does that mean if I start doing that with tables they will appear empty as in my original problem?
Post Reply

Return to “Lua Scripts”