Page 1 of 1

Call a function from a Widget in a Gadget

Posted: 10 Feb 2012, 22:22
by super_g
I have a Gadget doing some calculations and I want to call a function that is contained in a Widget. The Gadget has both syced and unsyced components. How can I do this?

Re: Call a function from a Widget in a Gadget

Posted: 10 Feb 2012, 22:34
by zwzsg
Almost anything that a widget can do, an unsynced gadget can do too. Why do you specifically requires a widget? Anyway...





In the widget:

Code: Select all

local function MyWidgetFunction(arg1,arg2)
    -- Do stuff
end

function widget:Initialize()
   widgetHandler:RegisterGlobal("MyWidgetFunction",MyWidgetFunction)
end
In the gadget:

Code: Select all

if gadgetHandler:IsSyncedCode() then
   -- Don't know but don't think you can do it there
else

   function gadget:SomeCallin()
      Script.LuaUI.MyWidgetFunction(arg1,arg2)
   end

end
Please note that:
- Gadget will not get the return value from the widget function (Edit: Now that I rethink of it I'm not sure anymore)
- Player might switch off your widget, potentially breaking things
- Widget to Gadget communication is another topic. Ask if you need it too.
- Synced Gadget to Unsynced Gadget is another topic. Ask if you need it too.


Also, please take a minute to ponder the meaning of "synced" vs "unsynced" with regard to Spring's gadgets and widgets. This will help you greatly!

Re: Call a function from a Widget in a Gadget

Posted: 10 Feb 2012, 23:25
by super_g
That worked perfectly!

Right now to pass information from the Widget to the Gadget I am using messages. I feel that this is not the best way to do this.

Can you register any function form anywhere as global?

I am considering moving more of the functionality into gadgets, where possible.
With respect to the synced and unsynced gadget communication. That would be most helpful to be able to do that in both directions.