creating an array with variables embedded

I have an array that looks like this:

local difficultyLevel = true   if difficultyLevel then     local imagePaths = {         {imgValue="image/easy/pic1.png",theWeight=9},         {imgValue="image/easy/pic2.png",theWeight=2}     } else     local imagePaths = {         {imgValue="image/hard/pic1.png",theWeight=9},         {imgValue="image/hard/pic2.png",theWeight=2}    } end

This works fine. No problems with loading the correct image when I need it. Now instead I want to use a variable within the array to reduce lines of code. For example:

local difficultyLevel = true   if difficultyLevel then     local level = "easy" else     local level = "hard" end   local imagePaths = {   {imgValue="image/"..level.."/pic1.png",theWeight=9},   {imgValue="image/"..level.."/pic2.png",theWeight=2} }

This fails horribly. It as if I can’t use concatenation within the defining of the parts of the array. Is this not possible to do?

I think the reason it fails is you are defining ‘level’ as local within your if…then statement. Therefore the following statement can’t see it and as far as it is concerned, ‘level’ is nil and it will throw up an error when you try and concatenate it.

[lua]

local difficultyLevel = true

local level = “hard”

if difficultyLevel then

    level = “easy”

end

local imagePaths = {

  {imgValue=“image/”…level…"/pic1.png",theWeight=9},

  {imgValue=“image/”…level…"/pic2.png",theWeight=2}

}

[/lua]

Thank Nick. I’ll give it a try tonight when I get home.

There should be

local difficultyLevel = true local imagePaths if difficultyLevel then imagePaths = { {imgValue="image/easy/pic1.png",theWeight=9}, {imgValue="image/easy/pic2.png",theWeight=2} } else imagePaths = { {imgValue="image/hard/pic1.png",theWeight=9}, {imgValue="image/hard/pic2.png",theWeight=2} } end



local difficultyLevel = true local level if difficultyLevel then level = "easy" else level = "hard" end

Because you create local variable inside conditions, there are destroyed after conditions ends.

Thanks all. That was the problem. I have to remember about the declaration of variables.

I think the reason it fails is you are defining ‘level’ as local within your if…then statement. Therefore the following statement can’t see it and as far as it is concerned, ‘level’ is nil and it will throw up an error when you try and concatenate it.

[lua]

local difficultyLevel = true

local level = “hard”

if difficultyLevel then

    level = “easy”

end

local imagePaths = {

  {imgValue=“image/”…level…"/pic1.png",theWeight=9},

  {imgValue=“image/”…level…"/pic2.png",theWeight=2}

}

[/lua]

Thank Nick. I’ll give it a try tonight when I get home.

There should be

local difficultyLevel = true local imagePaths if difficultyLevel then imagePaths = { {imgValue="image/easy/pic1.png",theWeight=9}, {imgValue="image/easy/pic2.png",theWeight=2} } else imagePaths = { {imgValue="image/hard/pic1.png",theWeight=9}, {imgValue="image/hard/pic2.png",theWeight=2} } end



local difficultyLevel = true local level if difficultyLevel then level = "easy" else level = "hard" end

Because you create local variable inside conditions, there are destroyed after conditions ends.

Thanks all. That was the problem. I have to remember about the declaration of variables.