• Table reference by string name
    5 replies, posted
Hi all. Just wondering if there is a way to get get a table into a variable via it's name in string form. I know it can be done for children of nested tables but can it be done for parents? for example: For nested tables i can do: local parent = {child={"stuff"}} print( parent["child"] ) // stuff Want i want to do: local myTable = {"stuff"} local iWant = "myTable" local myTableAgain = iWant to table? print( iWant[1] ) // stuff
Maybe debug.getlocal
You can make something like this - local tbl = { "Hello","World" } and after that if u want to use with this table in another name it like that - local tbl = { "Hello","World" } local iWant = tbl to print the first one in the table you need to do something like that - local tbl = { "Hello","World" } local iWant = tbl print( iWant[1] ) another way - local tbl = { [1] = { "Hello" }, [2] = { "World" }, } local iWant = tbl for k , v in pairs( iWant ) do print( v ) end this print all the table in your console
I already know how to do this, as i showed in my opening post. What i need is to access a table (global or otherwise) via a string of its name. local myTable = {"stuff"} local iWant = "myTable" In other words, i need to access "stuff" from myTable by using only the iWant variable.
If it's a local variable as in the example code then use debug.getlocal in a helper function as @DEBUNKED purposed. If it's a global then you can just index the _G table. You shouldn't have to do either of these and should probably nested it within another table.
Yeah, that's what i figured. I was hoping there was a hack to prevent me from remaking a ton of tables in my gamemode as nested. Thanks for the info people.
Sorry, you need to Log In to post a reply to this thread.