• 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.

XML-coding question

rcbarend

Resource contributor
Messages
435
Country
netherlands
Hi,

I have a recurring problem in some of my gauges, which is:
In displaying a numeric value, I'd like the number of decimals displayed to be dependant on the value.
I.e. how to code a value-dependant format specifier line.

Something like:
Code:
If (G:Var1) > 100, (G:Var1) displayed as in:
<String>%((G:Var1))%!d!</String>
 
If (G:Var1) between 10 and 100, (G:Var1) displayed as in:
<String>%((G:Var1))%!3.1f!</String>
 
If (G:Var1) < 10, (G:Var1) displayed as in:
<String>%((G:Var1))%!3.2f!</String>
 
Of course I can use multiple <Element> structures, and test for the (G:Var1) range in a <Visibility> structure for each <Element>, but that gives a lot of duplicated code in each <Element>
 
So I'm looking for a way to code the above in one <String>......</String> structure.

Any suggestions ?
Thanks in advance.....

Rob
 
Hi Rob,

Here's a construction that will work:

Code:
<String>
    %((L:TestVar, number) 100 &gt;)%{if}%((L:TestVar, number))%!d!%{else}
    %(10 100 (L:TestVar, number) rng)%{if}%((L:TestVar, number))%!3.1f!%{else}
    %((L:TestVar, number))%!3.2f!
</String>

I seem to recall that there is a limit of three conditions in something like this ... the example above has two conditions - two {if}...

Hope it helps,

Bob
 
Last edited:
Works like a charm (after removing the Newline characters of course ;) )
Thanks !!!

Rob
 
Code:
      %(45 49 (L:CORE3_Flt_States,number) rng)
      %{if}
        %(0 2 (L:CORE5_Flaps_Position,degree) rng)
        %{if}
          %CLB\t%((L:CORE5_Flaps_Position,degree))%!2d!&#176;\n
          %((L:CORE2_SPD_MCH_Ctrl_type,number) 1 ==)
          %{if}%\{clr3}
            %KTS%\{clr1}\t%((L:VsV1Vr_Optimum_Clb,knots))%!3d!\n
            %Mach\t0.76\n
          %{end}
          %((L:CORE2_SPD_MCH_Ctrl_type,number) 2 ==)
          %{if}
            %\{clr3}
            %Mach%\{clr1}\t0.76\n
            %KTS\t%((L:VsV1Vr_Optimum_Clb,knots))%!3d!\n
          %{end}
          %FL\t%((L:VsV1Vr_Optimum_FL,number))%!3d!\n
        %{else}
          %CLB\t%((L:CORE5_Flaps_Position,degree))%!2d!&#176;\n
          %Vs\t%((L:VsV1Vr_VStall,knots))%!03d! \n
          %Vref\t%((L:VsV1Vr_VRef,knots))%!03d! \n
          %Vm\t%((L:VsV1Vr_Vm,knots))%!03d! \n
        %{end}
      %{end}


You need also an {end}.
And here are 3 {if} nested.

Edi
 
I have an additional query with regard to the tab code. Whilst it works, the code \n is also visible as part of the text string.
How can I get it to disappear?
Code:
<Tooltip>Relative Wind Indicator
\n%((A:AMBIENT WIND DIRECTION,degrees))%!3.0f!&#176;% %((A:AMBIENT WIND DIRECTION,radians) (A:PLANE HEADING DEGREES GYRO,radians) - sin (A:AMBIENT WIND VELOCITY,knots) * )%!3.0f!kts</Tooltip>
 
You need also an {end}.

Actually Bob's code doesn't, because the conditional scope finishes at the end of the script.
Anyway, it is always preferable to use the {end} command for better readability.

Another approach could use {case} conditional, for example:

Code:
<String>%( (L:TestVar, number) s0 l0 100 > 10 100 l0 rng 2 * +)%{case}%{:0}%!3.2f!%{:1}%!d!%{:2}%!3.1f!{end}</String>

Tom

Edit: @vololiberista , try using a single line,
<Tooltip>Relative Wind Indicator\n(etc,etc)
 
Actually Bob's code doesn't, because the conditional scope finishes at the end of the script.
Anyway, it is always preferable to use the {end} command for better readability.

Another approach could use {case} conditional, for example:

Code:
<String>%( (L:TestVar, number) s0 l0 100 > 10 100 l0 rng 2 * +)%{case}%{:0}%!3.2f!%{:1}%!d!%{:2}%!3.1f!{end}</String>

Tom

Edit: @vololiberista , try using a single line,
<Tooltip>Relative Wind Indicator\n(etc,etc)

All that does Tom is to put \n at the beginnig of the next line. I can't find a way to hide the code. It seems to be always treated as text
 
The "\n " and " \t " should not be in your tooltip.

They are only used in Formatted Text Elements :
\n = next line (where several lines of text appear , each one line lower per \n)
\t = horizontal placement of text , each per \t as detailed in the 'Tabs' list of the formatted text header.

From post #6 above ,
try the following.
Code:
<Tooltip>%Relative Wind Indicator%  %((A:AMBIENT WIND DIRECTION,degrees))%!3.0f!&#176;% %((A:AMBIENT WIND DIRECTION,radians) (A:PLANE HEADING DEGREES GYRO,radians) - sin (A:AMBIENT WIND VELOCITY,knots) * )%!3.0f!kts%</Tooltip>

