Python Cookbook

From BF2 Technical Information Wiki
Jump to navigation Jump to search

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


CD Keys

  • Accessing CD Key Hash: How to access the CD key hashes of connected players from within BF2 Python programs.


Dynamic Programming

  • 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.
  • 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.


Configuration Variables

  • 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.


Maps