WiiMoteLib.WiiMoteState Problem

Dec 11, 2009 at 9:45 PM
Edited Dec 11, 2009 at 9:48 PM

Below is some code that I am having a problem with ... If anyone has ANY idea what is going on here, I would greatly appreciate the input ... I will explain what I do with the wiiMote while the prog is running, and the debug output ...

Ok, I start the prog and click Button7 ... After a second or two, the battery level is printed in the debug window ... All is good.

Next, I press (and hold) A on the wiiMote ... 'True:False' is printed ... All is good.

Next, I let go of A on the wiiMote ... 'False:False' is printed ... THIS IS WRONG - PrevState.ButtonStatus.A (the second item printed) SHOULD BE TRUE

Next, I press (and hold) A on the wiiMote ... 'True:True' is printed ... THIS IS WRONG - PrevState.ButtonStatus.A (the second item printed) SHOULD BE FALSE

If I change 'PrevStat = CurrStat' to 'PrevStat.ButtonState = CurrStat.ButtonState' it works correctly.  This indicates to me that the statement 'PrevStat = CurrStat' is setting the reference not the value (not sure if this even makes sense, let alone how to fix it).

 

Dim CurrStat As WiimoteLib.WiimoteState
Dim PrevStat As WiimoteLib.WiimoteState

Sub Button7Click(sender As Object, e As EventArgs)
    Dim wiiMote As New WiimoteLib.Wiimote
    Dim wiiMoteCol As New WiimoteLib.WiimoteCollection
    wiiMoteCol.FindAllWiimotes()
    wiiMote = wiiMoteCol.Item(0)
    wiiMote.Connect
    debug.Print(wiiMote.WiimoteState.Battery.ToString)
    PrevStat = wiiMote.WiimoteState
    AddHandler wiiMote.WiimoteChanged, AddressOf EventHandler
End Sub

Sub EventHandler(sender as Object, wiiMoteChangedEventArgs as WiimoteLib.WiimoteChangedEventArgs)
    CurrStat = wiiMoteChangedEventArgs.WiimoteState
    debug.Print(CurrStat.ButtonState.A & ":" & PrevStat.ButtonState.A)
    PrevStat = CurrStat
End Sub

 

Thanks.

DJ

Dec 13, 2010 at 2:39 PM

WiiMoteState is a class, not a struct.

By calling PrevStat = wiiMote.WiimoteState, you are effectively setting the reference address for PrevStat to the same as CurrStat. You are not copying the actual state's values.

What I've done is use a struct and parsed data into that struct (if you are familiar with XNA at all, that is how it is performed).