• Which the release of FS2020 we see an explosition of activity on the forun and of course we are very happy to see this. But having all questions about FS2020 in one forum becomes a bit messy. So therefore we would like to ask you all to use the following guidelines when posting your questions:

    • Tag FS2020 specific questions with the MSFS2020 tag.
    • Questions about making 3D assets can be posted in the 3D asset design forum. Either post them in the subforum of the modelling tool you use or in the general forum if they are general.
    • Questions about aircraft design can be posted in the Aircraft design forum
    • Questions about airport design can be posted in the FS2020 airport design forum. Once airport development tools have been updated for FS2020 you can post tool speciifc questions in the subforums of those tools as well of course.
    • Questions about terrain design can be posted in the FS2020 terrain design forum.
    • Questions about SimConnect can be posted in the SimConnect forum.

    Any other question that is not specific to an aspect of development or tool can be posted in the General chat forum.

    By following these guidelines we make sure that the forums remain easy to read for everybody and also that the right people can find your post to answer it.

C to XML

Messages
100
Country
italy
I'm sorry my question might seems trivial but I don't know how to do it the right way. I developed a lot of FS gauges but all in C/C++ and never had any close encounter with the XML gauge world...

So I'd have 3 questions about C/C++ <-> XML gauges interaction

1) I have a gauge written in C/C++. I already made an "SDK" to let C/C++ gauge developers be able to read internal variables/states of my gauge.

But if I want to "export" C internal variables to make them available to gauges written in XML, what is THE way to do it? Is there any clarifying example available somewhere?

2) If I "export" a TON of them (100+), should I expect performance issues?

3) Since I already have an SDK for C/C++ gauges, which way is the best:

a) Handle XML-reachable variables "directly" from the C/C++ gauge
or
b) Build a new, separate, C/C++ gauge, using the SDK, and providing C-to-XML bridging [to be used only when needed]

Thank you very much in advance to anybody willing to help me!
Marco
 
Pardon me the question but, if you already know how to make C/C++ gauges, why you need to link to XML gauges ? You are already doing it in the most efficient way so, in any way you do it, you must expect some kind of performance loss, if you "cross the bridge" between C/C++ memory and the XML interpreter.

Maybe you are working with a team where nobody else except you know how to use C++ ?

The only reason why, as a C++ developer, I would think to use some (tiny) amount of XML, is just to drive VC animations, but even then, I'd keep the XML "code" to a very minimum level. In fact, I'll probably have just one line of <Code> to link any object in the VC (or the exterior) model, to an internal simulation, with ALL the logic in C++.

It's also an issue of protecting your time investment in your own code: the XML "language" (which is not even a language, it's more like a database of possible state changes) used by gauges is something that's really FSX-specific. P3D is supporting it but it's also adding LUA script, and other simulators (like XPlane or upcoming ones), use different systems.

