Closed chenjunnn closed 1 year ago
I believe that having the getter for the array only means you can't set the entire array, but you can set them by index. Have you tried P[0] = x?
IIRC the reason for dynamic memory fields to be read only is that assigning another array would require freeing up native memory while we garbage collect - this is not something that is going to work without some gimmicks and a performance overhead.
@adamdbrw Thanks for your help! I have tried setting the value by index last time but I forget to initialize camInfoMsg
itself 🤣
private sensor_msgs.msg.CameraInfo camInfoMsg;
...
camInfoMsg = new sensor_msgs.msg.CameraInfo(); // I forgot to initialize last time
camInfoMsg.K[0] = fx;
camInfoMsg.K[2] = cx;
camInfoMsg.K[4] = fy;
camInfoMsg.K[5] = cy;
camInfoMsg.K[8] = 1;
The above method is totally fine.
Glad this works for you! Different memory models of C# and C++ can be confusing and require some design choices when mixing :D
Describe the bug I want to publish camera info from Unity to ROS2, then I found the
K
variable insensor_msgs.msg.CameraInfo
is read-only and there is no constructor which can change the value ofK
. Also theP
andR
are read-only, too.The original type in raw sensor_msgs CameraInfo message definition is float64 array with specified length of 9, and all the arrays of specified length in csharp are read-only.
So is there a solution to change the value of
K
in csharp script?