- Home /
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.
Answer by Bunny83 · Sep 01, 2015 at 12:36 AM
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.
Answer by IHackedDeath · Aug 31, 2015 at 04:35 AM
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.
This script actually comes with a tutorial called Sky Arena that uses Photon. What I'm trying to achieve is have all 4 players associate the correct client(player) with the correct player#. If I create the room I have a photon ID of 1, the next person ID 2, etc. However, if player two leaves and rejoins he is now ID 3, photon doesn't reuse ID's. When you create a room in Photon you can store customProperties that all clients in the room can see. These customProperties have to be a hashtable, so I have a hashtale with p1, p2, p3, & p4 as keys and int's as values. The int will equal a player's ID. I want to use something like
player2 : PhotonPlayer;
p2int = (int)PhotonNetwork.room.customProperties[p2];// I want the int value of p2's key player2 = PhotonNetwork.room.Find(p2int);
Answer by Scribe · Aug 31, 2015 at 05:23 PM
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.
I get the error: "Ambiguous reference 'parseInt':". I don't think Unity recognizes parseInt. Also, parseInt seems like it turns a string into an int. I'm not 100% sure of this since I've never come across parseInt before, but if that's what it's doing then it won't really help my problem. What i want is to get the value of a key in a hashtable
Your answer
Follow this Question
Related Questions
Custom class and hashtable 1 Answer
Dictionary vs Hashtable 1 Answer
How to call value by key when key is an object? 1 Answer
Can you Help me about how to get the variable of Java Script to C# script? 2 Answers
From Java to C# help 1 Answer