Well written (I assume yours is) C++, instead, is a standard language so, the less you use sim-specific code, the easier will be to port it to other platforms, even in the future. If you wrote the most complex and faithful simulation of a complex system (let's say, an FMC) entirely in C++, and interface with the sim just for the trivial things such as writing things on the screen, that code is yours forever and will be easily ported. If, instead, you took the monumental (and highly inefficient) task of doing the same in XML gauge "code", good luck porting it to another sim...

In any case, the performance loss doesn't really depends from the number of variables exported, but more on how many of them and (especially) how OFTEN they are being exchanged between C++ and XML.

So, if you really want to do it, try to be smart, and categorize things depending on required priority of update. Do you need to update all your 100+ variables at *EACH* frame ? Most likely not, so sort them out by update. priority.

You can use the <Lag> parameter to your advantage: if you update a variable every second, with some Lag added to it, it will look just as smooth as if you updated it 30 times per second, and it IS realistic to expect that an analog needle, for example, might have *some* inertia, just because of its own weight...so, be smart and creative, if you really must do it.
 
First of all thank you very much Umberto for your very exhaustive reply. Your advices are exactly what I was looking for.

The example you made fully applies to what I'd like to do.
I'm the author of the Carousel INS gauges, and, for the next release, I'm willing to expand the SDK it comes with, so that internal INS variables (custom/proprietary, not FS variables) become reachable not only from C/C++ (as it is today) but also from XML gauges, for VC or 2D panel.

Each INS unit would require about (just guessing here, I still have to figure out the actual number) 50+ variables, so about 150 for 3 separate units.
All this is valid if I will "export" variables as they are at C/C++ level, but I agree with you, those are really low level and I believe it would be possible to implement a much less sophisticated interface for XML, which would substantially lower the amount of variables required. That's why I was, at least originally, thinking about building a C++ gauge implementing such "higher level" XML logic thus providing C++ to/from XML bridging capabilities, to be used only when required, without having to add any additional code to the INS itself, which is already quite CPU intensive. Would you "endorse" such kind of approach?

> I'll probably have just one line of <Code> to link any object in the VC (or the exterior) model, to an internal simulation, with ALL the logic in C++.

Could you please let me have a link where such <Code> lines are explained? Also, as far as I know, VC code is "linked" within the aircraft model, so I'll not have any chance of doing some experiments/tests without owning the model source, and learning how to compile it, is this correct? Any other possible solution? 2D panel gauge I guess?

Once again thank you very much for your help,
Marco
 
For people to have access to variables from within your C/C++ gauge does not require that you develop an XML anything... even a gauge.

All you need do is 'publish' the variables you want to be seen and viola! there they are.
 
> I'll probably have just one line of <Code> to link any object in the VC (or the exterior) model, to an internal simulation, with ALL the logic in C++.

Could you please let me have a link where such <Code> lines are explained? Also, as far as I know, VC code is "linked" within the aircraft model, so I'll not have any chance of doing some experiments/tests without owning the model source, and learning how to compile it, is this correct? Any other possible solution? 2D panel gauge I guess?

Once again thank you very much for your help,
Marco

I'm a beginner in model, sdk, xml and C++. but I already did what you want to know, make a line on modeldef.xml <Code>(L:Lvar,bool)</Code> (remember the need of modeldef.xml for animation not only one line) then all the logic came from C++ that drive LVars.
 
WarpD, kalong, thank you for your contribution but, unfortunately, the problem is not as simple as it appears.
It's not just to publish one variable to set a needle's position, if I convert the C/C++ interface as it is, I'd have to deal with LOTS of variables and, I suspect, it would be a nightmare to handle them not only from the C/C++ side but also from the XML side. That's why, as I wrote in my previous post, I'm considering writing a C/C++_to_XML converter which could lower the number of variables and make them easier to be handled from the XML counterpart.
Just to make an example: I'll have to publish a string which could contain a couple of dots or apostrophes. In C dots and apostrophes are NOT part of the string itself (as I originally use additional ICONs in the C++ source to display them, not to occupy a whole character space). I believe that rebuilding such string would not be easy from XML, or am I wrong?

Thank you,
Marco
 
It is possible to build the string or use drawing elements for some parts in XML. For the most part, I would supply just the variables that could be supplied to the display, or to needles and indicators and such, and events for certain key input commands like the keypad. When it comes to display logic, ie deciding what to display, that can be handled in XML entirely independent from your gauge.
 
Hi Marco,

First of all let me tell you that you really did a great job with the Carousel INS. So great that long ago inspired me to develop another, this one completely written in XML (triple unit, triple mix logic included).
Fair to say the XML code was complex, though not impossible; I followed you units' behavior and documentation and the final product, like yours, worked a treat. So I have to say thank you for your great contribution, that I suppose is being enjoyed by many FS enthusiasts.

Now going to the point of your interest, actually it could be way more simple than you think it is.
To start with the Virtual Cockpit interface, all that you need is use an XML lvar variable for each one of the keys and dials in the CDU and MSU units, and tie those with your internal variables. Then the code in the MDL file will use those lvars as flags only (ON/OFF-Pos1,Pos2,Pos3, etc). In case of the data display windows, you could use two different approaches: either export each digit status and value in a single lvar, letting the user arrange them in his XML code, or directly export the complete string value to a C string variable through XMLTools module, also letting the user position that string properly (perhaps the simplest way).
For the 2D panels you could keep your currents, or also build a similar interface and let the user develop the visual unit with his own bitmaps.
And don't worry about the amount of LVars needed, they won't be a factor in this case, speaking of FPS performance.


Tom
 
Hi Tom,

> First of all let me tell you that you really did a great job with the Carousel INS. So great that long ago inspired me to develop another, this one completely written in XML (triple
> unit, triple mix logic included).

Thanks a lot but, if I did a great job, then your was outstanding, I never even imagined such "advanced" gauges could be developed using XML!
Is it freeware/is it still available on the internet? I'd like to check it out!

> Then the code in the MDL file will use those lvars as flags only (ON/OFF-Pos1,Pos2,Pos3, etc)

In the link Luka kindly provided I can see that if an LVAR is controlled (read "modified") by the XML side, the way the C side can understand it has been modified is to:

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

so, as far as I understood, I'd have to explicitly check, at each loop or at a given frequency, each and every variable. A sort of "polling" all them.
Is there a more efficient way of having some kind of callback executed when (and only when) it has been modified?
I don't know how fast check_named_variable() and get_named_variable_value() are, but calling them hundred(s) of times, and quite frequently, I fear the resulting code would not be that efficient...

> And don't worry about the amount of LVars needed, they won't be a factor in this case, speaking of FPS performance

Again I imagine LVARS would simply cause some RAM to be used/allocated, that's not a big problem. What scares me is checking tons of them at (almost) each loop.

Marco
 
Marco,

I once made the mistake of creating a module that read approximately 1100 lvars using the method described above. The performance hit was significant. However, that was 1100 lvars. Also, the id number of an lvar will not change over the life of the aircraft, so you can significantly reduce the number of calls to the relatively expensive check_named_variable function by changing the code to this:
if (pitot_cover_id == 0) pitot_cover_id = check_named_variable("PitotCover");
pitot_cover = get_named_variable_value (pitot_cover_id);
This is the approach my sound gauge uses and people have added hundreds of sounds to an aircraft panel without any noticeable performance hit. The sound gauge does read every lvar on every gauge update.

Doug
 
But... he's publishing the variables... not simply reading/writing them. To "own" them requires a different approach.

For published variables that are visible to XML, you need to use register_named_variable to define it, and that returns an ID value for reference to get/set the value. Then you would use set_named_variable_value to control the variables value.

When done, a simple call to unregister_all_named_vars cleans things up as your gauge unloads.
 
Thanks a lot but, if I did a great job, then your was outstanding, I never even imagined such "advanced" gauges could be developed using XML!
Is it freeware/is it still available on the internet? I'd like to check it out!

Yes, they can be done but to make them efficient requires a bit of XML scripting knowledge. Positive side, they are very efficient in terms of FPS performance, which might sound not much logical to some people.
Unfortunately the INS is currently being included in a payware product (DC-8) so I cannot make any part of the code public until the product is released.

so, as far as I understood, I'd have to explicitly check, at each loop or at a given frequency, each and every variable. A sort of "polling" all them.
Is there a more efficient way of having some kind of callback executed when (and only when) it has been modified?
I don't know how fast check_named_variable() and get_named_variable_value() are, but calling them hundred(s) of times, and quite frequently, I fear the resulting code would not be that efficient...

Actually all depends on you. It would become a compromise between making things easier to the XML user or making things easier to you.

For example, and being on your side, you could use a single LVAR for each unit to control the keyboard and switches, and two LVars for each unit to control both displays. A total of three LVars per unit, so 9 LVars in total, literally nothing in terms of resources. As only one key/switch can be operated per cycle, just polling that Key/switch single LVar for a value different than the idle default should work.
BUT, the "negative" side, you'll have to give the XML user a bunch of different codes for each key/switch action he would have to control by himself. Perhaps not quite easy for the majority of your potential users.

On the other hand, and being on the user side, you could define different lvars for each key/switch, with pnemonic names and index (ie (L:INSCDUKey_1:1,bool) for unit #1) and also define an extra LVar that would be the master flag telling you that an action has been made on any of the CDU units, so you would be polling that var only. Etc, etc.

I believe you'll have to figure out the middle point between both sides.

Tom
 
> Also, the id number of an lvar will not change over the life of the aircraft

That was something I wanted to eventually investigate, thanks for confirming it Doug!

> you need to use register_named_variable to define it

You're probably right WarpD, but register_named_variable returns an "ID" as well so I guess Doug's principle is still valid, isn't it?

> Unfortunately the INS is currently being included in a payware product (DC-8) so I cannot make any part of the code public until the product is released.

I fully understand. Planned release date? The usual idiot's question, LOL!

I understood what you meant with your description Tom, each proposed solutions has its positive and negative aspects, for the C/C++ or the XML side.
And I agree the best solution is probably to balance them, 50% on each side :-)

> you'll have to give the XML user a bunch of different codes for each key/switch action he would have to control by himself

Ignorant question: Could those "codes" be supplied through something like constants (#define) as in header files (.H/.HPP) in C/C++? Does XML provides such "facility"?

I feared my original post was not going to receive attention but today it went alive and let me thank you all for this very interesting discussion.
Best of all today I'm at home with a not so bad flu so no work and plenty of time to read, reply and, above all, try to digest all the info I'm receiving!

Marco
 
> Unfortunately the INS is currently being included in a payware product (DC-8) so I cannot make any part of the code public until the product is released.

I fully understand. Planned release date? The usual idiot's question, LOL!

You will find more info in this thread: http://fsdeveloper.com/forum/threads/dc-8-50-series-update.427961/page-7


> you'll have to give the XML user a bunch of different codes for each key/switch action he would have to control by himself

Ignorant question: Could those "codes" be supplied through something like constants (#define) as in header files (.H/.HPP) in C/C++? Does XML provides such "facility"?

Actually the constants (#define) would be on the C++ side, being their equivalent number on the XML one.

For example, using (L:CDU1_Key,number) to receive XML data:

C++ code:

Code:
#define IDLE 9999
#define U1KEY1  101
#define U1KEY2  102
...
#define U2KEYWPCH  2100
...
ID32 idCDU1Input = GetLVAR("CDU1_Key","number");
if( dCDU1Input != IDLE )
{
     DoAnAction(idCDU1Input);
    SetLVAR(IDLE,"CDU1_Key","number");
}
....
....
void DoAnAction(ID32 idValue)
{
   
    switch (idValue)
    {
        case U1KEY1:
        {  
            DoSomething();
            break;
        }
        ........
      }
}
.....
FLOAT64 GetLVAR(PCSTRINGZ szName,PCSTRINGZ szUnits)
{
    return get_named_variable_typed_value(register_named_variable(szName),get_units_enum(szUnits));
}      
......
void SetLVAR(FLOAT64 fValue,PCSTRINGZ szName,PCSTRINGZ szUnits)
{
    set_named_variable_typed_value(register_named_variable(szName),fValue,get_units_enum(szUnits));
}

XML code in modeldef.xml :

Code:
......
    <MouseRect>
      <Cursor>Hand</Cursor>
      <CallbackCode>
           101 (>L:CDU1_Key,number)
      </CallbackCode>
.......

Tom
 
You're probably right WarpD, but register_named_variable returns an "ID" as well so I guess Doug's principle is still valid, isn't it?
No... Doug's function call requests the ID of a variable that already is supposed to exist. Asking for an ID doesn't indicate ownership. Registering a name and obtaining an ID does. Major difference.
 
No... Doug's function call requests the ID of a variable that already is supposed to exist. Asking for an ID doesn't indicate ownership. Registering a name and obtaining an ID does. Major difference.

As far as I'm able to understand, WarpD, you're saying that if a gauge owns an ID then it can be sure given id is valid and remains valid throughout the gauge life cycle while, requesting an id of a named variable created by a different "entity", does not guarantee so because the external entity could have still to register it or could have removed it already.Did I understood correctly?
 
Actually the constants (#define) would be on the C++ side, being their equivalent number on the XML one

So in the XML side values MUST be hardcoded, right? :-(
That's a big limitation for XML gauges (imposed by XML itself I guess) IMHO

BTW I read the thread you pointed me at and all I can say is that DC-8 looks awesome, great job! Will it support also P3D?
 
An additional question for you experts:
Suppose gauge_a registers a named variable and gauge_b check_named_variable's it.
Is it sufficient, in the panel.cfg, to put gauge_a before gauge_b (so having gauge_a a lower "ordinal" number) to guarantee that gauge_b will immediately find the registered named variable?
Or in any case gauge_b will have to deal with check_named_variable() returning an invalid ID (or "failing" if you wish)?
And, if so (un-guaranteed case) is it easier to handle the condition from the C side or the XML side?
What would happen, using this line of the code Tom kindly posted "101 (>L:CDU1_Key,number)" if "CDU1_Key" does not (yet) exist?

Thank you very much in advance as usual!
Marco
 
So in the XML side values MUST be hardcoded, right? :-(
That's a big limitation for XML gauges (imposed by XML itself I guess) IMHO

Actually it is necessary to differenciate between Virtual Cockpit XML schema (coming in modeldef.xml master file) and 2D/panel gauges, which use another, similar one.
In case of gauges, it is possible to define "macros" , snippets of scripting code that accept pnemonic names and can be reused in different places of a single gauge. Unfortunately VC modeldef file does not support macros so here we are indeed faced with an important limitation. Macros are unavoidable if you need to code large and complex gauges in XML.

Suppose gauge_a registers a named variable and gauge_b check_named_variable's it.
Is it sufficient, in the panel.cfg, to put gauge_a before gauge_b (so having gauge_a a lower "ordinal" number) to guarantee that gauge_b will immediately find the registered named variable?
Or in any case gauge_b will have to deal with check_named_variable() returning an invalid ID (or "failing" if you wish)?
And, if so (un-guaranteed case) is it easier to handle the condition from the C side or the XML side?
What would happen, using this line of the code Tom kindly posted "101 (>L:CDU1_Key,number)" if "CDU1_Key" does not (yet) exist?

In C++, once you register a variable by its name, you obtain a unique ID that is shared amongst the entire panelset (C++/XML gauges and also VC MDL scripts).
When a single LVar is first referenced, it is given the ID and a default value of 0. Bear in mind an LVar is not precisely a standard variable but a "record" from a struct array with related members: name, value, unit. To reference an LVar in C++ it is necessary to register it, in XML it is done by a similar internal function and is transparent to the user.

What is nice in C++, when you don't know if the variable has been already registered, you simply call the registering function and if it has an ID previously assigned, the function will return that number.

You might take a look at these functions I use as a standard for assigning/retrieving a value to/from an LVar in all cases, and I don't have to worry whether that name was already defined or not:

Code:
to assign a value:

SetLVAR(100,"CDU1_Key","number");
--
void SetLVAR(FLOAT64 fValue,PCSTRINGZ szName,PCSTRINGZ szUnits)
{
   set_named_variable_typed_value(register_named_variable(szName),fValue,get_units_enum(szUnits));
}
....
To retrieve a value:

double dValue = 10 * GetLVAR("CDU1_Key","number");
--
FLOAT64 GetLVAR(PCSTRINGZ szName,PCSTRINGZ szUnits)
{
   return get_named_variable_typed_value(register_named_variable(szName),get_units_enum(szUnits));
}
....
typecasting:

bool bValue = (bool)GetLVAR("CDU1_Key","number");

etc

This is a simple approach that has the advantage of using a similar, pnemonic style found ins XML scripts, ideal IMHO when dealing with C-XML interfaces.

Tom
 
Back
Top