SetDataOnSimObject notdoing anything for user's latitude, longitude, altitude or airspeed.

Discuss on the SimConnect SDK can be used by programmers to write add-on components for Prepar3D
Post Reply
rbearbeats
Posts: 1
Joined: Tue Oct 19, 2021 8:25 pm

SetDataOnSimObject notdoing anything for user's latitude, longitude, altitude or airspeed.

Post by rbearbeats »

I'm using the c++ library and attempting to from an external program, move the user's plane to a specific latitude, longitude and change the altitude and airspeed. Unfortunately I've run into an issue where this doesn't seem to do anything while the game is running whether or not the simulation is paused.

Below is a small example of the code, is there something I am missing or have gotten wrong? I have gotten it working when the code is run before the game is launched in the startup menu, but once in game the `SimConnect_SetDataOnSimObject` seem to be ignored or immediately overwritten.

My goal with this is to have an external program change these values and then the simulation can keep running without having to restart or open a new scenario. Stepping through the code shows that all `AddToDataDefinition`s and `SetDataOnSimObject`s succeed but I'm not sure why the changes wouldn't then be reflected in game.

If anyone could point me in the right direction I would be greatly appreciate it!

Code: Select all

#include <Windows.h>
#include <tchar.h>
#include <stdio.h>
#include "SimConnect.h"
#include <strsafe.h>
#include <fstream>

static enum DATA_DEFINE_ID {
	ALTITUDE,
	LATITUDE,
	LONGITUDE,
	AIRSPEED
};

HANDLE hSimConnect = NULL;

int main() {

	HRESULT hr;

	hr = SimConnect_Open(&hSimConnect, "SimConnect Unity Plugin", NULL, 0, 0, 0);

	hr = SimConnect_AddToDataDefinition(hSimConnect, ALTITUDE, "PLANE ALTITUDE", "feet", SIMCONNECT_DATATYPE_FLOAT32, 0, SIMCONNECT_UNUSED);
	hr = SimConnect_AddToDataDefinition(hSimConnect, LATITUDE, "PLANE LATITUDE", "degrees", SIMCONNECT_DATATYPE_FLOAT32, 0, SIMCONNECT_UNUSED);
	hr = SimConnect_AddToDataDefinition(hSimConnect, LONGITUDE, "PLANE LONGITUDE", "degrees", SIMCONNECT_DATATYPE_FLOAT32, 0, SIMCONNECT_UNUSED);
	hr = SimConnect_AddToDataDefinition(hSimConnect, AIRSPEED, "AIRSPEED TRUE", "knots", SIMCONNECT_DATATYPE_INT32, 0, SIMCONNECT_UNUSED);

	double altitude = 25000;
	double latitude = 30.352651;
	double longitude = -87.320735; 
	int airspeed = 400;

	hr = SimConnect_SetDataOnSimObject(hSimConnect, ALTITUDE, SIMCONNECT_OBJECT_ID_USER, 0, 0, sizeof(altitude), &altitude);
	hr = SimConnect_SetDataOnSimObject(hSimConnect, LATITUDE, SIMCONNECT_OBJECT_ID_USER, 0, 0, sizeof(latitude), &latitude);
	hr = SimConnect_SetDataOnSimObject(hSimConnect, LONGITUDE, SIMCONNECT_OBJECT_ID_USER, 0, 0, sizeof(longitude), &longitude);
	hr = SimConnect_SetDataOnSimObject(hSimConnect, AIRSPEED, SIMCONNECT_OBJECT_ID_USER, 0, 0, sizeof(airspeed), &airspeed);

}
Clifton Crane
Lockheed Martin
Posts: 1207
Joined: Tue Sep 25, 2012 2:34 pm

Re: SetDataOnSimObject notdoing anything for user's latitude, longitude, altitude or airspeed.

Post by Clifton Crane »

Hi rbearbeats,

It looks like you have a size mismatch between types. You should be passing SIMCONNECT_DATATYPE_FLOAT64 in your calls to SimConnect_AddToDataDefinition since your Lat/Lon/Alt is using double's in the SimConnect_SetDataOnSimObject calls. These sizes must match so Prepar3D knows how to correctly align the returned data.

You may want to have a look at the SetData sample. This sample shows how to use structs instead of primitives, which would reduce the number of SimConnect_SetDataOnSimObject calls needed.

You may also want to have a look at the DataHarvester SimConnect sample (note: there are actually two samples, one is PDK based and the other SimConnect based). This sample continuously polls the user object for data, so its a bit of the reverse of what you are trying to accomplish. In your sample, your calls to SimConnect_SetDataOnSimObject only occur once in the main() function. If you planned to continuously set data on the user object, you would need to keep your SimConnect_CallDispatch calls in a while loop or rely on a SimConnect based event in your dispatch function.

Regards,
Clifton
Clifton Crane
Prepar3D® Software Engineer Sr.
Post Reply