Using variables within variable names

Hi, most of my coding knowledge is in C++, not Lua, and as such I’m not sure how to properly phrase this question. Is there a way achieve something like this example:

i = 1
variable1 = 100
variable2 = 200

print(variable[i])
i = i+1
print(variable[i])

would print
100
200

Is there any way to achieve this within Lua, or is there some alternative way to be able to call variables dynamically without a bunch of if statements / an array?

2 Likes

Well this wouldn’t work because when you use square braces in Lua, that means you’re attempting to access and index of a table or an array

There’s maybe a couple ways to get around and somewhat make this work. But it’s probably better to just straight up write the variable names.

Lua is built off of C by the way. And yes I am aware there are many differences in their (C and C++) syntaxes, I just wanted to mention that.

2 Likes
local variable = {100,200}
local i = 1

print(variable[i]) -- 100
i += 1
print (variable[i]) -- 200
3 Likes

This is a pretty good way. Should’ve provided an example, but this is probably what you’re looking for.

Although this works, I would just recommend just straight up typing the variable name. Unless you have an extremely specific use case

2 Likes
local days = {
day1 = "d1",
day2 = "d2"
}

local dayCount = 1
print(days["day"..dayCount]) -- d1
dayCount += 1
print(days["day"..dayCount]) -- d2

Works in the same way

2 Likes

The issue is when I don’t know which variable would need to be accessed at a given time. For example, it feels like it would be very convenient to do something like:

variable1 = somethinghere
variable2 = somethinghere
variable3 = somethinghere
variable4 = somethinghere

print (variable[math.rand(1,4)])

rather than

variable1 = somethinghere
variable2 = somethinghere
variable3 = somethinghere
variable4 = somethinghere

variableX = math.rand(1,4)

if variableX = 1
print(variable1)
elseif variableX = 2
print(variable2)
elseif variableX = 3
print(variable3)
elseif variableX = 4
print(variable4)

Besides using an array, is there some other way to do this quickly? This is obviously useless example code, but I hope it should illustrate what I mean.

2 Likes

There is not. It would need to be in some sort of table

In your example code you would just make a variable table and index it that way

You shouldn’t be writing your code in a way its ambiguous when you would need to access a variable like that

3 Likes

You could I guess set a global variable and use getfenv to get it and concatenate but why do all that?

This would be math.random(1, 4) in Lua for future reference

This

And this

Need to have a “then” at the end. And then it should autofill and end when you press enter. If it doesn’t, just add end below each one.

If you’ve worked with JavaScript, the end is basically just like the curly braces

So it would be like:

if condition then

elseif condition2 then

end

And if there is an else / elseif line, you don’t need to add another end

And remember to use the == (double equals) operator when you want to check if two things are equal to each other.

The single equals operator = is reserved for setting values

(I am aware that you’ve already marked a solution but I thought I’d just provide some useful info)

3 Likes

Sorry to bring this post back up but I just wanted to share the getfenv example I was talking about:

-- global variables
MyVariable = 5
MyVariable2 = 10
local increment = 1

local res = getfenv(0)[“MyVariable”..increment]
increment += 1
local res2 = getfenv(0)[“MyVariable”..increment]

-- boom it works but don’t do it
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.