Cheers
Karol
EDIT:
If you require the tooltip to show as 3 vertical lines try ,
Code:
<Tooltip>%Relative Wind Indicator%
%((A:AMBIENT WIND DIRECTION,degrees))%!3.0f!&#176;%
%((A:AMBIENT WIND DIRECTION,radians) (A:PLANE HEADING DEGREES GYRO,radians) - sin (A:AMBIENT WIND VELOCITY,knots) * )%!3.0f!kts%</Tooltip>
 
Last edited:
. . . for better readability...

Another approach could use {case} conditional, for example:

Code:
<String>%( (L:TestVar, number) s0 l0 100 > 10 100 l0 rng 2 * +)%{case}%{:0}%!3.2f!%{:1}%!d!%{:2}%!3.1f!{end}</String>

Tom

Hehehe ... Tom, I will admit only that your script may be more readable, not understandable. :confused:

Clever.

Bob
 
You'll never guess what worked!!
Code:
<Tooltip>Relative Wind Indicator
%((A:AMBIENT WIND DIRECTION,degrees))%!3.0f!&#176;% %((A:AMBIENT WIND DIRECTION,radians) (A:PLANE HEADING DEGREES GYRO,radians) - sin (A:AMBIENT WIND VELOCITY,knots) * )%!3.0f!kts</Tooltip>
   </Mouse>
Boringly simple!!
 
Hehehe ... Tom, I will admit only that your script may be more readable, not understandable. :confused:

Bob,

Shouldn't be much difficult.

First, (L:TestVar, number) is left on the stack and a copy is saved in register #0 with s0

Then boolean maths are employed l0 100 > 10 100 l0 rng 2 * + to obtain a value of 0,1 or 2, using the value previously saved in register #0 and restored with l0

Finally, {case} operator determines which of the format structures will be used to display that value, ie:

%{:0}%!3.2f! will extract (L:TestVar, number) previously left on the stack and display it with 2 decimal positions, when the resulting bool math operation is 0, meaning (L:TestVar, number) is lower than 10

%{:1}%!d! will do the same as above when the resulting bool math operation is 1, meaning (L:TestVar, number) is greater than 100

and so on.

Tom
 
Why {end}? A good question!

1.)
I learned it from someone in an other forum and years ago.

But maybe it's not necesssary?

2.)
One the other hand it can be found in the FS9 GPS.

PHP:
      <Element Name="XTKInfoText">
          <Position X="2" Y="211"/>
          <FormattedText X="50" Y="20" Font="Arial Black" FontSize="13" Adjust="Left" VerticalAdjust="Center" Color="Yellow" Bright="Yes">
           <String>%((A:GPS IS ACTIVE WAY POINT, bool))%{if}%((A:GPS WP CROSS TRK, nmiles) s1 abs d s0 100 >=)%{if}%!d!%{else}%(l0 10 >=)%{if}%!.1f!%{else}%!.2f!%{end}%{end}\{dpl=nm}%(l1 100 * near s0 0 >)%{if}>%{else}%(l0 0 &lt;)%{if}&lt;%{end}%{end}%{end}</String>
          </FormattedText>
      </Element>

Edi
 
3.) I deleted one {end} in a more complex script as shown above - btw this is really simple boolsche algebra - the text was not shown.
i clear words it does not work!

Edi
 
Why {end}? A good question!

1.)
I learned it from someone in an other forum and years ago.

But maybe it's not necesssary?

It is not always necessary.

Let me explain with a couple of basic examples:

<String>%((L:MyVar,bool))%{if} JOHN %{end} IS MY FRIEND</String>

When MyVar = 1, displays JOHN IS MY FRIEND
When MyVar = 0, displays IS MY FRIEND. Had I not written {end}, it would display nothing.

Now

<String>%((L:MyVar,bool))%{if} JOHN %{else} PETER</String>

When MyVar = 1, displays JOHN and when MyVar = 0 displays PETER. In this case {end} is not necessary because the end of the script (</String>) is acting as a limiter thus returning the proper name.

In <String> commands, whenever an {if} or {else} operator is found, the snippet contained will execute until {end} OR the end of the script is reached (with </String>)

In regular scripts, whenever an if{ or els{ operator is found, the snippet contained will execute until a closing bracket } is found OR the end of the script is reached.


Tom
 
That is a very clear explanation, Tom. On the other tentacle, it will never hurt to include {end}, whether needed or not. Right? :cool:
 
It is not always necessary.

Of Course, you are right,Tom!!!
With a simple script you can think about to adding or not to adding the {end}.

But when you think about changing a script at first you have to think "Did i insert all {end}s"
And some of my text scripts reach at 100 lines.

You can discuss this also with } in elements.
I forgot an "}". the element worked fine. Until i started to change the script.

Bill said it in nice words!!!

so i think get used a proper syntax writing.. An "if{" ands with an "}". And an {if} ends with {end}. Wheter it's necessary or not necessary.


Edi
 
Obviously I agree with all of your comment, and personally never forget a closing {end} or }, either for need or readability.

But the point was about being technically necessary, meaning the script doesn't work as expected if {end} or } are not always present. Which is not true, as it works fine in the situation I've described.

I believe it is important to know these (and many others) tech details because they can help to prevent debuggin nightmares.

Tom
 
Tom,
i did miss the point that you wanted to explain the technically point.

I believe it is important to know these (and many others) tech details because they can help to prevent debuggin nightmares.

Absolute!

Edi
 
Thanks guys.

All clear to me now on the {if} {else} {end} and {case} 's ...
And I fully agree not to take shortcuts by leaving out {end} even when it works without.

Cheers, Rob
 
Back
Top