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

FSX C++ and L:Var (read/write)

Messages
74
Country
flag
Hi guys
I'd like to assign L:Var value from my dll gauge. An example :

we got a L:var1 changing when user click somewhere in VC
we read the L:var1 with execute_calculator_code
a L:var1 value goes in my "logic" function
function return a C++ varA

Question: how can I write the C++ varA in L:var2?
And I need to do it dynamically, I mean every time L:var1 changing the value - L:var2 must change as well. Where should go this function, may I use PRE UPDATE case?
 

Thanks
I made everything as there written. But my L:var doesn't want to move
I have part with animcode below
Code:
<PartInfo>
    <Name>test_anim</Name>
    <AnimLength>100</AnimLength>
        <Animation>
            <Parameter>
                <Code>
                        (L:my_test_var, Enum) 10 *                    
                </Code>
            </Parameter>
        </Animation>      
</PartInfo>

And I'm sure that szString are "my_test_number(>L:my_test_var, Enum)" in execute_calculator_code(szString,NULL,NULL,NULL);
So I'm asking why my test_anim doesn't work? any suggestion?

PS
my_test_number changes in dependence of what user want to do
 
You would have needed a blank space for that to work:

Code:
my_test_number (>L:my_test_var, Enum)

In any case set_named_variable_value(); is a faster and more efficient method. Of course, if you are only dealing with a few L:vars there is practically no difference in execution times... :cool:
 
And I'm sure that szString are "my_test_number(>L:my_test_var, Enum)" in execute_calculator_code(szString,NULL,NULL,NULL);
So I'm asking why my test_anim doesn't work? any suggestion?

You can't use a C++ variable name (ie my_test_number) within an XML script in a call to execute_calculator_code as the parser won't recognize it.
As an example, when I want to pass a local C++ var value with that function, I use this other one:

Code:
void vSetXML( FLOAT64 fValue, PCSTRINGZ szScript )
{
    char cTemp[60];
    PCSTRINGZ szSetValue;

    if ( (fValue - (int)fValue) == 0)
        sprintf_s(cTemp,"%d%s",fValue,szScript);
    else
        sprintf_s(cTemp,"%f%s",fValue,szScript);

    szSetValue = cTemp;
    execute_calculator_code(szSetValue,NULL,NULL,NULL);

}

And a call to the function:

Code:
FLOAT64 fMyCVariable;

fMyCVariable = 455.671;

vSetXML( fMyCVariable, " (>L:MyXMLVar,number)");


Tom
 
You can't use a C++ variable name (ie my_test_number) within an XML script in a call to execute_calculator_code as the parser won't recognize it.
As an example, when I want to pass a local C++ var value with that function, I use this other one:

Code:
void vSetXML( FLOAT64 fValue, PCSTRINGZ szScript )
{
    char cTemp[60];
    PCSTRINGZ szSetValue;

    if ( (fValue - (int)fValue) == 0)
        sprintf_s(cTemp,"%d%s",fValue,szScript);
    else
        sprintf_s(cTemp,"%f%s",fValue,szScript);

    szSetValue = cTemp;
    execute_calculator_code(szSetValue,NULL,NULL,NULL);

}

And a call to the function:

Code:
FLOAT64 fMyCVariable;

fMyCVariable = 455.671;

vSetXML( fMyCVariable, " (>L:MyXMLVar,number)");


Tom

Do bare in mind that is not thread safe. Although I highly doubt that SkyLu will be doing multi threading any time soon.

A alternative code that I use for multi threading derived from Umberto tutorial for my own use.

Lvars.h
class LVars
{
public:
LVars(const char* varname) : mNameVar(varname), mValue(0), mID(0)
{
register_named_variable(mNameVar);
mID = check_named_variable(mNameVar);
};

int mID;
double mValue;

void Update();
void Set(double val);
double Get();

private:
const char* mNameVar;

};

In a singleton pattern (up to you to go create it).
LVars* YourVar = new LVars ("MyXMLVar");

Lvars.cpp
void LVars::Update()
{
mValue = get_named_variable_value(mID);
};

double LVars::Get()
{
return mValue;
};

void LVars::Set(double val)
{
mValue = val;
set_named_variable_value ( mID, (FLOAT64)mValue ) ;
};


void GetUpdateatyourhz()
{
Something->YourVar->Update();
}

To use the Var
Something->YourVar->Get();

You can futher adjust the codes to your needs such as "on demand update" to get rid of the update at fixed hz and add delete this for the class destructor.

Note: This is probably not the most efficient code, but I adapt it to my needs so....

Edited to make the purpose of singleton clearer (avoid more than one instantiation of the LVars Class or else it will cause a memory leak if you keep creating new LVars Class)
 
Last edited:
Do bare in mind that is not thread safe. Although I highly doubt that SkyLu will be doing multi threading any time soon.

What do you mean by saying that code used inside a standard gauge, interacting with .mdl variables, is not thread safe?

A alternative code that I use for multi threading derived from Umberto tutorial for my own use.

So, are you implementing multithreading in FSX gauges using a Singleton class?

Tom
 
