Any LUA pros here? - Table - Functions - Parameters

Hi, any skilled LUA programmer here?

Okey, my scenario is that I want to add some functions to a lua table and then execute them.

I am using transition.to and I would like to dynamically change the parameters.

  
-- Variable for the scope  
local tst1 = nil  
  
-- Lua tables  
local tableFirst = {}  
local tableSecond = {}  
  
--Second transition in the table  
tableSecond[1] = {function() transition.to(tst1,{x=100,time=400}); end}  
  
-- Function to shot the second transition above  
local test = function(id)  
 tableSecond[id][1]()   
end  
--First transition in a Lua table  
tableFirst[1] = {function(id) transition.to(tst1,{x=600,time=1700,onComplete = function(obj) test(id) end}); end}  
  
--The object  
tst1 = display.newRect(100,100,100,100)  
  
--Start all transitions  
tableFirst[1][1](1)   
  

So I want to dynamically change the parameters in the both Lua tables. For example x,y and time. This data is fetched from a database on runtime.

Anyone knows how to do this?

Best regards, Joakim [import]uid: 81188 topic_id: 23310 reply_id: 323310[/import]

Ok, I just realized that I can pass in the value by a variable, but how do I pass in the param?

local xLocation = 100  
tableSecond[1] = {function() transition.to(tst1,{x=xLocation,time=400}); end}  

Joakim [import]uid: 81188 topic_id: 23310 reply_id: 93332[/import]

You could have a second corresponding table with params, or make the transition function a property of the first table and add the params as a property of that table. For example:

[lua]tableSecond[1] = {}
tableSecond[1].transitionParams = {x=100, delay=1, time=1000}
tableSecond[1].transitionFunction = {function() transition.to(tst1,tableSecond[1].transitionParams); end}[/lua]

Let me know how that works. And what’s been up with your game? Were you able to make an example for me? [import]uid: 49520 topic_id: 23310 reply_id: 93342[/import]

Hi James,

Thanks for asking, I haven’t got time - i have been coding with a project that needs to be delivered. When thats done, I will send you the code. Your great!

Yes, this looks ten times nicer then what I came up with…but

tableFirst[1] = {}  
tableFirst[1].transitionParams = {x=100, delay=1, time=1000, onComplete=function(obj) test(id) end;}  
tableFirst[1].transitionFunction = {function() transition.to(tst1,tableFirst[1].transitionParams); end}  
tableFirst[1].transitionFunction(1)   
  

I am trying to add the onComplete event with a function that can accept parameters. But I am not sure that I am executing the transition correct by tableFirst[1].transitionFunction(1) ?

Regards, Joakim [import]uid: 81188 topic_id: 23310 reply_id: 93348[/import]

With your code I am getting this error:

attempt to call field ‘transitionFunction’ (a table value)
Joakim [import]uid: 81188 topic_id: 23310 reply_id: 93349[/import]

Try removing the table brackets in front of the function:

[lua]tableFirst[1] = {}
tableFirst[1].transitionParams = {x=100, delay=1, time=1000, onComplete=function(obj) test(id) end;}
tableFirst[1].transitionFunction = function() transition.to(tst1,tableFirst[1].transitionParams); end[/lua]

then you can add some oncomplete params and add them to your transitionParams fields:

[lua]tableFirst[1].onCompleteParams = nil --whatever you want
tableFirst[1].transitionParams = {x=100, delay=1, time=1000, onComplete=function(obj) test(onCompleteParams) end;}[/lua]

Try this and let me know if it works [import]uid: 49520 topic_id: 23310 reply_id: 93353[/import]

Yes, that worked really well, but - yes here comes that but again. I am getting my transition data from a database and the following code is how I managed to solve it. I am not sure if this is the best approach, but it is working. So, I have one more obstacle and that is my function, it returns nil?

  
tst1 = display.newRect(100,100,100,100)  
  
local tableFirst = {}  
  
local test = nil;  
  
test = function(id)  
 print(id)  
end  
local params = {  
 --This would be generated by the recordset from the db....  
 ["x"] = 600,  
 ["time"] = 1700,  
 ["onComplete"] = function(obj) test(id) end  
}  
  
tableFirst[1] = {}  
tableFirst[1].transitionFunction = function() transition.to(tst1,params); end  
tableFirst[1].transitionFunction(1);  

If I do it this way, it returns my value but then I can’t fill the data from the db…

tableFirst[1] = {function(id) transition.to(tst1,{x=600,time=1700,onComplete = function(obj) test(id) end}); end}  

Thanks, Joakim [import]uid: 81188 topic_id: 23310 reply_id: 93359[/import]

Just make params a property of the table and reference it in the oncomplete function. So replace:

[lua]tableFirst[1].transitionParams = {x=100, delay=1, time=1000, onComplete=function(obj) test(id) end;}[/lua]

to something like

