Help Using A String As A Value In A Key , Pair.

Any idea how I do this?

local gameState{ menuProgress = 1 }

local menuText{ buttonLabel1 = “tempText” }

print ( “buttonLabel”…(gameState.menuProgress) )                     ----------------->   buttonLabel1

print ( menuText.“buttonLabel”…(gameState.menuProgress))    ------------------>  Should Print “tempText” but doesn’t

Thanks!

Gullie

Hi there,

Try the following.

[lua]

local gameState = { menuProgress = 1 }                 – Note that I added an equal sign here

local menuText = { buttonLabel1 = “tempText” }   – Note that I added an equal sign here

print ( “buttonLabel”…(gameState.menuProgress) )   – No change; this should print buttonLabel1

print( menuText[“buttonLabel” … (gamestate.menuProgress)])     – Note that you use a string index by putting it in square brackets

[/lua]

In general, if myTable is a table and “myString” is a string assigned to a variable key, then the following are all equivalent:

myTable[key]

myTable[“myString”]

myTable.myString

However, only the bracket notation will let you manipulate the key, like string concatenation as you were trying to do.

Hope this helps!

  • Andrew

Thanks!

Hi there,

Try the following.

[lua]

local gameState = { menuProgress = 1 }                 – Note that I added an equal sign here

local menuText = { buttonLabel1 = “tempText” }   – Note that I added an equal sign here

print ( “buttonLabel”…(gameState.menuProgress) )   – No change; this should print buttonLabel1

print( menuText[“buttonLabel” … (gamestate.menuProgress)])     – Note that you use a string index by putting it in square brackets

[/lua]

In general, if myTable is a table and “myString” is a string assigned to a variable key, then the following are all equivalent:

myTable[key]

myTable[“myString”]

myTable.myString

However, only the bracket notation will let you manipulate the key, like string concatenation as you were trying to do.

Hope this helps!

  • Andrew

Thanks!