XML: Variables in C Gauges

From FSDeveloper Wiki
Jump to: navigation, search


To control XML variables from a C gauge:

// declare the variable and pointer ID for the variable

double pitot_cover = 1 ; // this is the C variable
ID pitot_cover_id; // this is the pointer address to the XML variable

// Register the variable (this is the XML "name," e.g., L:PitotCover

case PANEL_SERVICE_PRE_INITIALIZE:
    register_named_variable ( "PitotCover" );
break;

// now, you can use the next two statements to (a) associate the ID with the XML variable, then (b) set the variable to be identical to the C variable, e.g., pitot_cover

case PANEL_SERVICE_PRE_UPDATE:
    pitot_cover_id = check_named_variable ( "PitotCover" ) ;
    set_named_variable_value ( pitot_cover_id, (FLOAT64)pitot_cover ) ;
break;

// now, in my mouse routine, I can set the value of pitot_cover, and it will be automatically passed to the XML variable!

BOOL FSAPI mouse_pitotcb( PPIXPOINT relative_point, FLAGS32 mouse_flags)
{
    if ( pitot_cover == 0 ) { pitot_cover = 1 ; } else { pitot_cover = 0 ; }
    return FALSE;
}  

// now, to use the XML variable to control the C variable, the only // difference is that you would use the get_named_variable_value command instead, and omit the mouse code!

pitot_cover_id = check_named_variable("PitotCover");
pitot_cover = get_named_variable_value (pitot_cover_id);

Alternate Method

There is another method that may be used to communicate between C gauges and XML gauges, using the execute_calculator_code(); function. Here is the prototype for the function:

execute_calculator_code(PCSTRINGZ code, FLOAT64* fvalue, SINT32* ivalue, PCSTRINGZ* svalue);

"Translating" the function into more easily understood pseudo-code, we have this:

execute_calculator_code(XML variable name,units, floating point var name, integer variable name, string variable name);

To use the function, first determine from the "units" of the XML variable whether the returned value will be a floating point number, an integer, or a string.

When constructing the function call, only ONE of the fields 2 through 4 will be used, an the unused fields filled with "NULL" value. For example, we want to fetch an ADF frequency, so we'd use something like this:

FLOAT64 adf1_frequency = 0; // first declare the float C variable
execute_calculator_code("(A:ADF ACTIVE FREQUENCY:1, KHz)",&adf1_frequency,NULL,NULL);

If the XML variable is an integer, here is an example:

SINT32 time_of_day; // first declare the integer C variable
execute_calculator_code("(E:TIME OF DAY,Enum)",NULL,&time_of_day,NULL);

If the XML variable is a string, here is an example:

PCSTRINGZ VorIcaoCode; // first declare the string C variable
execute_calculator_code("(C:fs9gps:WaypointICAO,st ring)",NULL,NULL,&VorIcaoCode);

Take note that the string example also shows how we can easily fetch stuff from the GPS system using this technique!

Custom L:variables may also be obtained following the same structure shown above.

BOOLEAN:

execute_calculator_code("(L:MyCustomVariable,bool)",NULL,&my_custom_variable,NULL);

FLOAT:

execute_calculator_code("(L:MyCustomVariable,enum)",&my_custom_variable,NULL,NULL);

This method is much easier to use that the method described in the previous section.

To SEND a command with this method, use the following format:

execute_calculator_code("(>K:SOUND_TOGGLE)", NULL, NULL, NULL);

If you need to pass a value for a SET type key_event, simply use the same syntax used in an XML gauge by prefixing the command with the value (which can itself be another XML variable as shown below):

execute_calculator_code("(A:Plane heading degrees gyro,degrees) (>K:HEADING_BUG_SET)", NULL, NULL, NULL);