simulationhigh-level-architecture

HLA Data Sharing Between Two Unity Apps Not Working


I’m working on a project where I have two Unity applications that need to share data using High-Level Architecture (HLA). I’m using PitchRTI as the Runtime Infrastructure (RTI) and I’ve created a .dll plugin in C++ for the HLA interactions. Given is the plugin repo (FOM included): HLA_UNITY_PLUGIN

In my setup, both the app has joined the same federation and Unity app 1 updates the positionX and positionY attributes of a BoxObject and publishes them to the federation. Unity app 2 is supposed to subscribe to these attributes and receive their updated values. However, while I can see that packets are being received in Unity app 2, the attribute values are always 0.

  1. Unity app 1 - It's simply consist of 2 Input fields that on send updates the published value
void UpdatePosition()
{
    try
    {
        box1.positionX = (float)double.Parse(positionX.text);
        box1.positionY = (float)double.Parse(positionY.text);

        Debug.Log($"{box1.id}, {box1.positionX}, {box1.positionY}");

        Connector.UpdateUnit(box1);
    }
    catch (Exception e)
    {
        Debug.Log("Update error occurred: " + e);
    }
}
  1. Unity app 2 - Subscribe to the published value and listen for its updates.
private void GetBoxData()
{
    int size = 0;
    IntPtr dataPtr = Connector.GetBoxes(ref size);

    if (size > 0)
    {
        BoxData[] boxDataFromHLA = new BoxData[size];

        int offset = 0;
        int dataBoxSize = Marshal.SizeOf(typeof(BoxData));

        for (int i = 0; i < size; i++, offset += dataBoxSize)
        {
            boxDataFromHLA[i] = Marshal.PtrToStructure<BoxData>(new IntPtr(dataPtr.ToInt64() + offset));
            Debug.Log(boxDataFromHLA[i].positionX);
            Debug.Log(boxDataFromHLA[i].positionY);
            cube.transform.position = new Vector3(boxDataFromHLA[i].positionX, boxDataFromHLA[i].positionY, 1);
        }
    }
}

I am also attaching the FederationConnector Script

using UnityEngine;
using System.Runtime.InteropServices;
using System;

// Define the same data structure that is being used in dll 
public struct BoxData
{
    public int id;
    public float positionX;
    public float positionY;
}

public class Connector : MonoBehaviour
{
    [DllImport("hla_plugin.dll")]
    public static extern void Connect(string federationName, string federateName, string fomFilePath);

    [DllImport("hla_plugin.dll")]
    public static extern void Disconnect();

    // Test Sim methods
    [DllImport("hla_plugin.dll")]
    public static extern void PublishUnit();

    [DllImport("hla_plugin.dll")]
    public static extern void SubscribeUnit();

    [DllImport("hla_plugin.dll")]
    public static extern int CreateUnit();

    [DllImport("hla_plugin.dll")]
    public static extern void UpdateUnit(BoxData unit1Data);

    [DllImport("hla_plugin.dll")]
    public static extern IntPtr GetBoxes(ref int size);
}

I’ve checked the following potential issues:

Despite all this, I’m still not seeing the expected attribute updates in Unity app 2. Please, help me on identifying what could be causing this issue or how to debug it further would be greatly appreciated.

PS: I am extremely new to C++, HLA and simulation. Any help will be much appreciated.


Solution

  • Okay, figured it out. The problem was not with my Unity scripts but with the plugin I created.

    The root of the issue was that while I was publishing and subscribing to my object class, the updates of attributes weren’t being fetched from the RTI. To solve this, I introduced a reflect event in my plugin. This event is responsible for fetching updates from the RTI and synchronizing the shared data across the two Unity applications.

    For those interested in the specifics, I’ve updated the code in my repository. You can find the updated code here. The changes related to the introduction of the reflect event can be found in this commit.