5. Guide to Python in Civ IV: Iteration Looping

The next section I will cover is iteration, as it is different from other languages and rather important in most CIV modding.

Iteration is basically the process of going through every element in a related set. This is usually done if you want to find a particular object that matches a certain criterion, to set a particular attribute for every object in a set, or other related things. One thing I've used it for is in my Rebellion mod to cycle through every player in the game (and in turn cycling through every city each player has ) in order to find what player to spawn "rebels" for.

An example of iteration through a player's list of cities follows:

# NewObj: Active player
Player = Player.PyPlayer(GC.getActivePlayer().getID())
CyPlayer = CyGlobalContext().getPlayer(GC.getActivePlayer().getID())
# NewObj: Determine player's list of cities
CityList = Player.getCityList()

# NewObj: Loop through each of the active player's cities
for City in CityList:
        # Blah...

The first lines set up my object reference shortcuts (Player and CyPlayer are the same, but reference the Player object in different ways - the first through Jesse's Player wrapper, and the other without it), and the final line is the iteration.

"CityList" is a List (Python object type) that contains City object instances (actual cities) like an array. You could pick out individual cities through their index: CityList[0] grabs the player's first city, while CityList with no index simply refers to the whole structure. "City" is what you're using to store the City object instance that is currently being looked at. The whole line loops through the player's city list, making "City" the current object. Within the for loop structure (after the # blah) you can then do all sorts of things: for example, City.getName() would grab the current city's name. City.hasReligion(0) returns whether or not this city has religion '0' (1 means it does, 0 means it doesn't).

Sometimes you might just want to loop through the INDEX of how many Cities there are - that is, you would want City to be an integer rather than a City object. To do that you modify the line slightly:

for CityIndex in range(CityList):
        # Blah...

"range" allows us to use the length of the structure inside the parenthesis - in this case it gets the length of the player's city list. What this code snippet does is loops through every city index - so "print(CityIndex)" in this case would print out an integer (0 through the length of the list). Note that in the previous example trying "print(City)" will result in an error because while you can print out an integer, you cannot print out a complex City object!

The ability to iterate through objects and lists in this way gives great power in checking things and changing things with many of the things in CIV. Other things you can loop through is the player list, a player's unit list, religions list and more.


1. Guide to Python in Civ IV: Introduction
2. Guide to Python in Civ IV: Basic Python
3. Guide to Python in Civ IV: Classes in Python
4. Guide to Python in Civ IV: Python in Civ IV
5. Guide to Python in Civ IV: Iteration (Looping)
6. Guide to Python in Civ IV: Civ IV Function Assignment
7. Guide to Python in Civ IV: Creating a Unit in Civ IV
8. Guide to Python in Civ IV: Class Reference (by Locutus)
9. Further Class Reference (by Solver)

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License