Help converting this C# line to UnityScript

score= (int)PhotonNetwork.room.customProperties[Score];

customProperties[Score] is referring to a key in a hashtable with an int as its value. What this line is doing is making the variable int score equal the int value of the key Score in a hashtable. I have never seen = (int) before but it kinda explains itself. Anyway how can I convert this to UnityScript, or better yet how can I retrieve the value of a key from within a hashtable because I’ve been trying to achieve this for the past 2 days.

The “(int)” is an explicit cast in C#. In C# such a cast is required if the value type you want to assign is of a more general type as the variable. For value types like int and float there is only one base-type: object.

So i guess that “customProperties” is an untyped Hashtable which contains “object” values. In UnityScript there is no explicit cast for value types. The compiler does this automatically for you. So if your target variable is of type “int” you can simply assign your value to it and the compiler will cast the value automatically:

var score : int;

// [...]
score = PhotonNetwork.room.customProperties[Score];

Since we declared the score variable as an int, the compiler will automatically try to cast the value into an int.

Hello superLuigi,

I am assuming you already use unity and this is a script that comes with Photon Network.

There are 2 ways you could do this.

Have your score become a public variable and you can change it in the editor or,
Create a new public Int and then have score equal that new Int.

There are a lot of ways things like this can be done.

If you could provide some more details like what its for, or other scripting or even if this is from another platform or engine then I could provide some more assistance.

Kind Regards,

IHackedDeath.

You can create a hashtable in js. then read from that, so assuming youhave got the Dictionary ‘PhotonNetwork.room.customProperties’, the following should work:

var hash : Hashtable = new Hashtable(PhotonNetwork.room.customProperties);
score = parseInt(hash[Score]);

I would suggest only setting that variable at the start and then if the values change, as it would prompt GC.