Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would I use dynamic variable names?

Asked by
gitrog 326 Moderation Voter
6 years ago

So, I have two tables "trks" and "fabs". "trk" and "fab" are two possible values of a variable I have. If the variable is "trk" I want it to pull from the table "trks", and if it's "fab" I want it to pull from the table "fabs". I tried this, but it was invalid syntax, so I decided to ask y'all.

if hairColChosen > #[hairs[hairChosen] .. "s"] then
    --do the stuff
end

1 answer

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
6 years ago

You don't want to do that -- it would get really confusing (what if it was "game"? what if it was a parameter to your function? etc. it get's messy, and slow)

Instead, you should be pulling from a table.

local choices = {
    trks = "hello",
    fabs = "goodbye",
}


local choice = "trks"
print(choices[choice]) --> hello
0
could i have "hello" and "goodbye" set to another table? gitrog 326 — 6y
0
I wish there was a super syntax, that would be pretty useful saSlol2436 716 — 6y
0
Yes, of course you can put whatever you want inside of the `choices` table. This is just a simple example. BlueTaslem 18071 — 6y
Ad

Answer this question