Skip to content

Add struct-based SimVar set functionality#11

Merged
AussieScorcher merged 1 commit intostopbars:mainfrom
bstudtma:set-simvar-struct
Oct 20, 2025
Merged

Add struct-based SimVar set functionality#11
AussieScorcher merged 1 commit intostopbars:mainfrom
bstudtma:set-simvar-struct

Conversation

@bstudtma
Copy link
Contributor

Summary

Introduce a new API to set multiple SimVars in one call using a strongly-typed struct:

  • SimVarManager.SetAsync<T>(T values, CancellationToken ct = default)

Usage Example

// Get current position (includes altitude)
var original = await client.SimVars.GetAsync<Position>(cancellationToken);

// Log current altitude
Console.WriteLine($"Original Altitude: {original.Altitude:F2} ft");

// Create a new target position
var target = original;
target.Altitude = original.Altitude + 15.0;

// Set new altitude (and any other changed fields) via struct
await client.SimVars.SetAsync(target, cancellationToken);

Where Position is:

public struct Position
{
    /// <summary>Latitude in degrees.</summary>
    [SimConnect("PLANE LATITUDE", "degrees")]
    public double Latitude;

    /// <summary>Longitude in degrees.</summary>
    [SimConnect("PLANE LONGITUDE", "degrees")]
    public double Longitude;

    /// <summary>Altitude in feet (MSL).</summary>
    [SimConnect("PLANE ALTITUDE", "feet")]
    public double Altitude;
}

Tests

  • Add unit tests to verify:
    • Mapping from annotated struct fields to SimVar definitions.
    • Successful multi-SimVar set with SetAsync<T>.
    • Round-trip restore of the original values after the test completes.
  • Update existing tests to cover mixed types and no-op updates (unchanged fields).

Performance & Reliability

  • Reduces multiple round-trips to a single call for grouped updates.
  • Minimizes inconsistent intermediate states across related SimVars.

Author Information

Discord Username: bstudtma


Checklist:

  • Have you followed the guidelines in our Contributing document?
  • Have you checked to ensure there aren't other open Pull Requests for the same update/change?

Introduces IFieldWriter, SimVarFieldWriter, and SimVarFieldWriterFactory to enable writing SimVar-annotated structs into unmanaged buffers. Adds SimVarManager.SetAsync<T> and SetStructAsync for setting multiple SimVars in one call using a struct. Updates tests to verify struct-based SimVar setting and restoration.
Copilot AI review requested due to automatic review settings October 20, 2025 01:15
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Adds a struct-based API to set multiple SimVars in a single call, mirroring the existing struct-based GetAsync path.

  • Introduces SimVarManager.SetAsync(T value, ...) to write annotated struct fields in one request
  • Implements internal writer pipeline (IFieldWriter, SimVarFieldWriter, SimVarFieldWriterFactory) to pack struct data into the unmanaged buffer using the same field layout as the get path
  • Adds a test validating altitude change via struct set and restore

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
src/SimConnect.NET/SimVar/SimVarManager.cs Adds public SetAsync and internal SetStructAsync to build and send packed struct data to SimConnect.
src/SimConnect.NET/SimVar/Internal/SimVarFieldWriterFactory.cs New factory to reflect annotated struct fields, infer types, and build writers and layout sizes.
src/SimConnect.NET/SimVar/Internal/SimVarFieldWriter.cs Writer that marshals each field into the unmanaged buffer based on SimConnectDataType.
src/SimConnect.NET/SimVar/Internal/IFieldWriter.cs Interface for writing a single field into a packed buffer.
tests/SimConnect.NET.Tests.Net8/Tests/SimVarTests.cs Adds a test to validate struct-based SetAsync for Position (altitude + restore).

@stopbars stopbars deleted a comment from Copilot AI Oct 20, 2025
@stopbars stopbars deleted a comment from Copilot AI Oct 20, 2025
@stopbars stopbars deleted a comment from Copilot AI Oct 20, 2025
@stopbars stopbars deleted a comment from Copilot AI Oct 20, 2025
@stopbars stopbars deleted a comment from Copilot AI Oct 20, 2025
@AussieScorcher
Copy link
Member

Amazing work! Will merge and be pushing a new SimConenct.NET version shortly :)

@AussieScorcher AussieScorcher merged commit 9fd2f53 into stopbars:main Oct 20, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments