is “SetDataOnSimObject” supposed to work in the managed simconnect version?
I can “AICreateSimulatedObject” or “RequestDataOnSimObject”, but no way to set data on it,
btw I want to change the position of a simobject (either user airplane or a simconnect created one)
I have a lame VB.net implementation (removed all the create/query data part)
Dim msfs_simConnect As SimConnect
Enum DEFINITIONS
StructAircraftPosition
End Enum
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi, Pack:=1)>
Structure StructAircraftPosition
REM This is how you declare a fixed size string
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=256)>
Public title As String
Public latitude As Double
Public longitude As Double
Public altitude As Double
End Structure
msfs_simConnect = New SimConnect(" VB Managed Data Request", Me.Handle, WM_USER_SIMCONNECT, Nothing, 0)
msfs_simConnect.AddToDataDefinition(DEFINITIONS.StructAircraftPosition, "Plane Latitude", "degrees", Microsoft.FlightSimulator.SimConnect.SIMCONNECT_DATATYPE.FLOAT64, 0, 1)
msfs_simConnect.AddToDataDefinition(DEFINITIONS.StructAircraftPosition, "Plane Longitude", "degrees", Microsoft.FlightSimulator.SimConnect.SIMCONNECT_DATATYPE.FLOAT64, 0, 2)
msfs_simConnect.AddToDataDefinition(DEFINITIONS.StructAircraftPosition, "Plane Altitude", "feet", Microsoft.FlightSimulator.SimConnect.SIMCONNECT_DATATYPE.FLOAT64, 0, 3)
' Register the data definition
msfs_simConnect.RegisterDataDefineStruct(Of StructAircraftPosition)(DEFINITIONS.StructAircraftPosition)
Dim position As StructAircraftPosition
position.latitude = 29.877
position.longitude = 23.352
position.altitude = 300
msfs_simConnect.SetDataOnSimObject(DEFINITIONS.StructAircraftPosition, SimConnect.SIMCONNECT_OBJECT_ID_USER, SIMCONNECT_DATA_SET_FLAG.DEFAULT, position)
Quite strange that Simvarwatcher, when try to set the values, doesn’t work too
Instesd, the provided .cpp solutions works like a charm (even with my lame vb.net injected simobject!)
@mamuDesign
The problem is that you can’t SET the ‘title’ so you need to omit it from the structure definition.
If you want to SET the aircraft's position try defining a new position structure like this:
Public Structure MyPositionStructure
Dim Latitude As Double
Dim Longitude As Double
Dim Altitude As Double
'...any other settable parameters you want here
End Structure
Then
Enum DEFINITIONS
StructAircraftPosition
AircraftPosition
End Enum
Then, after you've defined your SimConnect (and made sure it's connected!). Note the last parameter has to reference the '0' based position of the parameter in the structure definition
msfs_simConnect.AddToDataDefinition(DEFINITIONS.AircraftPosition, "Plane Latitude", "degrees", Microsoft.FlightSimulator.SimConnect.SIMCONNECT_DATATYPE.FLOAT64, 0, 0)
msfs_simConnect.AddToDataDefinition(DEFINITIONS.AircraftPosition, "Plane Longitude", "degrees", Microsoft.FlightSimulator.SimConnect.SIMCONNECT_DATATYPE.FLOAT64, 0, 1)
msfs_simConnect.AddToDataDefinition(DEFINITIONS.AircraftPosition, "Plane Altitude", "feet", Microsoft.FlightSimulator.SimConnect.SIMCONNECT_DATATYPE.FLOAT64, 0, 2)
'.... add any other settable parameters you want here
'Note the last parameter of AddToDataDefinition has to reference the '0'-based position of the field in the structure definition
' Register the data definition
msfs_simConnect.RegisterDataDefineStruct(Of MyPositionStructure) (DEFINITIONS.AircraftPosition)
Dim position As MyPositionStructure
position.latitude = 29.877
position.longitude = 23.352
position.altitude = 300
msfs_simConnect.SetDataOnSimObject(DEFINITIONS.AircraftPosition, SimConnect.SIMCONNECT_OBJECT_ID_USER, SIMCONNECT_DATA_SET_FLAG.DEFAULT, position)
BTW note that, if you want to animate your simobject along a path, every rookie starts with the idea they’ll use simconnect to update the absolute position of the simobject as fast as possible i.e. per visual frame and that’ll give a smooth in-game movement. Actually that method results in high performance impact and stuttery movement but the rookie persists in tweaking the update rate to try to get it to work.
A much smoother & more efficient method in MSFS is to put the simobject into slew mode, turn the ai control off (iirc) and send once-per-second updates to the slew speed and direction. This means your code reading in (or calculating) the desired position at time T+1, and calculating the required slew speed and direction to get there in one second, rather than simply using some given absolute position value but it works.
But the rite-of-passage is to fail at the rapid-absolute-update method first and then move on to the slew method or decide the job’s a good-un and quit there.
Yes the slew method definitely works on non user objects. I’ve used it to concurrently smoothly animate 60 simobjects which were gliders from a real gliding competition with typically ~4 seconds intervals between the recorded position updates. For planes the real art is to estimate the bank accurately as that’s not in the time/lat/long/alt data but it ended up really convincing.
I think there’s no need to ‘estimate’ the bank angle (or pitch for that matter)
You could very easily add pitch and bank angle (or anything else you wanted) into the simple ‘AircraftPosition’ structure I suggested above and define the additional ‘AddToDataDefinition’ declarations.
There’s also no need to examine or set the data every visual frame or use some sort of external timer. To get a 4 second timeframe you can use 'SimConnect.SubscribeToSystemEvent(EventID, “4Sec”) to do what you want to do with the data in the SIMCONNECT_RECV_EVENT structure that will be returned every 4 seconds.
Thanks but my use case is loading standard FAI format flight log files (IGC) and then animating SimObjects (of gliders) along the paths of those time/lat/long/alt standard-format records. The log files come both from real aircraft (in gliding competitions mostly) and from sim aircraft. But the files are standard and essentially contain a long series of time/lat/long/alt records and a 4-second period is typical.
This ‘replay’ requirement is quite common, and in my case it can involve replaying up to 60 tracklogs at once, smoothly animating that many planes. The subtle point is you can most effectively animate the planes using a relatively slow, e.g. 1s, update rate and have MSFS slew the planes, rather than by far the most common assumption to do a per-frame position update.
Without the bank (or pitch) information in the tracklog an algorithm is needed to infer that from the position movement, but that can be surprisingly realistic. Won’t be accurate for uncoordinated turns though.
i can send SLEW_TOGGLE and SLEW_RESET without issue, so code should be correct, however no way to slew the airplane (not receiving any exception by simconnect btw)
e.g.
' working
msfs_simConnect.MapClientEventToSimEvent(EVENT_ID.SLEW, "SLEW_TOGGLE")
msfs_simConnect.TransmitClientEvent(SimConnect.SIMCONNECT_OBJECT_ID_USER, EVENT_ID.SLEW, 0, priority.groupPriority, SIMCONNECT_EVENT_FLAG.GROUPID_IS_PRIORITY)
' not working, no excpetion
msfs_simConnect.MapClientEventToSimEvent(EVENT_ID.SLEW_AHEAD, "AXIS_SLEW_AHEAD_SET")
msfs_simConnect.TransmitClientEvent(SimConnect.SIMCONNECT_OBJECT_ID_USER, EVENT_ID.SLEW_AHEAD, 2000, priority.groupPriority, SIMCONNECT_EVENT_FLAG.GROUPID_IS_PRIORITY)
(also don’t really understand the difference about SLEW_AHEAD_PLUS and AXIS_SLEW_AHEAD_SET, but none of them works…)