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

[import]uid: 83824 topic_id: 17500 reply_id: 317500[/import]

@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…

[lua]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
},
}
}[/lua]

now accessing this will be as simple as

[lua]local London = data.London
local Madrid = data.Madrid
local londonTarget = London[1].target[/lua]

So place these variables in a hash table, that would make it simpler to access…

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 17500 reply_id: 66441[/import]

Hi JayantV, thanks for your reply. Certainly your way of doing things is simpler and will make things a lot easier. However I think my original question remains. If in your example above I have a variable called wk_string which contains “Madrid” (it could contain any other city name), how can I access the table using wk_string? Is there something like val(wk_string)? I might be missing something simple!

Adrian [import]uid: 83824 topic_id: 17500 reply_id: 66454[/import]

there is nothing *simple* however not my preferred way, you can tinker with the _G and pick up the variables that you want.

there is no eval function in Lua

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 17500 reply_id: 66456[/import]

Thanks again. Not sure what you mean by tinkering with the _G. I am trying to think of a way to access the correct city table by using the contents of wk_string. As you intimate, this is not just a queston of tables but of lua.

If I have 4 variables, var1 = “A”, var2 = “B”, var3 = “C” and var4 = “D”, and I have a string (wk_string) which contains “B”, how can I say “if wk_string == var2” and get the correct answer.

Adrian [import]uid: 83824 topic_id: 17500 reply_id: 66462[/import]

you know that _G is the global variable table.
So if your variables, var1, var2, etc are global, then you can refer to them as

_G.var1

or

local myVar = “var1”
print(_G[myVar])

that is what you were after, I guess, right?

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 17500 reply_id: 66466[/import]

(contd) as for your comparison question,

if you had
var1=“A”
var2=“B”
var3=“C”
var4=“D”

and you want to check if wk_string was equal to var2 then just

print(wk_string == var2) will return a true/false

if you want to loop through to find which one of these has the wk_string then you’d rather use an array

[lua]local data = {“A”,“B”,“C”,“D”}
local index = 1
local i

for _,i in pairs(data) do
if wk_string == i then
print("Found it in " … index)
end
index = index + 1
end[/lua]

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 17500 reply_id: 66467[/import]

The issue I have is the “if wk_string == var2” line. I have setup a test within my app as follows:

***********************************************

local wk_string

wk_string = “AI_” … Player2currenttown … “[1].target”

print ("wk_string = " … wk_string)
print ("AILondon[1].target = " … AILondon[1].target)

if wk_string == AILondon[1].target then
print(“equals”)
else
print (“not equals”)
end

*********************************************************

In the above example wk_string prints out as “AILondon[1].target”.
The next line prints “Dublin” (the data I have put in elsewhere).
Then it prints “not equals”.

The Player2currenttown variable is set to London and the target is Dublin.

I want to be able to use wk_string to search through the table. Otherwise I have to through huge amounts of:

If Player2currenttown == “London” then
If Player2currenttown == “Madrid” then
If Player2currenttown == “Dublin” then
If Player2currenttown == “Zagreb” then

and on and on for every city.

Hope this makes sense!!

Adrian :slight_smile:
[import]uid: 83824 topic_id: 17500 reply_id: 66476[/import]

yes, it will not print equal, despite the fact that wk_string is “AILondon[1].target” and you know the reason why…

AILondon[1].target is not equal to “AILondon[1].target”

I see more and more reason of why you need an array. I can see from your code what you are trying to do… there is a better way to achieve what you are after. At the end it is your call on do you want to change to arrays and make life easier, or continue on this track where you will face one impediment after another.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 17500 reply_id: 66479[/import]

I will go back and look at arrays. I have used arrays extensively in other languages and can’t immediately see how they would help me here. I am not too set in my ways that I can’t learn something new or admit that there is a better way than the way I do it :slight_smile:

Thanks

Adrian [import]uid: 83824 topic_id: 17500 reply_id: 66484[/import]

The reason I pointed the two articles for reading is that in Lua, tables can be Arrays or Dictionary/Hash objects and they can be multi-dimensional and do not have to be symmetrical or ordered (which is different from other languages).

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 17500 reply_id: 66485[/import]