[lua]tableFirst[1].transitionParams = {x=100, delay=1, time=1000, onComplete=function() tableFirst[1].params"onComplete end ;}[/lua]

You’ll have to play around with it to get the properties right, but something like that will work [import]uid: 49520 topic_id: 23310 reply_id: 93393[/import]

@Joakim,

did you try making this into a function so that you can apss it all as parameters?

function myFuncWithParams(theObject, delay, duration, x, y)  
 transition.to(theObject, {delay=delay, time=duration, x=x, y=y)  
end  

and call it as

myFunctionWithParams(tst1, 1, 1000, 100, 100) [import]uid: 3826 topic_id: 23310 reply_id: 93413[/import]

@James,

The problem is that I have to dynamically build the transition parameters, as I don’t know what they are. I don’t know if its x,y, alpha,So I can’t manage to work with this approach…

  
tableFirst[1].transitionParams = {db.Param1=db.value1, db.Param2=db.value2, onComplete=function() tableFirst[1].params["onComplete](onCompleteParams) end ;}  

It has to be built up like this otherwise I can’t manage to get the params from the db to work.

tst1 = display.newRect(100,100,100,100)  
   
local tableFirst = {}  
   
local test = nil;  
   
test = function(id)  
 print(id)  
end  
   
   
local params = {  
 --This would be generated by the recordset from the db....  
 [db.param] = db.value,  
 [db.param] = db.value,  
 ["onComplete"] = function(obj) test(db.id) end  
}  
   
tableFirst[1] = {}  
tableFirst[1].transitionFunction = function() transition.to(tst1,params); end  
   
   
tableFirst[1].transitionFunction(1);  
  

Thats the only way I could get this to work with data from the db, but the function is still returning nil for me…I am maybe having the wrong approach, but I am looping five objects here and it seems that my original function messes pup all params. So I was thinking that it is maybe better to put all code in tables, and then execute…

This is my original code that seems to lose the params…

[code]
local singleAnimationfromDB = function ( event , imgname )

local no = 1
local group = {}

if isRunning == false then
isRunning = true

local sqlStr = “”

if( event==“TIME”) then
–Börja med att kolla hur många grupper det finns att animera

for row in db:nrows(“select distinct animGroup, count(*) from Animation where refid=” … animalID … " and swap=0 and isTouch=0 group by animGroup, refid") do
group[#group+1] = row.animGroup
end

if #group > 0 then
local ranGroup = math.random(#group)
sqlStr = “SELECT Animation.image, Animation.expression, Stagedata.layer, Animation.xRef, Animation.yRef, Stagedata.tag from Animation join Stagedata on stagedata.refid = Animation.refid where Animation.animGroup = " … group[ranGroup] … " and Animation.swap=0 and Animation.image = Stagedata.img and Animation.refId=” … animalID
end
elseif(event==“TOUCH”) then

–Hämta upp grupperna om det finns några
for row in db:nrows(“SELECT Animation.image, Animation.expression, Stagedata.layer, Animation.xRef, Animation.yRef, Stagedata.tag, Animation.animGroup from Animation join Stagedata on Stagedata.refid = Animation.refID where Animation.image = '” … imgname … “’ and Stagedata.img = '” … imgname … “’ and Animation.refId=” … animalID) do
group[#group+1] = row.animGroup
end

if #group > 0 then
sqlStr = “Select image,expression,layer,xref,yref,Animation.tag from Animation join Stagedata on Stagedata.refid = Animation.refid where animgroup=” … group[1] … " and Animation.refid = " … animalID …" and Stagedata.img = Animation.image"
end

end

if #group > 0 then

for row in db:nrows(sqlStr) do
–splitta ut den i två strängar beroende på lodstrecket |

local s = row.expression
–print(s)
local value = string.find (s, ‘|’,1,false );

local startString = (s:sub(0,value-1))
local endString = (s:sub(value+1,string.len(s)))

–Bygg upp start parametrarna
startParams = {}
for w in string.gmatch(startString, “%S+”) do
–Splitta varje entry vid = och skapa nya
local value = string.find (w, ‘=’,1,false );
local startString = (w:sub(0,value-1))
local endString = (w:sub(value+1,string.len(w)))
startParams[startString] = tonumber(endString)
end
–Kolla om vi ska ändra ankarpunkten
if(row.xRef~=0 or row.yRef~=0) then
startParams[“xReference”] = row.xRef
startParams[“yReference”] = row.yRef
end

–Bygg upp slut parametrarna
endParams = {}
for w in string.gmatch(endString, “%S+”) do
–Splitta varje entry vid = och skapa nya
local value = string.find (w, ‘=’,1,false );
local startString = (w:sub(0,value-1))
local endString = (w:sub(value+1,string.len(w)))
endParams[startString] = tonumber(endString)
end

local function startAnim( child)
local reset = function()
transition.to(child, endParams)
isRunning = false
end
startParams[“onComplete”] = reset

transition.to(child, startParams)

end

for k=1,refGroup.numChildren do
local child = refGroup[k]
if (child.name == “group” … row.layer) then
for l = 1, child.numChildren do
local semiChild = child[l]
if semiChild.name == row.image … “_” … row.tag then
startAnim(semiChild)
break
end
end
end
end
end
end
end
end

[/code] [import]uid: 81188 topic_id: 23310 reply_id: 93465[/import]