Unity-Technologies / ROS-TCP-Endpoint

ROS package used to create an endpoint to accept ROS messages sent from a Unity scene using the ROS TCP Connector scripts
Apache License 2.0
177 stars 118 forks source link

Need a "Subscribe" override that the callback function can know which topic the message is arrived from #156

Closed ftatp closed 1 year ago

ftatp commented 1 year ago

I have to subscribe more than 2 nodes at once with the same type of message.

_ros.Subscribe<PointCloud2>(topicName1, GeneratePointCloud);
_ros.Subscribe<PointCloud2>(topicName2, GeneratePointCloud);
_ros.Subscribe<PointCloud2>(topicName3, GeneratePointCloud);

GeneratePointCloud(PointCloud2 message)
{
    ~~~
}

Each generated pointCloud needs to know which topic it is from. But because there is no parameter in the CallbackFunction that describes the topic, I have to make different callback functions for each different topics doing the same thing.

So the best thing that I think is making an override of Subscribe that has the parameters like this: Subscribe(string topic, Callback<T, string topic>);

P.S. I want to contribute on the ROSConnection.dll to test it. Is it possible for me to look into the codes and modify it? I'll just fork it and give some pull requests

LaurieCheers-unity commented 1 year ago

One simple solution would be to pass different lambda functions to the different Subscribe calls:

_ros.Subscribe<PointCloud2>(topicName1, msg=>GeneratePointCloud(msg, topicName1));
_ros.Subscribe<PointCloud2>(topicName2, msg=>GeneratePointCloud(msg, topicName2));
_ros.Subscribe<PointCloud2>(topicName3, msg=>GeneratePointCloud(msg, topicName3));

GeneratePointCloud(PointCloud2 message, string topicName)
{
    ~~
}

And yes, we do accept submissions on this repo, though we probably wouldn't be interested in this specific change.