Search Unity

[SyncVar(hook = "OnChange"] Error: Hookvar must have one argument. Not sure what to do.

Discussion in 'Multiplayer' started by Thimble2600, Jul 9, 2018.

  1. Thimble2600

    Thimble2600

    Joined:
    Nov 27, 2015
    Posts:
    165
    I've since figured out the issue, still I'm awful at Unity Networking and Networking in general so feel free to tell me I'm doing something wrong.

    What it is I'm making (not really important):
    I'm making a chat system. The system is designed to only show one message at a time for a short duration. Kind of a throwback to singleplayer games where you press a button for the next line of dialog, only it's running on a timer. Pretty impractical for lots of people talking at once but I'll make it a local radius thing sometime later.

    What I need help with: Basically when a player presses return in an input field it changes the message shown by the chat log. I also want it to prompt the message window by changing the
    RectTransform
    for all clients chat log, so I felt using a
    SyncVar Hook
    , unless it only does it on client-side? (I am quite awful at understanding how networking works). Anyway, when I declare
    [SyncVar(Hook="OnChangeMessage")]
    Unity throws a few errors, most notably "SyncVar Hook function OnChangeMessage must have one argument ServerChat".



    Declaring
    Code (CSharp):
    1. public class ServerChat : NetworkBehaviour
    2. {
    3.     [SyncVar]
    4.     public string m_sender = "Sender";
    5.  
    6.     [SyncVar(hook = "OnChangeMessage")]
    7.     public string m_message = "Message";
    8.     ....
    9. }
    10.  
    Using
    Code (CSharp):
    1.  
    2.     public void ChangeMessage()
    3.     {
    4.         if (!isServer)
    5.             return;
    6.      
    7.         if(m_inputField.text.Contains("\n"))
    8.         {
    9.             m_message = m_inputField.text;
    10.             m_inputField.ActivateInputField();
    11.             m_inputField.text = string.Empty;
    12.         }
    13.     }
    Hook Method
    Code (CSharp):
    1.  private void OnChangeMessage()
    2. {
    3.         rTransform.position = startPosition;
    4.         timer = interval; //reset the timer so the message window can scroll again
    5.         m_message_message.text = m_message;
    6. }
     
    Last edited: Jul 9, 2018
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,574
    You need to declare any SyncVar hook method to take the same input as the SyncVar, so it can be passed to the hook method when called. m_message is of type string, so OnChangeMessage needs to take a string.

    Code (csharp):
    1.  
    2. private void OnChangeMessage(string new_m_message)
    3.  
    Normally you'd also want to assign m_message the value of whatever is passed to the hook method, because the syncvar is not actually updated on the clients automatically when using a hook like it is when not using a hook.
     
unityunity