Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Setting networkview to observe a script..

Discussion in 'Multiplayer' started by Udyrfrykte, Oct 9, 2010.

  1. Udyrfrykte

    Udyrfrykte

    Joined:
    Feb 14, 2010
    Posts:
    156
    ....... It is apparently not as simple as dragging it over, because I've tried that, trust me.

    How do I proceed about doing this? Using Unity3
     
  2. cerebrate

    cerebrate

    Joined:
    Jan 8, 2010
    Posts:
    261
    If you want to send state changes over the network, you can't just drag the script in to the observable slot. You have to send all the information you want to send by coding it in, using RPC calls, or serializing it with the serializing functions. You don't even have to put anything in the networkview in order to use it:



    Both of those scripts communicate over the network with the networkview using RPC calls through it.

    http://unity3d.com/support/documentation/ScriptReference/NetworkView.RPC.html


    It frankly sounds like you haven't actually gone through any of the documentation for networking. I would highly recommend you go through this tutorial:

    http://forum.unity3d.com/threads/29015-UniKnowledge-entry-Unity-Networking-the-Zero-to-Hero-guide

    Speaking of which, if an admin sees this, can we get that thread stickied, or put in the multiplayer networking faq that is in the announcements part.
     
  3. AkilaeTribe

    AkilaeTribe

    Joined:
    Jul 4, 2010
    Posts:
    1,149
    If my memory is good :

    - We have two game object, A and B, one for an user, the second for another user.
    - Each have a Network View, and a common Network View ID.
    - Making an RPC call with the game object A will trigger the first function encountered in any script of the game object B, with the same name (so, networkView.RPC ("TestFunction", RPCMode.All) would trigger the first function called TestFunction it meets, whatever the script is).
    - Avoiding functions with same name in different scripts, in the same game object, is better then, because only one (and a random one) would be called.

    In order to state synchronize a script (and it's variables), the Observed element must be this script, and OnSerializeNetworkView function is required.
     
  4. Udyrfrykte

    Udyrfrykte

    Joined:
    Feb 14, 2010
    Posts:
    156
    I have gone through that tutorial and read up on other documentation, maybe I'm just thick, that's definitely an option though.
    I've been using RPC calls but realized I don't completely understand them, because something goes... weird (see my other thread).

    So I thought I'd switch back to OnSerializeNetworkView. To quote M2H networking tutorial.

    "What if we did want to move the cube in the y-direction, or we wanted more contro over what's
    being synchronized by unity. Open and run “Tutorial 2/Tutorial 2A2”. The game should play
    exactly the same, but the code running in the background has been changed. The networkview
    attached to the PlayerCube is now observing the “Tutorial 2A2.js” script. This specifically means
    that the network view is now looking for a “OnSerializeNetworkView” function inside that script."

    I do have a OnSerializeNetworkView function, but I can't "drag" the script into being observed. But I see this is done in the tutorial files (a script is being observed by the networkView).

    I will admit that I've kinda rushed through the tutorial since I am kinda stressed (under the gun), but the tutorial doesn't explain how to do it. It just magically is observing the script. My logic for this was that it was as simple and dragging it onto the networkview.

    To sum things up: I do understand the concept of networking, Unity uses RPCs or OnSerializeNetworkView. I want to use OnSerializeNetworkView. From my understanding this works by observing a script in the networkview. I don't know how to observe a script.

    I'm not so arrogant that I think I'd master networking just by watching snippets of code. I have read a lot and tried a lot, but I can't get it working correctly.

    @AkilaeTribe
    Thanks for that. It wasn't quite what I was looking for, but it helps me understand RPCs more.
    I wish to state synchronize a script, as you mentioned. My problem however is that I don't know how to place the observed element as the script.
     
  5. AkilaeTribe

    AkilaeTribe

    Joined:
    Jul 4, 2010
    Posts:
    1,149
    The observed script would better be on the same game object as the network view. Is it the case ?
     
  6. Udyrfrykte

    Udyrfrykte

    Joined:
    Feb 14, 2010
    Posts:
    156
    Yes, it is.

    edit:
    ... wait, would it be better that the same script that is being observed is also on the gameobject? (If so, yes, that's what I'm trying)

    Or do you mean that the script should be on the gameobject, rather than being observed?
     
    Last edited: Oct 9, 2010
  7. TwisterK

    TwisterK

    Joined:
    Oct 26, 2010
    Posts:
    110
    I had the same problem as well. Just drag the title of the script instead, it works =)
     
  8. elco

    elco

    Joined:
    Nov 4, 2010
    Posts:
    1
    drag the name of the script component into the observed field. for example if your scirpt is called playerscript, drag the title of the component (also probably called playerscript) to the observed field on the network component.
     
  9. Moredice_legacy

    Moredice_legacy

    Joined:
    Jan 31, 2010
    Posts:
    101
    It would work best if your object to be observed is a prefab object. And if you want it observed to synchronize movement, you need the transform of the object to be observed; ergo you need to drag-and-drop it onto the observed field from your project list.

    Example:
     
  10. Duncan

    Duncan

    Joined:
    Jan 8, 2011
    Posts:
    30
    I second stickying this, also I think its worth pointing out that the documentation barely covers networking scripts, it says that you can observe a script and not much more. From talking on irc and reading everything I can it seems that observing a script does very little though. As I understand it if I put a network view on an object I can observe the transform and still use onserialise in the script. This will however only serialise data when the transform changes. Is it safe to assume that a view that is observing a script will serialise data every time a variable in the script changes? If not, what is point of observing a script at all? And how do I tell it I want an extra serialise when myvariable changes? Do I have to send an RPC every time?

    If someone can clarify the details of exactly what happens when one observes a script that would be much appreciated. Alternatively, if those who claim the docs cover all this could link me to the page where my questions are answered that would also work.

    TYIA

    Edit: I have ascertained by trial and error that the view does serialise when script variables change. This nullifies the rest of my questions about the subject.
     
    Last edited: Feb 24, 2011
  11. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,086
    I believe that whatever data you specify that you want to serialize will automatically be transmitted whenever it changes.
     
  12. benb

    benb

    Joined:
    Aug 9, 2012
    Posts:
    20
    You can indeed observe a script, you simply drag the script component into the Observed field
     
  13. tumanoidb

    tumanoidb

    Joined:
    Feb 1, 2014
    Posts:
    3