Combine a variable and a string to make another variable?

Hello,

I was wondering if it was possible to do something like:



for i = 1, 6 do
Keys* = KEY_ ..i
end


When I PrintTable keys, I want it to show the value of KEY_1, KEY_2, KEY_3, KEY_4, KEY_5, KEY_6, you get it.

It’s meant to be used with input.IsKeyDown().

Is this possible? If not, is there some sort of workaround?

[lua]a = KEY_
b = 1
key = nil

key = a … b
print( key ) [/lua]
Output: KEY_1

Why would it not work? I don’t think I fully get what you meant.

Will that print KEY_1 as a string, or a variable? it’s a variable I want to call

As a string, but you can use it in a table
[lua]KEYS[ key ][/lua]
As long as the KEYS table has that key.

I already had that in mind, but I want it to work automatically, let’s say the user changes the loop max of 6 to 8, KEY_7 and KEY_8 won’t exist

This may help you out:

They’re called variable variables.

To get the value, you use _G[ string ].

Yours:
[lua]local Keys = { };
for i = 1, 6 do
Keys* = “KEY_” … i
end
print( Keys )[/lua]
[1] = KEY_1
[2] = KEY_2
[3] = KEY_3
[4] = KEY_4
[5] = KEY_5
[6] = KEY_6

Grabbing the variable variable value:
[lua]local Keys = { };
for i = 1, 6 do
Keys* = G[ "KEY" … i ]
end
print( Keys )[/lua]
[1] = 2
[2] = 3
[3] = 4
[4] = 5
[5] = 6
[6] = 7

That worked! thank you!