Explicit variable values

Hi Guys, I am tearing my hair out with this one. I tried to post a question like this some time ago and got no joy.

I have several tables that I use as lookup tables. These have city names and x and y coordinates in them. These are working fine. I also have tables that show the relationship between cities - i.e. the amount of stops (turns) between each city etc... I am now writing the AI for my game (a turn based travelling game). My problem is I want to set the table out like:

local AI_London = {
{target = europe.AI_route_London[1].target, first_stop = europe.AI_route_London[1].first_stop, total_stops = europe.AI_route_London[1].total_stops},
}

As well as AI_London, there will be several additional tables (AI_Paris, etc...). I have only put one entry into the table but there will be around 40 for each one.

In my program, I know what the current town is (it could be one of hundreds), and I want to access the relevant table. For instance if the computer player is in Madrid I want to access the AI_Madrid table. I can create a variable such as 'wk_string = "AI_" .. currenttown' and if I print wk_string to the console, it will give me AI_Madrid. My problem is using that to reference the table. Is there some "value" command that I can use. I'm not sure that I've explained myself clearly so please let me know if you need clarification.

Thanks

Adrian

@Adrian9, have a read about tables either from the web or you can also read about them here and here

if you want to just skip all that and just want the solution, then...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
local data = 
{
   London = 
   {
      {
        target = europe.AI_route_London[1].target, 
        first_stop = europe.AI_route_London[1].first_stop,
        total_stops = europe.AI_route_London[1].total_stops
      },
   }
   Madrid = 
   {
      {
        target = europe.AI_route_Madrid[1].target, 
        first_stop = europe.AI_route_Madrid[1].first_stop,
        total_stops = europe.AI_route_Madrid[1].total_stops
      },
   }
}
views:1436 update:2011/11/10 9:30:09
corona forums © 2003-2011