Tutorial 3-9: Saving and loading parameters from file

When the engine is closed, all parameter settings will be discarded and the default parameter settings will be loaded the next time the engine is started. To preserve parameter settings between sessions, the parameter settings must be saved to file before closing the engine and then loaded from file after engine initialization.

The Export function saves the parameters passed to the function to an xml file. The parameters must be first read from the engine using GetParam, GetAxisParam, or similar function. Alternatively, the GetAndExportAll function will read all WMX3 parameter settings from the engine and saves them to an xml file.

These functions should be called before closing the engine.

The Import function reads parameters from an xml file and copies them to the parameter classes passed to the function. The parameters must then be written to the engine using SetParam, SetAxisParam, or similar function. Alternatively, the ImportAndSetAll function will read all WMX3 parameter settings from an xml file and write them to the engine.

The following code saves and loads all parameters to and from file.

Config::SystemParam param;
Config::AxisParam axisParam;

//Import parameters from file
err = wmxlib_cm.config->Import("C:\\MyParam.xml", &param, &axisParam);
if (err == ErrorCode::None) {
    //If the parameters were successfully read from file, attempt to write them to the engine
    err = wmxlib_cm.config->SetParam(&param);
    if (err != ErrorCode::None) {
        wmxlib_cm.ErrorToString(err, errString, sizeof(errString));
        printf("Failed to set parameters. Error=%d (%s)\n", err, errString);
        goto exit;
    }
    err = wmxlib_cm.config->SetAxisParam(&axisParam);
    if (err != ErrorCode::None) {
        wmxlib_cm.ErrorToString(err, errString, sizeof(errString));
        printf("Failed to set axis parameters. Error=%d (%s)\n", err, errString);
        goto exit;
    }
}
else {
    wmxlib_cm.ErrorToString(err, errString, sizeof(errString));
    printf("Failed to import parameters from file. Error=%d (%s)\n", err, errString);
}

//Export parameters to file
err = wmxlib_cm.config->GetParam(&param);
if (err == ErrorCode::None) {
    err = wmxlib_cm.config->GetAxisParam(&axisParam);
    if (err == ErrorCode::None) {
        //If the parameters were successfully read from the engine, attempt to write them to file
        err = wmxlib_cm.config->Export("C:\\MyParam.xml", &param, &axisParam);
        if (err != ErrorCode::None) {
            wmxlib_cm.ErrorToString(err, errString, sizeof(errString));
            printf("Failed to export parameters to file. Error=%d (%s)\n", err, errString);
            goto exit;
        }
    }
    else {
        wmxlib_cm.ErrorToString(err, errString, sizeof(errString));
        printf("Failed to get axis parameters. Error=%d (%s)\n", err, errString);
    }
}
else {
    wmxlib_cm.ErrorToString(err, errString, sizeof(errString));
    printf("Failed to get parameters. Error=%d (%s)\n", err, errString);
}

Note that due to the file operation involved, it may be necessary to run the application as an administrator. Failing to obtain permission to write to file or read from file will cause the Export and Import functions to return an error.

The following code is an alternative implementation for saving and loading all parameters from file using the GetAndExportAll and ImportAndSetAll functions.

//Import Parameters from file
err = wmxlib_cm.config->ImportAndSetAll("C:\\MyParam.xml");
if (err != ErrorCode::None) {
    wmxlib_cm.ErrorToString(err, errString, sizeof(errString));
    printf("Failed to import parameters. Error=%d (%s)\n", err, errString);
    goto exit;
}

//Export parameters to file
err = wmxlib_cm.config->GetAndExportAll("C:\\MyParam.xml");
if (err != ErrorCode::None) {
    wmxlib_cm.ErrorToString(err, errString, sizeof(errString));
    printf("Failed to export parameters. Error=%d (%s)\n", err, errString);
    goto exit;
}