Lua arguments OO:style

Discussion regarding the development of CEGUI itself - as opposed to questions about CEGUI usage that should be in the help forums.

Moderators: CEGUI MVP, CEGUI Team

vr
Just popping in
Just popping in
Posts: 19
Joined: Thu Oct 12, 2006 20:57

Lua arguments OO:style

Postby vr » Tue Jun 19, 2007 08:37

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

User avatar
lindquist
CEGUI Team (Retired)
Posts: 770
Joined: Mon Jan 24, 2005 21:20
Location: Copenhagen, Denmark

Postby lindquist » Tue Jun 19, 2007 15:55

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

fooguru
Just popping in
Just popping in
Posts: 7
Joined: Wed Sep 19, 2007 19:57

Postby fooguru » Sun Dec 30, 2007 16:23

Great stuff man


Return to “CEGUI Library Development Discussion”

Who is online

Users browsing this forum: No registered users and 11 guests