Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice

Does Unet work with Javascript?

Discussion in 'UNet' started by Tolbiac, Jun 10, 2015.

  1. Tolbiac

    Tolbiac

    Joined:
    Jan 15, 2013
    Posts:
    3
    Hey there good people!

    I'm not the most code-savvy guy around these parts so pardon me if my question sounds stupid but: does Unet work with javascript/unityscript? I knew how to use the old network system fairly well with javascript, however since it's going to be eventualy replaced by Unet, I decided to take a look at it... and didn't find much in term of documentation for Unet in Javascript (most of it is written for C#).

    I've tried to decypher the C# examples, and so far I managed to do the basic such as getting your server online, have someone join, spawn some players, etc (most of it was relatively similar to the old system anyway). All in Javascript. However I wanted to try the new @SyncVar and @ClientRpc attributes... and that's where the problems start: the new attributes don't seem to be doing anything? Yes the objects have a network identity component. But when I try to modify a variable that follows a @SyncVar with my server, the client doesn't seem to receive the changes. Same for @ClientRpc.

    At this point I'm guessing I'm just too green at this and simply don't get how the new system works and I should read some more about it. But just in case I'd like to know if the issue isn't simply that javascript isn't currently supported, or poorly? If so, are there known workarounds?

    Thanks for the help.
     
  2. Tolbiac

    Tolbiac

    Joined:
    Jan 15, 2013
    Posts:
    3
    Okay so, I guess I may not have been quite clear, or maybe people think I'm just a rambling fool. But I actually don't know what's up and would like some help.

    As an example I coded a simple counter that is changed by the server, and synced with a client.
    in C# it works perfectly:

    using UnityEngine;
    using UnityEngine.UI;
    using UnityEngine.Networking;
    using System.Collections;

    public class synctest : NetworkBehaviour {

    [SyncVar]
    public float counter = 0;

    void Update () {
    transform.GetComponent<Text>().text = counter.ToString();
    if (transform.GetComponent<NetworkIdentity>().isServer) counter = counter+1 ;
    }
    }


    However it doesn't in Javascript:

    import UnityEngine.Networking;
    import UnityEngine.UI;

    @SyncVar
    var counter : float;

    function Update() {
    transform.GetComponent(Text).text = counter.ToString();
    if (transform.GetComponent(NetworkIdentity).isServer) counter +=1 ;
    }


    I'm guessing I'm doing something horribly wrong (I'm not a real coder) but I would love to be told so by someone that could help me understand this :3 Thanks.


    Edit: from the scripting manual "Using Javascript every script automatically derives from MonoBehaviour. When using C# or Boo you have to explicitly derive from MonoBehaviour." so I guess that's my issue, somehow I'd need my javascript scripts to derive from NetworkBehaviour, if that's even possible?
     
    Last edited: Jun 10, 2015
  3. Tolbiac

    Tolbiac

    Joined:
    Jan 15, 2013
    Posts:
    3
    Aaaand I found my own solution.

    #pragma strict
    import UnityEngine.Networking;
    import UnityEngine.UI;

    class synctest extends NetworkBehaviour {
    @SyncVar
    var counter : float;

    function Update() {
    transform.GetComponent(Text).text = counter.ToString();
    if (transform.GetComponent(NetworkIdentity).isServer) counter +=1 ;
    }
    }
    It's probably super stupid to most of you, but when you learn to code on the fly to do some basic stuff for your game project, man, that's hard stuff :p

    Would be nice to have that kind of thing available in the javascript documentation.

     
    Last edited: Jun 11, 2015
  4. Velo222

    Velo222

    Joined:
    Apr 29, 2012
    Posts:
    1,437
    Actually that's not stupid at all. You just saved me a lot of trouble, thank you. :) Javascript is really different than C# in some cases.
     
  5. Protozoaire

    Protozoaire

    Joined:
    Apr 14, 2015
    Posts:
    61
    Great ! Thank you :)
     
  6. OlliQueck

    OlliQueck

    Joined:
    Apr 11, 2013
    Posts:
    49
    i'm in the same boat. Trying to work with JS and UNET. I'm stuck with getting a hook out of a syncvar. No docs either. anyone has an idea?
     
  7. OlliQueck

    OlliQueck

    Joined:
    Apr 11, 2013
    Posts:
    49
    ok seems i've got it

    @SyncVar(hook = "OnOwnerid_hook")

    function OnOwnerid_hook(id : int)
    {...}
     
  8. Velo222

    Velo222

    Joined:
    Apr 29, 2012
    Posts:
    1,437
    Nice, I was just looking at that today coincidentally. Wondering how exactly to do a hook. Glad you got it.