Adding local config options

Submitted by talvo on Thu, 2004-03-18 00:19

Many external coders need to have configurable settings which players may change without recompiling the server. In fact, it could be as easy as using the @config command. As of PennMUSH 1.7.7p28 adding your own is easy.

Adding your config

We will be working primarily with the add_config() function, but first we need to setup some global variables to hold the data. Outside of any function declarations, usually near the top of a source file, we will declare some global variables:

int config_happyserver = 1;
char config_favoriteadmin[256] = "grapenut";

Now that we have a place to store our variables, we can go into the local_config() function in src/local.c and add the following:

add_config("happyserver", cf_bool, &config_happyserver,
           sizeof config_happyserver, "cosmetic");
add_config("favoriteadmin", cf_str, config_favoriteadmin,
           sizeof config_favoriteadmin, "cosmetic");

Here's how we break this function down. The first argument is the name of the option, ie, what does it look for in the .cnf file.

The second argument is what data type the option is. Valid types are cf_int, cf_str, and cf_bool.

The third option is a pointer to the variable that will hold the option's data. Notice that the happyserver int variable has a & in front. The favoriteadmin variable is already a pointer, so it doesn't need the & operator.

The fourth option is the size in bytes of the option's variable.

The last variable is simply a string telling which category the option belongs to.

The final step in adding a config option is to check this line:

  hashinit(&local_options, 10, sizeof(CONF));

In this example we have enough space for 10 new config options. This number should be set at or above the number of config options you have added with add_config().

Using new config options

Using local config options in softcode is just like using regular config options - the config() function is your friend.

To access the value of a local config option in hardcode, use the get_config() function (e.g. get_config("happyserver")). get_config() returns a pointer to a CONF structure, so you'll want to learn about them (reading the code for config_list_helper() can be useful here).

Note: get_config() wasn't properly exposed to hardcode files until 1.7.7p29, so upgrade to that version or later before expecting this to work.