How to insert a function inside a table

How to insert a function inside a table ?

Tabla={info,info2,info3,info4,300,250,300,350,350,300,250,300, ¿?,¿?,¿?}

for Recorrer = 1, 4,1 do

transition.to( Tabla[Recorrer] , { time=2000, x=Tabla[Recorrer+4], y=Tabla[Recorrer+5], rotation=180, alpha=0, onComplete= Tabla( ¿? )})
end

The idea is to incorporate different onComplete functions.

Do you think this?

local someTable = {data1,data2}
someTable[3] = function ()
....
end

transition.to ( object, { ..., onComplete = someTable[3] } )

Or do you mean something else? What do you try to achieve?

1 Like

The quickest way is to create the table like this:

local myTable = { info1, 300, "string", function() doSomething() end }

but is recommended to create the functions outside the table creation, like in @atanasovskyjiri sample, or like this:

local myFunction = function()
  -- do something 
end

local myTable = { info1, 300, "string", myFunction }

And I personally suggest you that is even better (And safer) to reference all those values with a key

local myTable = {
  x          = info1,
  y          = 300,
  label      = "string", 
  onComplete = myFunction,
}
2 Likes

@fferraro67
I see you want to use numeric indicies, but I think that is a bad idea. It is better to use names instead.

local function onComplete1()
...
end
local function onComplete2()
...
end
...

local options = {}
options[1] = { time = 1000,  x = 100, y = 100 , onComplete = onComplete1 }
options[2] = { time = 1500,  x = 150, y = 150 , onComplete = onComplete2 }
...

Then later,

transition.to( obj, options[1] )

I understand your position, but the idea is to save lines of code. And this is not the way. What’s more … I have serious doubts that there is any stable way that onComplete can use functions from a table. It is something I am testing.

@fferraro67 Who are you responding to. It is hard to tell.

BTW, transition.to() is always using a reference to a function where that reference is stored in a table. The second argument to the function is literally a table. So, be assured you can do this.

Tip: Better than saving lines of code is to keep your code legible. I understand the desire for clean and short, but if you go too extreme you may rue the decision later when you can’t remember what a specific index in the table is used for.

1 Like

I never said that the code did not have to be legible.
There must be a balance: readable and with the fewest number of lines. Without sacrificing reading and facilitating subsequent maintenance.