- Home /
How to get current NetworkPlayer from a script belonging to it ?
I suppose it should be quite easy to get current Network player from a script attached to it , but I didn't manage to get any answer I understand (and I searched a lot ^^).
In my program, I instantiate a prefab with networkviews when I create a player. I have a script attached to this prefab. How can I know to which networkplayer this script is attached inside this script ?
Now, the only solution I have found is to send a message to this script when I instantiate it, with the Network Player in the arguments, but I am pretty sure there must be easier ways !
Answer by Edwige · Jan 31, 2011 at 01:06 PM
I have found a very easy way to get the local NetworkPlayer when you are in a script : Network.player
You said you want the networkplayer of a certain network view and not the local networkplayer. Anyway, fact is that on clients the only networkplayer you can trust is the server since that's the only one you're connected to. On the server you have all available, cause all are connected to the server. That's why i always setup an authoritative server and everything is managed on the server but that's a lot of work ;)
Answer by Matthew A · Jan 28, 2011 at 01:52 PM
Not sure if this is entirely what you mean but:
It should be possible to get all the network views attached to the object with:
var views : NetworkView[] = GetComponents(NetworkView);
Then you can iterate through them and use the observed member to get the observed script:
for (view : NetworkView in views) {
if (this == view.observed) {
// current view is observing this script
break;
}
}
Thanks for the answer. With your method and the other stated before, I come to that :
NetworkView[] views = GetComponents(); for (int i=0;i
Answer by Bunny83 · Jan 28, 2011 at 01:26 PM
You can't "attach" something to a networkplayer. The networkplayer is just an identifer for a player on the network. If you instantiate your player prefab with Network.Instantiate and the prefab contains a NetworkView, this NetworkView belongs to the player that called Network.Instantiate.
Inside a script you can get the NetworkView component and check it's owner.
{
if (networkView.owner == Network.player)
{
Actually it's not the NetworkView that is owned by the player, it's the NetworkViewID of that NetworkView. This ID links the NetworkViews on all those different clients.
Some further notes:
I don't like Network.Instantiate() because you have no control of the creation and the much more important: the removing of the object. Since Network.Instantiate() uses buffered RPCs it's really hard to removed them selectively. It depends on your network setup. If you don't need an authoritative server it's quite easy. When a player leaves, everything that belongs to that player can be removed. But it still depends on what you need.
Ok, this helps me understanding :) But it doesn't really solve the problem : how can I get the networkview the script is attached to, then ? :)
Like i did in the scipt above... Every component have some default references to other common components like transform, renderer, rigidbody and networkView (note it's not NetworkView). look here: http://unity3d.com/support/documentation/ScriptReference/Component-networkView.html?from=$$anonymous$$onoBehaviour
Isn't that essentially the same as using networkView.Is$$anonymous$$ine?
No, I needed the player number :) I have to search an array for an id corresponding to this specific player (the array is created when player is logged in). With Network.player, it works fine.
ehmm, you never said anything about your own playermanagement with any numbers or what ever. You always talk about NetworkPlayer and that's a type Unity uses to identify a player on the network. You didn't post any code and it seems you haven't been very specific what you wanna do.