Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  4. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

udp server - how would i call a function to run on the server?

Discussion in 'Connected Games' started by xred13, Sep 14, 2018.

  1. xred13

    xred13

    Joined:
    Jul 14, 2018
    Posts:
    47
    So i've been thinking about writing a costum network manager, how would i, as a client, tell the server to run a function? Would i have to get a specific message relating to that function and in case the server got that message it would run the function? In that case, the server would also need to search for the specific gameobject and call the function, right?
     
  2. xred13

    xred13

    Joined:
    Jul 14, 2018
    Posts:
    47
    For example, with unet i can simply write a command calling a rpc and inside that rpc i would write code like i would normally do in the client, so it gets called in other clients. What is the best approach to this?
     
  3. xred13

    xred13

    Joined:
    Jul 14, 2018
    Posts:
    47
    simpler:

    tell other clients to run a function on the same script and gameobject as i am currently in
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    3,339
    I recently implemented this type of thing myself, and here was my approach.

    The custom network manager maintains a list of all networked gameobjects, added to when spawned or found as part of the scene. All networked gameobjects have a script that that contains a unique ID for that networked object (set manually if part of the scene, or generated by the server if spawned). Both the clients and server know the ID of that object. The same script contains a list of all network aware scripts on the object (analogous to Unet's NetworkBehaviour scripts). Each network aware script on the object maintains a list of RPC receiving delegates that point to methods within that script. All of my RPC receiving methods take the same parameters, an RPC message and the client/server connection the message came from.

    When I send an RPC message I include the unique object ID, the network script index number, the RPC delegate index number, and whatever payload I want to send serialized as a byte array.

    The receiving machine searches the list of networked gameobjects maintained by the network manager for the object matching the object ID. It then passes the message off to a script on that object which looks up the correct network aware script by index, and then the correct RPC delegate method is called by its index. That RPC delegate method then deserilizes whatever payload I sent it and it does its thing, whatever I intended it to do.
     
    Last edited: Sep 15, 2018
  5. xred13

    xred13

    Joined:
    Jul 14, 2018
    Posts:
    47
    Thank You! :)
     
    Joe-Censored likes this.