-------------------------------------------------------------------------------- -- info -------------------------------------------------------------------------------- function gadget:GetInfo() return { name = "CrosstalkGadget", desc = "Example of gadget<->widget communication.", author = "SpliFF", date = "Sept, 2009", license = "GNU GPL, v2 or later", layer = 0, enabled = true -- loaded by default? } end -------------------------------------------------------------------------------- -- about -------------------------------------------------------------------------------- -- This little demo gadget shows how to pass data to a widget. The concept is fairly -- simple but the techniques are not obvious. -- -- Synced code must always run the same on all computers. However sometimes you -- might wish only some players to get feedback from a synced event. In most cases it is enough -- to pass the data to the unsynced part of the gadget via 'SendToUnsynced'. However in a -- few situations you may want a widget to deal with the data instead. This code demonstrates -- that you can acheive that by first passing the data to unsynced and *then* sending it to the widget. -- In short we use the unsynced code as a proxy to pass the request. -- -- To receive data a widget must first register an interest. CrosstalkWidget is a widget -- that demonstrates the widget side of the message passing. -------------------------------------------------------------------------------- if (gadgetHandler:IsSyncedCode()) then -- synced (all players) -------------------------------------------------------------------------------- function gadget:GameFrame(f) if (f%320) == 0 then -- Send the data to the gadget's unsynced section. We can't send to LuaUI directly from synced code. SendToUnsynced('SendToWidget', 'hi', 'there') -- func name, arg1, arg2, etc... end end -------------------------------------------------------------------------------- else -- unsynced (this player) -------------------------------------------------------------------------------- -- Called from gadget synced. This is our proxy to pass the data on to the widget. local function SendToWidget(_, msg1, msg2) -- Make sure the widget function is still registered. The widget may have been stopped. if (Script.LuaUI('WidgetUpdate')) then -- Call the widget function. Widget must declare 'Update' this via 'widgetHandler:RegisterGlobal' Script.LuaUI.WidgetUpdate(msg1, msg2) -- arg1, arg2, etc... end end function gadget:Initialize() -- Register a function for 'SendToUnsynced'. gadgetHandler:AddSyncAction("SendToWidget", SendToWidget) end -------------------------------------------------------------------------------- end -- of synced/unsynced --------------------------------------------------------------------------------