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
Lua arguments OO:style
Moderators: CEGUI MVP, CEGUI Team
- lindquist
- CEGUI Team (Retired)
- Posts: 770
- Joined: Mon Jan 24, 2005 21:20
- Location: Copenhagen, Denmark
CEGUI 0.5 has this functionality. Unfortunately I guess I forgot to document it properly.
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
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
Return to “CEGUI Library Development Discussion”
Who is online
Users browsing this forum: No registered users and 6 guests