• 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 Position user a/c using c# code

connomar

Resource contributor
Messages
267
Country
us-minnesota
Do any of you knowledgeable folks have a code snippet showing how, using C# to position the user aircraft by sending it the lat lon coordinates. The code samples are all C++ of course and life is too short to reinvent the wheel.

I have code which can read the user data, I just want to reverse it.

Thanks
Martin
 
Thanks DragonflightDesign & Jon for responding. I studied that code for ages, but got caught up with the complexities of c++ to c# plus not sending an array. Although I finally got it to work, I struggled with Exception 19 and 20. I ended up with:


private unsafe void PositionAircraft(string title,double lat,double lon)
{
if (!simconnectStatus) return;
//Create managed structure and insert wanted data
var s = new Struct1();
s.title = title;
s.latitude = lat;
s.longitude = lon;


simconnect.SetDataOnSimObject(DEFINITIONS.Struct1, 0, SIMCONNECT_DATA_SET_FLAG.DEFAULT, s);
MessageBox.Show($"Sent aircraft to position {lat},{lon}", "Step Complete", MessageBoxButtons.OK);


}


This code throws Exception 20, BUT it does position the aircraft, so I just ignore the exception. I was expecting to have to allocate memory for the structure and pass the pointer to the function.

Martin
 
It's possible that the Exception 20 is appearing because you appear to be creating the struct on-the-fly. SimConnect tends not to like that: it wants everything in place beforehand. Yes, it sounds awful and it caught me out a few times in all three languages but when you get used to it, it isn't too bad.
 
C# isn't a language... it's an abomination! (runs screaming from the thread)
 
I originally learned C on Unix and I think used Microsoft Version 3 C which came on 10 or more 3.5 inch floppies, but things have moved on faster than my brain.
 
It's possible that the Exception 20 is appearing because you appear to be creating the struct on-the-fly. SimConnect tends not to like that: it wants everything in place beforehand. Yes, it sounds awful and it caught me out a few times in all three languages but when you get used to it, it isn't too bad.
Many thanks for the heads up on creating structures inside the method. That fixed it.

Martin
 
Back
Top