Search Unity

Resolved Participant Events Examples Issues

Discussion in 'Vivox (Voice & Text Chat)' started by MiTschMR, Sep 28, 2022.

Thread Status:
Not open for further replies.
  1. MiTschMR

    MiTschMR

    Joined:
    Aug 28, 2018
    Posts:
    489
    When looking at the Participant Events Examples I start asking myself what the developers of these examples tried to do and whether they even tested the code. I mean, look at this switch statement of the IChannelSession.Participants.AfterValueUpdated example:
    Code (CSharp):
    1. switch (property)
    2.  
    3.     {
    4.         case "LocalMute":
    5.         {
    6.             if (username != accountId.Name) //can't local mute yourself, so don't check for it
    7.             {
    8.                 //update their muted image
    9.             }
    10.             break;
    11.         }
    12.         case "LocalMute":
    13.         {
    14.             //update speaking indicator image
    15.             break;
    16.         }
    17.         default:
    18.         break;
    19.     }
    Two times checking "LocalMute" which in my IDE (Visual Studio 2022) gives this error (and should logically thinking also not work):

    Error CS0152: The switch statement contains multiple cases with the label value '"LocalMute"'

    It baffles me how we are supposed to figure out the other value...

    In the BeforeKeyRemoved example I wonder about what if it is not me who gets removed from the channel. Should something be done here?

    Code (CSharp):
    1. if (participant.IsSelf)
    2.     {
    3.         BindChannelSessionHandlers(false, channelSession); //Unsubscribe from events here
    4.         currentChannelID = null;
    5.  
    6.         var user = client.GetLoginSession(accountId);
    7.         user.DeleteChannelSession(channelSession.Channel);
    8.     }
    It may be that I have no idea how it works, but it looks kinda spooky and makes me afraid of using the SDK at all due to not being able to understand what I should use and especially how.
     
  2. MurphyMurph_21

    MurphyMurph_21

    Joined:
    Jul 3, 2020
    Posts:
    73

    LOL!!! I don't think it used to look like that but anyways. Here is what your looking for I think
    ..... "SpeechDetected" & not "LocalMute" twice

    Code (CSharp):
    1.         private void OnUserValuesUpdated(object sender, ValueEventArg<string, IParticipant> valueArg)
    2.         {
    3.             var source = (VivoxUnity.IReadOnlyDictionary<string, IParticipant>)sender;
    4.  
    5.             var senderIParticipant = source[valueArg.Key];
    6.             MyCustomEvent.OnUserValuesUpdated(senderIParticipant);
    7.  
    8.             switch (valueArg.PropertyName)
    9.             {
    10.                 case "LocalMute":
    11.  
    12.                     if (!senderIParticipant.IsSelf) //can't local mute yourself, so don't check for it
    13.                     {
    14.                         if (senderIParticipant.LocalMute)
    15.                         {
    16.                             // Fires way too much in my opinion - like every second
    17.                             // fire your event / code here to change UI, etc..
    18.                         }
    19.                         else
    20.                         {
    21.                             // Fires way too much in my opinion - like every second
    22.                             // fire your event / code here to change UI, etc..
    23.                         }
    24.                     }
    25.                     break;
    26.  
    27.                 case "SpeechDetected":
    28.                     {
    29.                         if (senderIParticipant.SpeechDetected)
    30.                         {
    31.                             // fire your event / code here to change UI, etc..
    32.                         }
    33.                         else
    34.                         {
    35.                            // fire your event / code here to change UI, etc..
    36.                         }
    37.                         break;
    38.                     }
    39.                 default:
    40.                     break;
    41.             }

    As for what to do when a user leaves channel I would fire an event so you can do multiple things when this happens. For example have one method that removes the user from the UI, maybe another that notifies the user or the group that someone left, etc...

    My version below....

    Code (CSharp):
    1.      
    2. public class VivoxEvents()
    3. {
    4.      private void OnUserLeftChannel(object sender, KeyEventArg<string> keyArg)
    5.     {
    6.        var source = (VivoxUnity.IReadOnlyDictionary<string, IParticipant>)sender;
    7.        var senderIParticipant = source[keyArg.Key];
    8.        MyCustomEvents.OnUserLeftChannel(senderIParticipant);
    9.     }
    10. }
    11.  
    12. public class MyCustomEvents
    13. {
    14.         public static Action<IParticipant> UserLeftChannel;
    15.  
    16.         public static void OnUserLeftChannel(IParticipant participant)
    17.         {
    18.             UserLeftChannel?.Invoke(participant);
    19.         }
    20.  
    21. }
    22.  
    23. public class MyUI : MonoBehaviour
    24. {
    25.     [SerializeField] Text newMessage;
    26.  
    27.     void Start()
    28.     {
    29.           MyCustomEvents.UserLeftChannel += UpdateUIWhenPlayerLeaves;
    30.     }
    31.     void OnApplicationQuit()
    32.     {
    33.           MyCustomEvents.UserLeftChannel -= UpdateUIWhenPlayerLeaves;
    34.     }
    35.  
    36.     void UpdateUIWhenPlayerLeaves(IParticipant participant)
    37.     {
    38.          // print message to the Text field that user has left
    39.         newMessage.text += $"\n {participant.Account.DisplayName} has left the channel";
    40.         if (!participant.IsSelf)
    41.         {
    42.             // remove UI code here
    43.         }
    44.     }
    45. }
     
    Last edited: Oct 8, 2022
    emilyryan and MiTschMR like this.
  3. MiTschMR

    MiTschMR

    Joined:
    Aug 28, 2018
    Posts:
    489
    Thank you very much!
     
    MurphyMurph_21 likes this.
  4. emilyryan

    emilyryan

    Unity Technologies

    Joined:
    Nov 22, 2019
    Posts:
    129
    Hello @MiTschMR I apologize for the mistake in our documentation! I'll submit a ticket right away to get that fixed!

    @MurphyMurph_21 Thanks so much for posting the awesome code snippets above!
     
    MurphyMurph_21 and MiTschMR like this.
  5. emilyryan

    emilyryan

    Unity Technologies

    Joined:
    Nov 22, 2019
    Posts:
    129
    Hello!

    This typo has officially been fixed! Thank you both again!
     
    MurphyMurph_21 likes this.
Thread Status:
Not open for further replies.