Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Loop Through Random Functions
Started by veracode Oct 23 2019 09:02 AM

8 replies to this topic

Best Answer veracode , 23 October 2019 - 10:12 AM

 
-- 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)
 
funcTable[myRandomNumber]()
 

 

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 

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

veracode

[GLOBAL: userInfoPane.html]
veracode
  • Enthusiast

  • 37 posts
  • Corona SDK

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 )



[TOPIC: post.html]
#2

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,861 posts
  • Corona SDK

 
-- 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)
 
funcTable[myRandomNumber]()
 

 

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



[TOPIC: post.html]
#3

veracode

[GLOBAL: userInfoPane.html]
veracode
  • Enthusiast

  • 37 posts
  • Corona SDK

  Best Answer

 
-- 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)
 
funcTable[myRandomNumber]()
 

 

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 



[TOPIC: post.html]
#4

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,861 posts
  • Corona SDK

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.



[TOPIC: post.html]
#5

veracode

[GLOBAL: userInfoPane.html]
veracode
  • Enthusiast

  • 37 posts
  • Corona SDK

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 



[TOPIC: post.html]
#6

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,861 posts
  • Corona SDK

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

[TOPIC: post.html]
#7

veracode

[GLOBAL: userInfoPane.html]
veracode
  • Enthusiast

  • 37 posts
  • Corona SDK

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.



[TOPIC: post.html]
#8

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,861 posts
  • Corona SDK

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.



[TOPIC: post.html]
#9

veracode

[GLOBAL: userInfoPane.html]
veracode
  • Enthusiast

  • 37 posts
  • Corona SDK

Thank you for all the help!




[topic_controls]
[/topic_controls]