assembling text and number to make a variable

hi, 

 

i would to know if it’s possible to generate a variable with a text and a number ?

 

For example the variable level1 to make level1()

 

main.lua ---------------------- level = require "level" level1() local character =  display.newCircle( 100, 100, 30 ) background.touch = function( event )         if event.phase == "began" then transition.to( character, {time=10, x=event.x, y=event.y} )         end end Runtime:addEventListener( "touch", background.touch) level.lua ----------------------------- function level1() background = display.newRect( 0, 0, 320, 480 ) background:setFillColor( 1, 0, 0 ) background.x=160 background.y=240 return background end function level2() background = display.newRect( 0, 0, 320, 480 ) background:setFillColor( 0, 1, 0 ) background.x=160 background.y=240 return background end return background

thanks for your response.

No, but you can use tables.
For example:
 

local levelFunctions = {}; levelFunctions[1] = function() ... end levelFunctions[2] = function() ... end

Hi,

Yes, i have already tried but how can i display this in my main.lua ?

When i write in my main.lua

levelFunctions[2]

My background don’t appear

Try this, assuming you have set up your functions in the same way that RagdogStudios suggested:

levelFunctions[2]()

thanks, i have tried but it’s return : attempt to index global ‘levelFunctions’ (a boolean value)

--main.lua levelFunctions = require "level" levelFunctions[2]() local character =  display.newCircle( 100, 100, 30 ) background.touch = function( event )         if event.phase == "began" then transition.to( character, {time=10, x=event.x, y=event.y} )         end end Runtime:addEventListener( "touch", background.touch)

--level.lua local levelFunctions = {}; levelFunctions[1] = function() background = display.newRect( 0, 0, 320, 480 ) background:setFillColor( 1, 0, 0 ) background.x=160 background.y=240 return background end levelFunctions[2] = function() background = display.newRect( 0, 0, 320, 480 ) background:setFillColor( 0, 1, 0 ) background.x=160 background.y=240 return background end

oups sorry i have forgot this “return levelFunctions”

thanks for your help, problem solved

--level.lua local levelFunctions = {}; levelFunctions[1] = function() background = display.newRect( 0, 0, 320, 480 ) background:setFillColor( 1, 0, 0 ) background.x=160 background.y=240 return background end levelFunctions[2] = function() background = display.newRect( 0, 0, 320, 480 ) background:setFillColor( 0, 1, 0 ) background.x=160 background.y=240 return background end return levelFunctions

 

No, but you can use tables.
For example:
 

local levelFunctions = {}; levelFunctions[1] = function() ... end levelFunctions[2] = function() ... end

Hi,

Yes, i have already tried but how can i display this in my main.lua ?

When i write in my main.lua

levelFunctions[2]

My background don’t appear

Try this, assuming you have set up your functions in the same way that RagdogStudios suggested:

levelFunctions[2]()

thanks, i have tried but it’s return : attempt to index global ‘levelFunctions’ (a boolean value)

--main.lua levelFunctions = require "level" levelFunctions[2]() local character =  display.newCircle( 100, 100, 30 ) background.touch = function( event )         if event.phase == "began" then transition.to( character, {time=10, x=event.x, y=event.y} )         end end Runtime:addEventListener( "touch", background.touch)

--level.lua local levelFunctions = {}; levelFunctions[1] = function() background = display.newRect( 0, 0, 320, 480 ) background:setFillColor( 1, 0, 0 ) background.x=160 background.y=240 return background end levelFunctions[2] = function() background = display.newRect( 0, 0, 320, 480 ) background:setFillColor( 0, 1, 0 ) background.x=160 background.y=240 return background end

oups sorry i have forgot this “return levelFunctions”

thanks for your help, problem solved

--level.lua local levelFunctions = {}; levelFunctions[1] = function() background = display.newRect( 0, 0, 320, 480 ) background:setFillColor( 1, 0, 0 ) background.x=160 background.y=240 return background end levelFunctions[2] = function() background = display.newRect( 0, 0, 320, 480 ) background:setFillColor( 0, 1, 0 ) background.x=160 background.y=240 return background end return levelFunctions