xpnteam / xpnet

Develop X-Plane plugins in .NET.
MIT License
50 stars 15 forks source link

Float Array issue #4

Closed sognatore99 closed 6 years ago

sognatore99 commented 6 years ago

I am trying to read some datarefs using GetFoatArray but when looking at the value there is an exception of type System.IndexOutOfRangeException. I tried both float array's that were writeable and only readable but i get the same error. The int array is working fine. The following two datarefs are what i was testing

/sim/joystick/joystick_axis_minimum
/sim/joystick/joystick_axis_values

In particular i need to get the second one as i think this is the raw data for joystick movement.

Thanks for any help.

jaurenq commented 6 years ago

Can you post what your code for working with those variables looks like? Thanks.

On Wed, Mar 7, 2018 at 10:38 AM sognatore99 notifications@github.com wrote:

I am trying to read some datarefs using GetFoatArray but when looking at the value there is an exception of type System.IndexOutOfRangeException. I tried both float array's that were writeable and only readable but i get the same error. The int array is working fine. The following two datarefs are what i was testing

/sim/joystick/joystick_axis_minimum /sim/joystick/joystick_axis_values

In particular i need to get the second one as i think this is the raw data for joystick movement.

Thanks for any help.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/jaurenq/XPNet/issues/4, or mute the thread https://github.com/notifications/unsubscribe-auth/ACsYeyFkqxwlX0SevElw3m5MpxJE3FmNks5tcAvQgaJpZM4Sgws0 .

sognatore99 commented 6 years ago

IXPDataRef<float[]> xplaneSimJoystickAxisValues = m_api.Data.GetFloatArray("sim/joystick/joystick_axis_values")

Then all I do is use the Log function and it gives me that error. If I look at in in real time during debugging the value field shows the exception.

I was only logging to see if i can tell if the joystick raw data is available. I need to detect if the user released the joystick so that i can turn on the co-pilot joystick so they can take control.

Thanks again for helping.

jaurenq commented 6 years ago

Ok, I think I see what's wrong. I'll have to wait until after work today to take a stab at fixing it. It's not handling correctly the case where the sim says that it has zero data values to provide for an array dataref.

sognatore99 commented 6 years ago

That would be odd as i thought one of these two datarefs would provide me the joystick raw data, so i would think that there should be data? I did however notice in your c++ code that the value of zero was being passed back to create the array.

sognatore99 commented 6 years ago

So I installed the DataRef Editor and found that there should be values in the Float array so the size passed back should not be zero.

jaurenq commented 6 years ago

So, the size of that array is definitely zero when you call the wrong function to get the size of the array... 😔 In XPDataRefFloatArray in DataRefs.cs, I was calling XPLMGetDatavi to get nValues instead of XPLMGetDatavf. XPDataRefByteArray had the same problem. I also fixed the code not to crash if any particular DataRef ever did claim to return zero values. I need to do some cleanup and testing with it, and get test cases for FloatArray and ByteArray into the test harness, before I check it in. In the meantime, if you want you can fix it locally and build a new XPNet.CLR.dll. Here's what my XPDataRefFloatArray.Value looks like now.

        public unsafe override float[] Value
        {
            get
            {
                int nValues = PluginBridge.ApiFunctions.XPLMGetDatavf(DataRef, null, 0, 0);

                EnsureBuffer(nValues);

                if (nValues > 0)
                {
                    fixed (float* p = &m_buffer[0])
                        PluginBridge.ApiFunctions.XPLMGetDatavf(DataRef, p, 0, nValues);
                }

                return m_buffer;
            }

            set
            {
                if (value.Length > 0)
                {
                    fixed (float* p = &value[0])
                        PluginBridge.ApiFunctions.XPLMSetDatavf(DataRef, p, 0, value.Length);
                }
                else
                {
                    PluginBridge.ApiFunctions.XPLMSetDatavf(DataRef, null, 0, 0);
                }
            }
        }

I haven't tested the updated 'set' yet, but with that updated 'get' it works for me now in X-Plane. I registered the Logger DLL and added the following into my xpnetcfg.json and was able to see the values of joystick_axis_values show up in the log file and change as I moved the joysticks around.

    "FloatArrayData": [
      "sim/joystick/joystick_axis_values"
    ]
jaurenq commented 6 years ago

I committed those changes. Give it a try when you can and let me know if it works for you now. Thanks!

sognatore99 commented 6 years ago

Excellent. All is working (well except my code :) but that I can now solve with your update. Thank you.