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

generate ai object

Messages
42
Country
unitedkingdom
hi guys

Im new to sim connect just wondering if you could make a code that uses sim connect to draw a ai object when a specified key is pressed that starts where your aircraft is then tracks 1000m in front then goes to the ground?
just a thought I have had

thanks
 
The SDK provides all the documentation and samples you need for that purpose


The SimConnect_AICreateEnrouteATCAircraft function is used to create an AI controlled aircraft that is about to start or is already underway on its flight plan, http://msdn.microsoft.com/en-us/library/cc526983.aspx#SimConnect_AICreateEnrouteATCAircraft

The SimConnect_AICreateNonATCAircraft function is used to create an aircraft that is not flying under ATC control (so is typically flying under VFR rules).
http://msdn.microsoft.com/en-us/library/cc526983.aspx#SimConnect_AICreateNonATCAircraft

The SimConnect_AICreateSimulatedObject function is used to create AI controlled objects other than aircraft.
http://msdn.microsoft.com/en-us/library/cc526983.aspx#SimConnect_AICreateSimulatedObject
 
Last edited:
ok thanks for that I have done this code and when I run the exe file it just opens then closes immediately just wondering if something is up the the code
To get that i have changed the code form the example ai aircraft one

Code:
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <strsafe.h>

#include "SimConnect.h"

int     quit = 0;
HANDLE  hSimConnect = NULL;
DWORD	HellfireID	= SIMCONNECT_OBJECT_ID_USER;

static enum EVENT_ID {
	EVENT_SIM_START,
	EVENT_Insert,	
};

static enum DATA_REQUEST_ID7 {
	REQUEST_Hellfie1,
};

static enum GROUP_ID {
	GROUP_ZX,
};

static enum INPUT_ID {
	INPUT_ZX,
};

struct structHellfireControl 
{
	double throttlePercent;
};

structHellfireControl		bc;

// Set up flags so these operations only happen once
static bool plansSent		= false;
static bool	objectsCreated	= false;

void sendFlightPlans()
{

	wp[0].Flags		= SIMCONNECT_WAYPOINT_SPEED_REQUESTED;
	wp[0].Altitude	= 200;
	wp[0].ktsSpeed	= 100;

	wp[1].Flags		= SIMCONNECT_WAYPOINT_SPEED_REQUESTED;

	wp[2].Flags		= SIMCONNECT_WAYPOINT_WRAP_TO_FIRST | SIMCONNECT_WAYPOINT_SPEED_REQUESTED;
	wp[2].Altitude	= 200;
	wp[2].ktsSpeed	= 200;

	// Send the waypoints to the Hellfire
	hr = SimConnect_SetDataOnSimObject(hSimConnect, DEFINITION_WAYPOINT, HellfireID, 0, ARRAYSIZE(wp), sizeof(wp[0]), wp);


}

