This is a "cookbook" of short, simple snippets of Python code that illustrate how to do some common BF2 programming task. Cookbook items will generally not be complete scripts--see the Scripts section for those. On the other hand, cookbook entries should be more than just the name of a method or event to use--see the Object Reference and the Event Reference for those.
Please contribute your BF2 Python "recipies" to this cookbook! All cookbook entries should follow the format of the O'Reilly Cookbooks: each recipie should have three sections: The Problem, The Solution, and Discussion. Please list your user name as the recipie contributor, as well.
If you have corrections or improvements to these recipies, feel free to update their pages; if you just want to comment about them or ask questions, put those on the appropriate discussion page (push the "discussion" tab at the top of the page).
If you're looking for a general Python cookbook (not specific to BF2), check out the ActiveState Python Cookbook
- Accessing CD Key Hash: How to access the CD key hashes of connected players from within BF2 Python programs.
- Computing CD Key Hashes: How to convert a CD key into the hash used by BF2.
- Changing Objects At Runtime: How to dynamically add or change attributes and methods of objects while a program is running; useful for overriding the behavior of standard BF2 Python code.
- Reloading Modules At Runtime: How to reload a modified module file at runtime, without restarting the server. Perfect for making quick changes.
- Adding New RCon Commands: How to add as many new, custom RCon commands as you would like, without changing any of the standard server files.
- How To Implement Better Error Catching: How to catch errors that Python normally doesn't tell you about.
- Dynamically Changing an Event Handler: Replacing an event handler that the original BF2 Python code has already registered with a new "wrapper" handler that you write, with no modification to original source code required. Uses a fix to the standard BF2 autobalance algorithm as an example.
- Finding the Value of a Server Configuration Variable: How to find out what any server configuration variable (set in
.confiles) is set to.
- Easy Custom Config Variables: An easy way to create and use your own configuration variables, without having to write your own parser or create a new config file.
- Ploting Object Positions on a Map Image: How to convert the X/Y/Z coordinates used by Battlefield 2 so that they can be used to plot points on HUD maps using the Python Imaging Library.