How to use ByteConverter effectively

NFOBoy

Active Member
Licensed User
Longtime User
I am wrapping the Game Service library from Google, and I now have b4A apps sending and receiving their real time messages. I am trying to duplicate two of the example that Google has (Trivial Quest and Button Click)

the messages passed back and forth are Byte(), which I have never really dealt with.

In the button click sample from Google they use this:
B4X:
 // Broadcast my score to everybody else.
    void broadcastScore(boolean finalScore) {
        if (!mMultiplayer)
            return; // playing single-player mode

        // First byte in message indicates whether it's a final score or not
        mMsgBuf[0] = (byte) (finalScore ? 'F' : 'U');

        // Second byte is the score.
        mMsgBuf[1] = (byte) mScore;

        // Send to every other participant.
        for (Participant p : mParticipants) {
            if (p.getParticipantId().equals(mMyId))
                continue;
            if (p.getStatus() != Participant.STATUS_JOINED)
                continue;
            if (finalScore) {
                // final score notification must be sent via reliable message
                getGamesClient().sendReliableRealTimeMessage(null, mMsgBuf, mRoomId,
                        p.getParticipantId());
            } else {
                // it's an interim score notification, so we can use unreliable
                getGamesClient().sendUnreliableRealTimeMessage(mMsgBuf, mRoomId,
                        p.getParticipantId());
            }
        }
    }

So I have this in my wrapper:

B4X:
 public void sendReliableRealTimeMessage(RoomWrapper room, GameClientWrapper GCW, String recipientParticipantId, byte[] messageData){
          GCW.me.sendReliableRealTimeMessage(this, messageData, room.getRoom().getRoomId(), recipientParticipantId);
    }

which is working very nicely... but I don't know how to effectively do this part in B4A speak:

B4X:
 // First byte in message indicates whether it's a final score or not
        mMsgBuf[0] = (byte) (finalScore ? 'F' : 'U');

        // Second byte is the score.
        mMsgBuf[1] = (byte) mScore;

I like how they can force a char into a byte config for the first element, and the integer into the second element, but I can't seem to figure that out elegantly.... so I am doing this:

B4X:
Sub tmrCountdown_Tick
   If remainingCountdown = 0 Then
      tmrCountdown.Enabled = False
      Return
   End If

   mMsgBuf(0) = 99 'message that it's a countdown event
   mMsgBuf(1) = remainingCountdown
   
   
   
   For Each par As String In listParticipants
      If par = myID Then
         showCountdown(remainingCountdown)
      Else
         roomConfig.sendReliableRealTimeMessage(Room, GGS.GamesClient, par, mMsgBuf)
      End If
   Next
   remainingCountdown = remainingCountdown - 1
   
End Sub
and
B4X:
Sub roomConfig_OnRealTimeMessageRecieved (isReliable As Boolean, buffer() As Byte, participantID As String)
   
   Select buffer(0)
      Case 99 
         showCountdown(buffer(1))
   End Select
   

   
End Sub

and then I'll add other lead elements to detect if it's a score update... etc...

My question is, is there a way to use the char method elegantly as shown in the Java code?

Also, I take it I need to write some more extensive functions to convert things to buffer() if I want to handle larger scores, String messages etc... so does anybody know of some good samples?
(my brain is fried for the night... so I'll dream about this tonight for sure)
 
Top