Cookbook:Finding the Value of a Server Configuration Variable

From BF2 Technical Information Wiki
Jump to navigation Jump to search


Many configuration options for BF2 are read from .con files that can be edited by the server administrator. You are writing a cool Python expansion for BF2, but for your code to work you need to know the values of some of these configuration parameters; for example, sv.autoDemoHook.


It turns out that if you type the name of a configuration variable into the server console window, the server will respond with the value of that variable, with a newline character appended to it for good measure. You can use the host.rcon_invoke method from within your Python program to simulate the same thing, and then use the string strip() method to get rid of the extraneous newline.

The following function encapsulates this approach:

def serverConfig(variableName):
    return host.rcon_invoke(variableName).strip()

To use it, just call the function:

 hook = serverConfig("sv.autoDemoHook") # Sets hook to "adminutils/demo/rotate_demo.exe" 

CAUTION: this serverConfig function always returns a string value; if the variable you want to read is numeric (for example, sv.serverPort) you will need to apply int() or a related function to convert the returned string to a numeric value:

 port = int(serverConfig("sv.serverPort")) # Sets port to numeric 16567 


This technique only works to read standard server configuration variables; the BF2 engine won't allow you to specify arbitrary custom configuration variables in .con files, so you can't use it for that purpose. See Easy Custom Config Variables for an alternative that would work for custom variables.

Submitted By

--Woody, based on an approach discovered by King of Camelot