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?
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:
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.
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