1

I have a lua "animation variable" which has a callback function used in an animation loop.

local av = AnimationVariable(ticker.Position.Y)
...
av:addCallback( ** animation function goes here **)

Skipping over details, this addCallback function is defined as follows in C++:

void LuaUIAnimationVariable::addCallback(luabind::object callback);

and when the animation variable is updated, the callback is executed as such (we call the function with one argument):

luabind::call_function<void>(boost::ref(callback), newValue);

My question is the following: How can I use a member function with addCallback? Assuming I have a Ticker:animate(ypos) function, using addCallback on a Ticker instance addCallBack(ticker:animate) does not compile, and addCallBack(ticker.animate) does not work. I understand that member functions in lua have an implicit "self" first parameter.

Any solution or am I forced to use a global function?

1 Answer 1

4

Not sure if I understand your question, but if you mean a Lua member function, you can use a closure:

av:addCallback(function(yval) ticker:animate(yval) end)
Sign up to request clarification or add additional context in comments.

4 Comments

This assumes that ticker is global, doesn't it? Problem is addCallback is inside a for-loop which goes through all my tickers.
It only assumes that ticker is within scope; whatever ticker refers to at the time the closure is created is what it will refer to whenever you call it.
@num3ric that's why they call it a closure. It captures the variable at the time it's created and remains unaffected if you assign to the variable outside the closure.
@furq: "whatever ticker refers to at the time the closure is created is what it will refer to whenever you call it." That's only true if it is a local variable. If ticker is global, then it will attempt to access the global variable every time the function is called. Lua closures only close over local variables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.