- Home /
Looking for some help on manually allocating NetworkViewIDs
Hey,
I've been trying to make an FPS lately. I made a server project and a client project, got a database working for storing account info, and got a simple (albeit buggy) room creation/joining system up.
I use RPCs to locally instantiate each player character. This works well, it detects which character is owned by which player and gives them access to movement if it's owned by the player. It also deletes unnecessary scripts and GameObjects on the player character prefab if it's the client isn't the owner (for example, the camera and a MouseLook script are being deleted now).
The problem is that I can't seem to get the NetworkViews to work properly. Each character has a NetworkView added to it when it's instantiated. I use Network.AllocateViewID() to set the NetworkViewID properly. I've tested with GUI and through the Inspector various times and this works (unless I'm mistaken): each player character has the same viewID on every client. It's set to observe the Transform with Reliable Delta Compressed as its state synchronization mode.
I've tested with two clients, but it doesn't work. The other player's movement is not sent across the network.
I thought it was because the players do not have a connection with each other, but rather only with the server, but when I tried to do the same method on the server, despite both NetworkViews having the same ID, the Transform wasn't being sent over.
So maybe I'm not fully aware of how NetworkViews work... I tried using NetworkView.SetScope() to true during the server test, but it didn't seem to make a difference.
What am I doing wrong?
Thanks in advance for any help that can be offered!
EDIT: Here's what the player character's script, Player.js, has inside it. I added some comments too. Mostly, it's a very simple movement system:
var translation:int; //Forward movement. -1, 0, or 1 based on input. var translation2:int; //Sideways movement. -1, 0, or 1 based on input. var functional:boolean = false; //Does this client own this Player? var transformNV:NetworkView; //'Transform Network View'. Another script //adds this NetworkView to the Player and sets it to Reliable Delta //Compressed and sets the Observed property to the Transform of the Player.
function Update() { if (functional) //If this script is owned by this client {
if (Input.GetKey("w")) //Checking each Input key
translation = 1; //Setting Translation based on it
if (Input.GetKey("s"))
translation = -1;
if (Input.GetKey("a"))
translation2 = -1;
if (Input.GetKey("d"))
translation2 = 1;
if (!Input.GetKey("w") && !Input.GetKey("s"))
translation = 0;
if (!Input.GetKey("d") && !Input.GetKey("a"))
translation2 = 0;
if (translation != 0 || translation2 != 0) //Translating:
transform.Translate((translation2 * 12) * Time.deltaTime,0,(translation * 12) * Time.deltaTime);
}
}
Some more details about my testing, hopefully it'll help:
I tested the Client project with 1 instance running in the Unity editor and another running in a built client (it won't let me open the same project twice, so one has to be a built client instead of in the editor).
I have each client connected to a Server, which is running as a separate project in the Unity editor.
I'm sure that the connection is working. I've sent lots of RPCs between the server and the clients.
I use the server to AllocateViewID() and send that viewID via RPC to other clients.
I've used GUI.Label on the built client to display the viewID of each player in the game. I looked over the results using the Label and the Inspector of the other client, and the player NetworkViewIDs match.
Just now, I looked over it some more, and it seems the NetworkViewID of my 'main menu' NetworkView (on the client, used to connect to rooms and the server) is matching the other client's player's NetworkViewID (they're both 2). Is that the problem? The main menu's NetworkViewID is 'Scene' and the other is 'Allocated'. The NetworkView for the Main Menu is off because I used it only for RPCs to and from the server.
Do you call AllocateViewID() after the server connection has been established?
$$anonymous$$ind you on reflection I also ended up just sending position information by RPC because I needed a packet for animation state too. It used to work on the transform being observed though. Do you have lots of log entries about receiving information for a viewId that doesn't exist?
When I allocateViewIDs for my players (on the player) then I always get them starting at 50+ BTW - no idea why, but that's what happens.
So my sequence is this:
Client connects to server
Client gets OnConnectedToServer message
Client creates networkView and allocates ViewID for avatar
Client send an AssignPlayer message to the server
Server broadcasts an AddPlayer message to all connected clients with the viewId for the new avatar
Obviously a bit of other data flies around in those calls like outfits, locations etc.
Well it won't be much slower if you only send updates when things have changed (that's reliable covered), you could do you own deltas (I do that for my animation states, which I had to do because Unity couldn't auto sync my animation states properly and they are part of multi-player gameplay for me which isn't true for most games), I don't know what they are doing for compressed. I'm not sending much data and given networks have $$anonymous$$imum packet sizes I'm probably actually sending no more data than I would if I used the observed property.
You'd have to interpolate anyway really - it's never really fast enough any other way.
You could also extrapolate too and blend the results if it becomes necessary.
Your answer
Follow this Question
Related Questions
View ID AllocatedID: ### not found during lookup. Strange behaviour may occur 0 Answers
Photon PUN, HTC Vive camera prefab over network instantiation 0 Answers
Can I delay the instantiation of the network objects? 1 Answer
Get NetworkPlayer from ViewID 1 Answer
Couldn't Invoke RPC Function ! 1 Answer