void setUpSimObjects()
{
	SIMCONNECT_DATA_INITPOSITION Init;
	HRESULT hr;

    
	// Initialize Mooney aircraft just in front of user aircraft

	Init.Altitude   = 200.0;				// Altitude of Sea-tac is 200 feet
    Init.Pitch      =  -0.2;
    Init.Bank       =  0.0;
    Init.OnGround   = 0;
    Init.Airspeed	= 1;
	
	hr = SimConnect_AICreateNonATCAircraft(hSimConnect, "Mooney Bravo", "N1001", Init, REQUEST_Hellfire1);

void CALLBACK MyDispatchProcSO(SIMCONNECT_RECV* pData, DWORD cbData, void *pContext)
{   
	HRESULT hr;

    switch(pData->dwID)
    {
        case SIMCONNECT_RECV_ID_EVENT:
        {
            SIMCONNECT_RECV_EVENT *evt = (SIMCONNECT_RECV_EVENT*)pData;

            switch(evt->uEventID)
            {
				case EVENT_SIM_START:

					// Sim has started so turn the input events on
					hr = SimConnect_SetInputGroupState(hSimConnect, INPUT_ZX, SIMCONNECT_STATE_ON);

					break;

                case EVENT_Z:
					if (!objectsCreated)
					{
						setUpSimObjects();
						objectsCreated = true;
					}
                    break;

				case EVENT_X:
					if (!plansSent && objectsCreated)
					{
						sendFlightPlans();
						plansSent = true;
					}
					break;

				case EVENT_C:
					{

						// Give the balloon some throttle
						if (bc.throttlePercent < 100.0f)
							bc.throttlePercent	+= 5.0f;

						hr = SimConnect_SetDataOnSimObject(hSimConnect, DEFINITION_THROTTLE, SIMCONNECT_OBJECT_ID_USER, 0, 0, sizeof(bc), &bc);

						break;
					}

				case EVENT_V:
					{

						// Give the balloon some throttle
						if (bc.throttlePercent > 0.0f)
							bc.throttlePercent	-= 5.0f;

						hr = SimConnect_SetDataOnSimObject(hSimConnect, DEFINITION_THROTTLE, SIMCONNECT_OBJECT_ID_USER, 0, 0, sizeof(bc), &bc);

						break;
					}
                
				default:
					printf("\nUnknown event: %d", evt->uEventID);
                    break;
            }
            break;
        }
		
		case SIMCONNECT_RECV_ID_ASSIGNED_OBJECT_ID:
		{
			SIMCONNECT_RECV_ASSIGNED_OBJECT_ID *pObjData = (SIMCONNECT_RECV_ASSIGNED_OBJECT_ID*)pData;
	
			switch( pObjData ->dwRequestID)
			{
			
			case REQUEST_Hellfire1:
				HellfireID = pObjData->dwObjectID;
				printf("\nCreated Mooney Bravo id = %d", MooneyID);
				break;	
			default:
				
				printf("\nUnknown creation %d", pObjData->dwRequestID);
				break;

			}
			break;
		}

		case SIMCONNECT_RECV_ID_QUIT:
        {
            quit = 1;
            break;
        }

        default:
            printf("\nReceived:%d",pData->dwID);
            break;
    }
}

void testSimObjects()
{
    HRESULT hr;

    if (SUCCEEDED(SimConnect_Open(&hSimConnect, "AI Objects and Waypoints", NULL, 0, 0, 0)))
    {
        printf("\nConnected to Flight Simulator!");   
          
		// Create some private events
        hr = SimConnect_MapClientEventToSimEvent(hSimConnect, EVENT_Insert);


        // Link the private events to keyboard keys, and ensure the input events are off
        hr = SimConnect_MapInputEventToClientEvent(hSimConnect, INPUT_ZX, "Insert", EVENT_Insert);

        hr = SimConnect_SetInputGroupState(hSimConnect, INPUT_ZX, SIMCONNECT_STATE_OFF);

        // Sign up for notifications
        hr = SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP_ZX, EVENT_Insert);

		// Set up a definition for a waypoint list
		hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_WAYPOINT,
			"AI Waypoint List", "number", SIMCONNECT_DATATYPE_WAYPOINT);

		// Set up a definition for the balloon throttle
		bc.throttlePercent = 0.0f;

        hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_THROTTLE,
			"GENERAL ENG THROTTLE LEVER POSITION:1", "percent");

		// Request a simulation start event
        hr = SimConnect_SubscribeToSystemEvent(hSimConnect, EVENT_SIM_START, "SimStart");

		while( 0 == quit )
        {
            SimConnect_CallDispatch(hSimConnect, MyDispatchProcSO, NULL);
            Sleep(1);
        } 

        hr = SimConnect_Close(hSimConnect);
    }
}


int __cdecl _tmain(int argc, _TCHAR* argv[])
{
    testSimObjects();
	return 0;
}
 
There can be thousands of reason why a code wont work.

I suggest you check the result returned by all simconnect functions since you store it in a HRESULT but never check if something went wrong. I also advise you to turn on the simconnect log and see if your calls are well received by the sim.
 
Back
Top