Page 1 of 1

Lua arguments OO:style

Posted: Tue Jun 19, 2007 08:37
by vr
Hi.

Im wondering how people uses the lua scripting for handling callbacks...


A brief example


function colourChangeHandler(args)
local scroller = CEGUI.toScrollbar(CEGUI.toWindowEventArgs(args).window)end

local value = scroller:getScrollPosition()
etc...
end
winMgr:getWindow("root/Window/MySlider"):subscribeEvent("ScrollPosChanged", "colourChangeHandler")

Now what if I would like some more information associated to this slider/callback?

Then I would have to use scope, hope that variables are not removed by garbage collection:

function setupSlider()

local my_car_wheel = createCarWheel()

function colourChangeHandler(args)
local scroller = CEGUI.toScrollbar(CEGUI.toWindowEventArgs(args).window)end

local value = scroller:getScrollPosition()
--
my_car_wheel:setVelocity(value)
etc...
end
winMgr:getWindow("root/Window/MySlider"):subscribeEvent("ScrollPosChanged", "colourChangeHandler")

end

So my question is, how do you all handle "additional data" when it comes to callbacks?

/Anders

Posted: Tue Jun 19, 2007 15:55
by lindquist
CEGUI 0.5 has this functionality. Unfortunately I guess I forgot to document it properly.

Code: Select all

function setupSlider()
  function colourChangeHandler(wheel,args)
    local scroller = CEGUI.toScrollbar(CEGUI.toWindowEventArgs(args).window)

    local value = scroller:getScrollPosition()

    wheel:setVelocity(value)
    --etc...
  end
  winMgr:getWindow("root/Window/MySlider"):subscribeEvent("ScrollPosChanged", colourChangeHandler, createCarWheel())
end


Note that when subscribing inside lua the callback no longer has to be a string, it can be any closure. And a last third parameter can be passed to subscribeEvent which will be passed as the first argument to the callback. This allows you to attach an arbitrary amount of data to the callback (just pass a table if you need more than one thing. Also it allows you to call lua table-member-functions passing the self instance.

HTH

Posted: Sun Dec 30, 2007 16:23
by fooguru
Great stuff man