What do you mean by saying that code used inside a standard gauge, interacting with .mdl variables, is not thread safe?
execute_calculator_code in particular is not thread safe. Thread safety applies to multi-threading applications only (as far as I know, and in this context - multi threaded gauges). So when I say its not thread safe, it means you cant use it in multi threaded gauges. (well actually you can, but then its not the most efficient to code and to run). All C++ gauges are by default single threaded. I believe you can find lots of information about thread safety by googling.

So, are you implementing multithreading in FSX gauges using a Singleton class?
Ermmm no. Singleton class not only apply to multi threading.....
 
execute_calculator_code in particular is not thread safe.
.... So when I say its not thread safe, it means you cant use it in multi threaded gauges. (well actually you can, but then its not the most efficient to code and to run).

I think it makes no sense to build a multithreading platform for standard gauges, but just in case someone would want to do so, any of those PANELSentry import functions (including execute_calculator_code,set/get_named_variable_value, etc) should work providing the multithread environment is properly coded.

Ermmm no. Singleton class not only apply to multi threading.....

Actually the Singleton Pattern is not a class specifically designed to handle multithreading operation. It's main purpose is to ensure that only one instance of an object is created while running an application. AFAIK the example given by Umberto is focused on a single thread implementation, if you need to use it in a multithread environment, locks/mutex should be added for safety reasons.

My apologies to SkyLu for being a bit off-topic.

Tom
 
I think it makes no sense to build a multithreading platform for standard gauges, but just in case someone would want to do so, any of those PANELSentry import functions (including execute_calculator_code,set/get_named_variable_value, etc) should work providing the multithread environment is properly coded.


I didn't recommend using multi threading for such gauges, I merely mentioned it for references purpose, just in case someone uses a forum search and try to find more info as such, they would be able to help themselves.

execute_calculator_code is only safe (from my experience) in using it in the thread where the default(or however you call it) thread is. The main idea of multi threading is to remove any overhead on the default thread so that FS does not wait for the complete execution of the code before moving on.

On the other hand, get_named_variable_value IS thread safe (again from my experience), and hence its safe to call it as and when you need it (one of the important things so you dont get locked up).

Actually the Singleton Pattern is not a class specifically designed to handle multithreading operation. It's main purpose is to ensure that only one instance of an object is created while running an application.

Exactly, and in no where did I mention its exclusive to multi threaded operation.

AFAIK the example given by Umberto is focused on a single thread implementation, if you need to use it in a multithread environment, locks/mutex should be added for safety reasons.

Yes, locks can be used, or (for example you are doing a gauge drawing) when your drawing thread is sleeping, you can call the variables to be updated in another waiting thread that is waiting for the drawing thread to go to sleep(well its kind of a lock to wait for the drawing thread to sleep but you get the drift.).

My apologies to SkyLu for being a bit off-topic.

Tom

Haha, I got carried away as well, my apologies as well.
 
execute_calculator_code is only safe (from my experience) in using it in the thread where the default(or however you call it) thread is. The main idea of multi threading is to remove any overhead on the default thread so that FS does not wait for the complete execution of the code before moving on.
On the other hand, get_named_variable_value IS thread safe (again from my experience), and hence its safe to call it as and when you need it (one of the important things so you dont get locked up).

I still can't find a technical reason for what you are saying. Anyway I guess the discussion is irrelevant in this case as it seems to be a tipical single threaded situation.

Exactly, and in no where did I mention its exclusive to multi threaded operation.

I'm sorry, by your comment: "A alternative code that I use for multi threading derived from Umberto tutorial for my own use" I supposed you were using a Singleton class because of being working with multithreading.

Tom
 
A alternative code that I use for multi threading derived from Umberto tutorial for my own use.

The use of L: vars from C++ was going to be the subject of my next tutorial...:)

In any case, instead of your approach of declaring a pointer to the CNamedVar class ( this is how it was named in my code, I see you stripped it down a bit... ), assigning it with the "new" keyword and then having to remember to dispose it, a more straightforward approach could be:

- declare the variable in the public part of the constructor for the CSimulation singleton inside CSimulation.h

- initialize it either in the initialization list of the same constructor OR in the constructor body

- use it in read or write in any of the Update() functions

For example:

CSimulation.h

Code:
class CSimulation : public CSingleton<CSimulation>
{
friend class CSingleton<CSimulation>;
public:
void Start(void) { mStarted = true; };
void Stop(void) { mStarted = false; };
const bool IsStarted(void) { return mStarted; };

void Update6hz(void);

CNamedVar myVariable;

private:
CSimulation()
: mStarted(false), myVariable( "MyExampleLVar" )
{};

~CSimulation()
{};

bool mStarted;
};

This initializes the variable in the constructor initialization list. As an alternative, it can be initialized in the constructor body too:

Code:
private:
CSimulation()
: mStarted(false)
{
  myVariable = CNamedVar( "MyExampleLVar" );
};

CSimulation.cpp

Code:
void CSimulation::Update6hz(void)
{
Sim->myVariable.Get();
}

With this approach, you don't have to worry about disposing and zeroing of pointers, because all the allocations are made in the Singleton constructor, and all disposal the deregistration is made in the Singleton destructor, which will automatically trigger the calling of all CNamedVar destructors too.
 
Back
Top