Civ/Civilization IV/Modding/Tutorials/Python Console

Overview
A really helpful feature of Civilization IV is the in-game Python console. This is similar to the interactive mode when you start python without specifying a .py file to run.

From the console you can interactively inspect, modify and debug all the python internals while the game is running. This will be invaluable for python programmers and modders.

Enabling the console
Warning: This section contains a cheat code.


 * 1) Edit the main CivilizationIV.ini file.
 * 2) * Change the "CheatCode = 0" line to "CheatCode = chipotle"

>>
 * 1) Start Civilization IV as per normal.
 * 2) Start a new game, or load an existing game.
 * 3) While in the main view of the game, press Shift-~ to enable the Python console (that is "shift + ö" on German keyboards).
 * 4) * You will be presented with a short version string and a prompt like this:

Useful commands
The Python console is very similar to the standard interactive Python mode. One notable exception is that you need to explicitly print out statements to see their return value.

You can use the arrow keys to go to the next/previous command you typed and to move the cursor left and right along the current line. Pressing return on the end of a logical Python statement executes that statement. Statements that define a block (such as the if statement) will expect more lines to be entered before the command is executed. Indentation is required in Python code blocks.

Here are some useful commands to try out:

>> print 'Hello World'

Prints out the standard 'Hello World' bit.

>> print dir

Prints out an alphabetized list of names comprising (some of) the attributes of the given object, and of attributes reachable from it. In other words, this will list all the classes, modules and variables that are in use at the moment in the current Civilization IV game.

Handy.

>> print dir(gc)

gc is the current game context and contains almost all the information about the current game, such as player info, city info, how many units are in the game, etc. This will list all the attributes of the game context.