Civilization IV/Python Events and Interface mods
Events are things which occur due to certain triggers. There is only certain amount of fixed triggers, and this ties down modders slightly, however the triggers will accommodate most scripts. If you open up CvEventManager.py, the event triggers run from line 193 down to line 747 (patch 1.52). There are nearly 60 different triggers that can be used, ranging from onUpdate which runs every frame (probably about 30 times a second), to onGameStart which runs only once, at the start of the game.
Most triggers have arguments associated with them. These arguments are stored in the argsList. Under most event headers these arguments are defined. It's usually pretty obvious what they do.
Perhaps the simplest method of adding events on triggers is to just to add the event into the events manager. This will work for small mods, although it raises certain compatibility issues. A better way of doing it is to create your own CvCustomEventManager file. I recommend Dr Elmer Jiggle's event manager (found here). It's a bit tricky to understand at first, but well worth it if you can get it working. For examples of it in action I'd check out any of TheLopez's ModComps (look in this forum).
 Things to note
- The onEndPlayerTurn trigger does not occur at the end of a player's turn, instead it occurs at the end of the start of a player's turn, after all the cities have built, etc. If you want to do an event on the end of a player's turn you'll probably have to make some code to do it at the start of the next player's turn. It would appear something similar goes on with onBeginGameTurn, and onEndGameTurn, although both occur at the END of the game turn rather than at the beginning like the player ones. Thanks to Kael for spotting this.
- If, for instance, you wanted a message to come up on every player's screen at the start of every game turn (in other words, at the start of the first player's turn), you could replace this code in the event manager:
def onBeginGameTurn(self, argsList): 'Called at the beginning of the end of each turn' iGameTurn = argsList CvTopCivs.CvTopCivs().turnChecker(iGameTurn)
with the following code:
def onBeginGameTurn(self, argsList): 'Called at the beginning of the end of each turn' iGameTurn = argsList CyInterface.addImmediateMessage("You have just started a new turn", "") # Adds the message "You have just started a new turn" with no sound attached. CvTopCivs.CvTopCivs().turnChecker(iGameTurn)
NOTE: When making a formal mod you shouldn't simply add stuff to the event manager like this as it can cause compatibility issues. Check out the custom event manager linked to above.
As mentioned in the introduction, almost all of the GUI is created dynamically either when you enter the relevant screen, or at the start of the game. Just about all of it is moddable. The files for modding the interface are in /Assets/Python/Screens.
 Things to note
- When placing items in the interface that the x and y coordinates that you enter will correspond to the top left hand corner of the item.
- Remember that the resolution can change. It may often be a good idea to change how your interface displays depending on the resolution. To find the X and Y resolutions you can use the code:
CyGInterfaceScreen.getXResolution() or CyGInterfaceScreen.getYResolution()
- If an item needs a name, that name must be unique to that item, or strange things start to happen.
- The way the techchooser script is written can make it quite hard to mod. Instead of being generated every time you load up the screen, the techchooser is instead generated at the start of the game, and then only the colour changes are done in-game. To stop this happening in game, and to ensure that the techchooser shows the mods you make to it while you're game is running I suggest you comment out line 72 (screen.setPersistent(True)). This will make the techchooser be constantly regenerated while open, and although this may cause your computer to slow slightly, it should work better. Sometimes though, due to the way it is generated for techchooser mods you will have to restart the game to see any changes.