• String to Table Name?
    6 replies, posted
I need to access a certain table depending on the map. So I tried to create a table like this for each map, so lets say I'm on gm_construct [lua] gm_construct = {} --Table Stuff [/lua] And then when the server is loaded I tried to access that table like so [lua] for k,v in pairs(game.GetMap()) do --Code end [/lua] However I forgot game.GetMap() returns a string. Would there be any efficient way to do this other than doing a bunch of if then statements like this [lua] if game.GetMap() == "gm_construct" then for k,v in pairs(gm_construct) do --Code end end if game.GetMap() == "gm_flatgrass" then for k,v in pairs(gm_flatgrass) do --Code end end [/lua] Appreciate any help.
You could do it like this: [lua]local map_tables = {} map_tables["gm_construct"] = {} for k, v in pairs(map_tables[game.GetMap()]) do end[/lua]
Thanks a lot!
[QUOTE=MakeR;40604725]You could do it like this: [lua]local map_tables = {} map_tables["gm_construct"] = {} for k, v in pairs(map_tables[game.GetMap()]) do end[/lua][/QUOTE] Don't forget to check if the index of that table is nil or not or the loop will throw you an error.
So I ran into another problem today and it's which tables again so I'd figure I would just post it here. I can not figure out how to name the index once I get farther into the tables.. I'll show you what I mean. I made this table [lua] test_table = { {{"pos a","pos b","pos c"}, {"pos aa","pos bb","pos cc"}}, {{"pos d","pos e","pos f"}, {"pos dd","pos ee","pos ff"}} } [/lua] Which outputs this [lua] 1: 1: 1 = pos a 2 = pos b 3 = pos c 2: 1 = pos aa 2 = pos bb 3 = pos cc 2: 1: 1 = pos d 2 = pos e 3 = pos f 2: 1 = pos dd 2 = pos ee 3 = pos ff [/lua] I want it to look like this [lua] gm_construct: Team 1: 1 = pos a 2 = pos b 3 = pos c Team 2: 1 = pos aa 2 = pos bb 3 = pos cc gm_flatgrass: Team 1: 1 = pos d 2 = pos e 3 = pos f Team 2: 1 = pos dd 2 = pos ee 3 = pos ff [/lua] Now I know how to index the first name which would be test_table["gm_construct"], but how to I give the inner tables's index a name?
[lua]test_table = { gm_construct = { Team1 = {"pos a","pos b","pos c"}, Team2 = {"pos aa","pos bb","pos cc"} }, gm_flatgrass = { Team1 = {"pos d","pos e","pos f"}, Team2 = {"pos dd","pos ee","pos ff"} } }[/lua]
Thanks I really should construct my table like that, it's much easier to understand.
Sorry, you need to Log In to post a reply to this thread.