Does Unet work with Javascript?

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.

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 = counter.ToString();
if (transform.GetComponent().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?

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 :stuck_out_tongue:

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

4 Likes

Actually that’s not stupid at all. You just saved me a lot of trouble, thank you. :slight_smile: Javascript is really different than C# in some cases.

Great ! Thank you :slight_smile:

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?

ok seems i’ve got it

@SyncVar(hook = “OnOwnerid_hook”)

function OnOwnerid_hook(id : int)
{…}

Nice, I was just looking at that today coincidentally. Wondering how exactly to do a hook. Glad you got it.