How do I spawn another aircraft and give it instructions to follow

Hi there, I’m quite new to this. I found a Python Simconnect interface GitHub - odwdinc/Python-SimConnect: Python interface for MSFS2020 SimConnect.dll and I was wondering if it is possible to spawn another aircraft and make it fly with specific instructions. Like lat, lon, hdg, alt, pitch updated very regularly. Is this possible? And if so how can I do it?

Many thanks

1 Like

Yes. that’s possible: the various “AI aircraft” functions are essentially what you are looking for:

Either you assign the aircraft a flight plan (-> AICreateEnrouteATCAircraft) or you create an aircraft that flies by itself (“visual flight rules”). You can also send lat/lon etc. coordinates yourself, in which case you have to “release the AI” (-> AIReleaseControl - in order to fully control the aircraft yourself).

Creating an AI aircraft and getting its assigned ID (for further requests) is asynchronous, so after having created the aircraft you want to “listen” to the various events, specifically:

SIMCONNECT_RECV_ID_ASSIGNED_OBJECT_ID

The answer you get has both your initial client request ID as well as the newly assigned “object ID”.

Some C++ code:

case ::SIMCONNECT_RECV_ID_ASSIGNED_OBJECT_ID:
{
    SIMCONNECT_RECV_ASSIGNED_OBJECT_ID *objectData = (SIMCONNECT_RECV_ASSIGNED_OBJECT_ID*)receivedData;
    auto it = skyConnect->d->pendingAIAircraftCreationRequests.extract(objectData->dwRequestID);
    if (!it.empty()) {
        Aircraft *aircraft = it.mapped();
        aircraft->setSimulationObjectId(objectData->dwObjectID);
       ...

Once you have this “object ID” you can then continue with all other requests (e.g. “Release AI”, assign flight plan or send your own position updates etc.)

Note that if you want to set the AI aircraft’s position yourself you need to create it with SimConnect_AICreateNonATCAircraft (and “release AI”). But also take note that once you have released the AI (and perhaps also “freeze” the aircraft ( KEY_FREEZE_LATITUDE_LONGITUDE_SET, KEY_FREEZE_ALTITUDE_SET, KEY_FREEZE_ATTITUDE_SET → refer to SDK Documentation) you basically need to update the position every simulated frame - otherwise the aircraft will just “stand still” in the air.

And that is the tricky part with which I am currently fighting, too (especially if you have recorded position/attitude/velocity data which might already contain "recorded jitter movements). But even when the original data has been sampled at only 1Hz I get “jittering AI aircraft” - so the problem is probably both “non-smooth/jittery recorded data” in combination with "delayed request messages…

→ If you find a way for smooth playback of AI aircraft - let us know here! (I am not the only one facing this issue currently ;))

I hope that gets you started :slight_smile:

2 Likes

Thank you, that’s very helpful. Much appreciated sir ! :slight_smile:

Hello, did you figure this out? I am trying to do a formation flight using AI but haven’t been able to figure out how to get them airborne. I can spawn in the aircraft and move/angle them the way I want, but I can’t change their altitude. For starters, I’m just trying to get an ai aircraft to fly heading 30 at 1,000feet. But all I can do it place it on the ground/move it on the ground. Any change to the altitude does nothing. My gamertag is hunter ii7 if you have a minute to discuss.

Hi, sorry no, I couldn’t get it to work. I ended up scrapping this project.
Best of luck :slight_smile:

In the Simconnect SDK in “AI Objects”, how should I edit it with Visual Studio or do I simply place the example of each section of the document that is in the MSFS SDK since I need this section https://docs.flightsimulator.com/ html/Programming_Tools/SimConnect/API_Reference/AI_Object/SimConnect_AICreateNonATCAircraft.htm and after that how to install it in MSFS? I would appreciate the response

First of all, developer topics are now discussed in the official developer forum, here:

I am not sure how “active” this user support “SDK” section still is.

Anyway, from the nature of your question I strongly suspect that you are not a developer, is that correct? I don’t mean to be disrespectful, I am just trying to put your question into the right contect.

Because if you are asking what to do with a function declaration (*) like the one you have linked to:

HRESULT SimConnect_AICreateNonATCAircraft(
    HANDLE  hSimConnect,
    const char*  szContainerTitle,
    const char*  szTailNumber,
    SIMCONNECT_DATA_INITPOSITION  InitPos,
    SIMCONNECT_DATA_REQUEST_ID  RequestID
    ); 

then you have a long way to go.

(*) Note that this “code” is not even doing anything: it is simply the declaration - or “signature” - of one given function that is part of the SimConnect application programming interface (API).

The short answer would be: the function SimConnect_AICreateNonATCAircraft will instantiate an AI object within MSFS - when called from your program code that you need to write yourself and then compile e.g. with Visual Studio, or any other C++ compiler / IDE of your choice).

Once you have compiled your executable you need to run it (note that the SimConnect API can also be called from in-process addons such as aircraft, but I have no experience with that: I write an exteral - out of process - application that runs besides MSFS).

It goes without saying that you need to have some basic development skills, need to be able to tell apart C++ code from C# code (because there also exists an official SimConnect C# API, and some of the provided SDK examples are written in C# instead of C++ (or even C), you need to know how to compile an executable, you need to know how to install the appropriate developer tools…

I suggest you install the MSFS SDK (enable developer mode in MSFS, there will be a download link in the developer menu somewhere, including a link to example code) and try to compile the provided (C#) examples with Visual Studio and make them run.

If you want to have a look at a real-world example (in C++) you can clone my app code from here:

(Again, the assumption is that you are familiar with a source versioning system like in this case Git)

Basic instructions how to build Sky Dolly hare here:

But if my initial guess is correct and you have no developer experience whatsoever then your first step should be to either learn C++ or C# (I suggest the later, as C++ is very confusing for beginners) and learn how to compile simple applications: first without a user interface (“command line applications”), then choose a user interface toolkit (or try to learn JavaScript or (preferrably) TypeScript and write a “web application”. There exist also “SimConnect wrappers” for either JavaScript and/or TypeScript, as far as I know (search the web).

Good luck!

Thank you for the late response and sorry for responding after so long. You are correct, I am not a programmer nor do I have much knowledge in that field and I would really like to learn. Thanks for the examples and I’ll try to make it work. Thank you again