Loop Through Random Functions

I am attempting to loop through function randomly as a work around for a game i  am working on but i keep receiving the error " main.lua:53 attempt to get length of upvalue ‘funcTable’ (a function value).

here is a screen shot of the exact error message. https://gyazo.com/2edbe62cfe5291b62973ccec204c04c5

i am not sure what is causing this error or how to fix it.

any help is appreciated!

----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- local backGround = display.newImageRect("HardGameBackground.png", display.contentWidth, display.contentHeight) backGround.x = display.contentCenterX backGround.y = display.contentCenterY math.randomseed(os.time()) local Text1 local Text2 local Text3 local Text4 local Text5 local textLoopTimer local function displayText1 () Text1 = display.newText( "This is Text 1", display.contentCenterX ,display.contentCenterY, native.systemFont, 180) Text1:setFillColor(0,0,0) end local function displayText2 () Text1 = display.newText( "This is Text 2", display.contentCenterX ,display.contentCenterY, native.systemFont, 180) Text1:setFillColor(1,0,0) end local function displayText3 () Text1 = display.newText( "This is Text 3", display.contentCenterX ,display.contentCenterY, native.systemFont, 180) Text1:setFillColor(0,255,0) end local function displayText4 () Text1 = display.newText( "This is Text 4", display.contentCenterX ,display.contentCenterY, native.systemFont, 180) Text1:setFillColor(255,255,0) end local function displayText5 () Text1 = display.newText( "This is Text 5", display.contentCenterX ,display.contentCenterY, native.systemFont, 180) Text1:setFillColor(0,0,255) end local funcTable = ({displayText1,displayText2,displayText3,displayText4,displayText5})[math.random(5)]; local function textLoop() for i = #funcTable, 1, -1 do local thisText = funcTable[i] end end textLoopTimer = timer.performWithDelay( 100, textLoop, 0 )

[lua]

– To setup the table of functions

local funcTable = {displayText1,displayText2,displayText3,displayText4,displayText5}

– To call a random function from it

local myRandomNumber = math.random(1, #funcTable)

funcTablemyRandomNumber

[/lua]

You also need to return Text1 from each of your functions, or thisText will always be equal to nil.

this makes sense and i see the Text1 issue and have corrected it thank you. 

but now im a bit confused on how to incorporate this into the loop so that the program will endlessly load random functions 

Well just literally put those last two lines inside textLoop and don’t bother with the actual for loop. You’ll just be be calling textLoop (which probably should now be renamed) every 100ms and it will call a random function each time.

fantastic this worked perfectly! 

now the next part would be removing the text before the next text is displayed 

Probably a better way is to just create the text object once and change the text, colours and anything else each time.

that would for sure work in this scenario, but the code where im going to attempt to apply this it will be loading images from a folder.

Similarly you can change the .fill property of an image rather than destroy and re-create, but there’s probably not a lot of performance to be gained.

Either way just keep a reference to the image at the top of the lua file, and either display.remove(myImage) and re-create or update .fill.

Thank you for all the help!