*固定*使用cpp(msfs2020 SimConnect)不断更新控制台



示例图片

我试图不断更新与海拔高度相关的控制台号码。目前,这是一个静态数字,在飞机上升或下降高度时不会更新。在文本底部附近有一条评论,引用了我用来将其发送到控制台的prinf(((不确定需要看到什么,所以我发送了所有内容(。

#include <iostream>
#include <Windows.h>
#include "SimConnect.h"
#include <string>
#include <sstream>
using namespace std;


HANDLE hSimConnect = NULL;


enum DATA_DEFINE_ID
{
DEFINITION_ID_AP,
};
enum DATA_REQUEST_ID
{
REQUEST_AP_SETTINGS,
};
enum EVENT_ID
{
EVENT_SET_AP_ALTITUDE,
};

struct DataRefs
{
double altitude;
double knots;
};

int main() {

HRESULT hr;
SIMCONNECT_RECV* pData = NULL;
DWORD cbData = 0;
bool bRequestProcessed = false;
int SelectedAltitude = 0;
SIMCONNECT_RECV_SIMOBJECT_DATA* pObjData = NULL;
DataRefs* pDataRefs = NULL;

if (SUCCEEDED(SimConnect_Open(&hSimConnect, "Client Event", NULL, NULL, NULL, NULL))) {
printf("Connected to MSFS2020!n");
}
else {

/* string str = "42";
int num2 = stoi(str);
cout << num2;
*/
printf("Failed to Connect to MSFS2020n");
}
//simVars
hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ID_AP, "PLANE ALTITUDE", "Feet");
//hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_ID_AP, "AIRSPEED TRUE", "Knots");
// Check simVars
hr = SimConnect_RequestDataOnSimObject(hSimConnect, REQUEST_AP_SETTINGS, DEFINITION_ID_AP, SIMCONNECT_OBJECT_ID_USER, SIMCONNECT_PERIOD_ONCE);
if (FAILED(hr))
{
printf("RequestDataOnSimObject for AutopilotData structure - errorn");
}
bRequestProcessed = false;
while (!bRequestProcessed)
{
hr = SimConnect_GetNextDispatch(hSimConnect, &pData, &cbData);
if (SUCCEEDED(hr))
{
pObjData = (SIMCONNECT_RECV_SIMOBJECT_DATA*)pData;
pDataRefs = (DataRefs*)&pObjData->dwData;
/* int altint;

altint = stoi (pDataRefs->altitude);

string str = "42";
int num2 = stoi(str);
cout << num2;
*/

/*
printf("rCurrent plane altitude: %.f feet", pDataRefs->altitude);
fflush(stdout);
*/
//This line of code is what im referring to
printf("rCurrent  altitude: %.f feet", pDataRefs->altitude);
//printf("rCurrent speed: %.f knots", pDataRefs->knots);
}
}
// Close
hr = SimConnect_Close(hSimConnect);
return 0;
}

发现问题。如果您使用CTRL-F";hr=SimConnect_RequestDataOnSimObject(hSimConnect、REQUEST_AP_SETTINGS、DEFINITION _ID_AP、SimConnect_OBJECT_ID_USER、SimConnect _PERIOD_ONCE(&";,您可以看到我使用SIMCONNECT_PERIOD_ONCE。查看文档(https://www.prepar3d.com/SDKv4/sdk/simconnect_api/references/structures_and_enumerations.html),我将SIMCONNECT_PERIOD_ONCE替换为SIMCONNECT_PERIOD_SECOND以每秒更新一次。

最新更新