Perfect, That's what I thought would be easiest! But i'm still really new to LUA, how do i tell the game to search through the players Cities?
Code:
for [B][COLOR="Blue"]pCity[/COLOR][/B] in [B][COLOR="Green"]pPlayer[/COLOR]:[COLOR="Red"]Cities()[/COLOR][/B] do
...
This is the
iterator that you will use in Lua in order to loop over a given player's Cities.
I've highlighted a few sections for you.
'
pCity' in blue is the variable that we are going to store each incremental result from our loop into. This name can be different depending on what notation you use (or it could be 'puppies' for all Lua cares) but it should be able to tell you what you're looking for. In this case, we're looking for what's called the
City object -- it's a table that the game uses to store everything about a particular City.
If
pCity is the
City object, then
pPlayer in green is the
Player object. Just like the City object, the Player object is a table that contains all the details about a particular player.
Then we get to the
Cities() part in red. This is the
method that we are calling, but if you notice, we have a
colon between
pPlayer and
Cities() -- this is effectively telling the game to run a
function on that player object.
Functions you may notice typically have the parentheses around them. They can either be empty, or have several
arguments in them, which are passed into the function to use.
In this case, let's say that a player has 3 Cities.
The above code would then loop repeatedly, and each time
pCity would be different.
On the first loop,
pCity would contain the data for
City#1.
On the second loop,
pCity now contains the data for
City#2.
On the final loop,
pCity contains the data for
CIty#3.
At each loop, you need to have code that tells your function what you want to do to each City, and it will try to apply those same instructions to every City it goes through.
One more note:
pPlayer I mentioned was the
player object -- a table that stores all of the information for a given player.
But.. which player? This is where
iPlayer comes into play -- this would be the
player ID.
In a normal, single-player game, the human player is usually
Player 1. However, internally, player 1's ID number is
0.
In order to pick the right player object, we need to know which player you are interested in. This is accomplished by assigning something to the
pPlayer variable.
Code:
pPlayer = [B][COLOR="Blue"]Players[[/COLOR][COLOR="Red"]iPlayer[/COLOR][COLOR="Blue"]][/COLOR][/B]
Again,
pPlayer can be whatever you want, but if you, for example, change it to
cats, then your code from above needs to read
cats:Cities() instead of
pPlayer:Cities().
In this case, all the players' information is stored in a table simply called
Players.
We are looking through this
Players table, using the bracket syntax, to find the data whose
table index matches our player ID number. Lua then adds a
reference to this data with the name of the variable you chose (
pPlayer in our case.)
Of course, this still leaves a problem -- what is
iPlayer?
Well, you'll need to figure that one out as you begin your function, but sometimes this data is given to you by the game.
One of the most frequently used
event hooks is
GameEvents.PlayerDoTurn(). This event gives you the
iPlayer ID of whatever player's turn is running. All you need to do is define your function to utilize this
argument.
Code:
function MyFunctionName()
function MyFunctionName(iPlayer)
These two define a function called
MyFunctionName, but the first one is not built to rely on any arguments, while the second one relies on the presence of
iPlayer.
If all else fails,
Game.GetActivePlayer() will usually return the ID of the human player, and you can use that to define
iPlayer.
Hope that helps.