calling functions from dynamically generated strings?

I’m working on an iPad “book” engine and also my horrible Lua skills!

Imagine a stack of pages to the right (unread) and left (read) of the ipad’s screen. Each page turn in this book requires a custom animated transition and no two are alike. I’ve got a bunch of “transition” functions and have put them into a table for easy indexing with the page count.

Is it possible to dynamically create a function name on the fly and call it?
This won’t work…
[lua]-- current page number
local pageCount = 3

–lots of functions with custom page transitions
function drawpage3()
transition.to (page2, {time=1000, x=-1024})
transition.from (page3.art, {time=2000, x=1024})
transition.from (page3.text, {delay=1500, alpha=0, transition=easing.outQuad})
end

– main function to flip pages
function turnpage()
drawpage…pageCount()
end[/lua]

However! I can get this code to work by “hacking” it to using the callback of transition.onComplete method (with a duration of “zero”)

[lua]–a table of all the transition functions (1 per page)
local animData = {drawpage1,drawpage2,drawpage3}

–logic to transition the pages increment the pagecount
–(e.g. page1 becomes oldPg and is positioned off screen to the left as
–page2 is pladed on screen)

function turnpage(oldPg, newPg)
transition.to (oldPg, {time=0, x=-1024})
transition.to (newPg, {time=0, x=0, onComplete=animData[pageCount]})
end[/lua]

Any tips or pointers on my flawed logic appreciated. I’m struggling with understanding functions as first class objects and how or even why to use them with/in tables?
[import]uid: 5339 topic_id: 1918 reply_id: 301918[/import]

hi andy,

the “hack” works because you cannot add parentheses to the onComplete function
animData[3] right below the variable declaration results in error

replace the turnpage function with this:

function turnpage()  
 animData[3]()  
end  

you can also pass arguments to the function (if it expects some), by using

animData[3](param1,param2,...)  

[import]uid: 6459 topic_id: 1918 reply_id: 5684[/import]