How to call a function with a variable

What we are trying to do is - Each time you press a button it calls the function navChecker. From there we would like to call act_sc1. Then when the button is pressed again, act_sc2 is called from navChecker and so on - up to act_sc8.

So we are trying to use the variable scAdvnc to advance the name of the function to be called which we are specifying as the timer listener along with “act_sc”.

This doesn’t produce an error - but nothing happens - the scenes don’t play.

I guess the question is can you even use variables in this way with the timer listener? If you can, what are we doing wrong? Or is there a better way to achieve this? The only thing that can’t change is the format of the functions, sc1 - 8 which have to have the format as shown above - function act_sc1 (event) etc.

Help would be most appreciated.

[blockcode] local scAdvnc = 1

local navChecker = function ()
scAdvnc = scAdvnc + 1
timerStash.timer_sa = timer.performWithDelay( 100, “act_sc”…scAdvnc, 1 )
end

function act_sc1 (event)
– code
end

function act_sc2 (event)
– code
end

– etc up to act_sc8

[import]uid: 166507 topic_id: 36168 reply_id: 336168[/import]

You probably should use a table/array for this:

[code]
local scAdvnc = 1

local act_sc = {}
act_sc[1] = function(event)
– code
end
act_sc[2] = function(event)
– code
end
– etc.

local navChecker = function ()
scAdvnc = scAdvnc + 1
timerStash.timer_sa = timer.performWithDelay( 100, act_sc[scAdvnc], 1 )
end

[/code] [import]uid: 199310 topic_id: 36168 reply_id: 143647[/import]

Thanks Rob. That would work but is there another way to do it without changing the format of the sc functions?

function act_sc1 (event)
– code
end

This is because all those sc functions are being generated that way out of Kwik across multiple pages. It would break the connection to that program if they were to be changed…

Although I could make it:

function act_sc[1] (event)

But that would be the wrong syntax I guess?

Any other ideas? [import]uid: 166507 topic_id: 36168 reply_id: 143649[/import]

function act\_sc1 (event)  
-- code  
end  
  
act\_sc[1] = act\_sc1  
  

Function names are just variables that hold the address of the function (a pointer in C terms) so you can assign them to any variable/table that can hold a memory address.
[import]uid: 199310 topic_id: 36168 reply_id: 143654[/import]

You probably should use a table/array for this:

[code]
local scAdvnc = 1

local act_sc = {}
act_sc[1] = function(event)
– code
end
act_sc[2] = function(event)
– code
end
– etc.

local navChecker = function ()
scAdvnc = scAdvnc + 1
timerStash.timer_sa = timer.performWithDelay( 100, act_sc[scAdvnc], 1 )
end

[/code] [import]uid: 199310 topic_id: 36168 reply_id: 143647[/import]

Thanks Rob. That would work but is there another way to do it without changing the format of the sc functions?

function act_sc1 (event)
– code
end

This is because all those sc functions are being generated that way out of Kwik across multiple pages. It would break the connection to that program if they were to be changed…

Although I could make it:

function act_sc[1] (event)

But that would be the wrong syntax I guess?

Any other ideas? [import]uid: 166507 topic_id: 36168 reply_id: 143649[/import]

function act\_sc1 (event)  
-- code  
end  
  
act\_sc[1] = act\_sc1  
  

Function names are just variables that hold the address of the function (a pointer in C terms) so you can assign them to any variable/table that can hold a memory address.
[import]uid: 199310 topic_id: 36168 reply_id: 143654[/import]

You probably should use a table/array for this:

[code]
local scAdvnc = 1

local act_sc = {}
act_sc[1] = function(event)
– code
end
act_sc[2] = function(event)
– code
end
– etc.

local navChecker = function ()
scAdvnc = scAdvnc + 1
timerStash.timer_sa = timer.performWithDelay( 100, act_sc[scAdvnc], 1 )
end

[/code] [import]uid: 199310 topic_id: 36168 reply_id: 143647[/import]

Thanks Rob. That would work but is there another way to do it without changing the format of the sc functions?

function act_sc1 (event)
– code
end

This is because all those sc functions are being generated that way out of Kwik across multiple pages. It would break the connection to that program if they were to be changed…

Although I could make it:

function act_sc[1] (event)

But that would be the wrong syntax I guess?

Any other ideas? [import]uid: 166507 topic_id: 36168 reply_id: 143649[/import]

function act\_sc1 (event)  
-- code  
end  
  
act\_sc[1] = act\_sc1  
  

Function names are just variables that hold the address of the function (a pointer in C terms) so you can assign them to any variable/table that can hold a memory address.
[import]uid: 199310 topic_id: 36168 reply_id: 143654[/import]

You probably should use a table/array for this:

[code]
local scAdvnc = 1

local act_sc = {}
act_sc[1] = function(event)
– code
end
act_sc[2] = function(event)
– code
end
– etc.

local navChecker = function ()
scAdvnc = scAdvnc + 1
timerStash.timer_sa = timer.performWithDelay( 100, act_sc[scAdvnc], 1 )
end

[/code] [import]uid: 199310 topic_id: 36168 reply_id: 143647[/import]

Thanks Rob. That would work but is there another way to do it without changing the format of the sc functions?

function act_sc1 (event)
– code
end

This is because all those sc functions are being generated that way out of Kwik across multiple pages. It would break the connection to that program if they were to be changed…

Although I could make it:

function act_sc[1] (event)

But that would be the wrong syntax I guess?

Any other ideas? [import]uid: 166507 topic_id: 36168 reply_id: 143649[/import]

function act\_sc1 (event)  
-- code  
end  
  
act\_sc[1] = act\_sc1  
  

Function names are just variables that hold the address of the function (a pointer in C terms) so you can assign them to any variable/table that can hold a memory address.
[import]uid: 199310 topic_id: 36168 reply_id: 143654[/import]