Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ----------------------------------------
- NETWORKING
- ----------------------------------------
- (COMMANDS)
- http://docs.unity3d.com/Manual/UNetActions.html | http://docs.unity3d.com/Manual/class-NetworkBehaviour.html
- Commands are the way for clients to request to do something on the server.
- Called from client to server, or server to server. Function only runs on server's machine.
- Can only be run from your own player object (isLocalPlayer must be true?), unlike RPC's.
- Must be prefixed Cmd, e.g. CmdFunctionName().
- Parameters must be simple (int, float, Vector3, bool, struct, GameObject[1][2], NetworkIdentity[1][2], etc).
- http://answers.unity3d.com/questions/1001474/unet-parameter-restrictions-for-command-and-client.html
- [1] http://forum.unity3d.com/threads/networkserver-findlocalobject-networkinstanceid-netid.327114/#post-2121215
- [2] http://forum.unity3d.com/threads/how-do-i-synchronize-gameobjects-or-components.347241/#post-2247157
- Instantiating objects (that show for everyone) must be done by server using NetworkServer.Spawn. If client is trying to do it, it must use a Command. Instantiate as normal on the server, then call "NetworkServer.Spawn(objInstance)" to make it appear to all clients.
- (?) To send reference to GameObject as parameter, send its netId (NetworkInstanceID). Can be found from its NetworkIdentity component.
- http://forum.unity3d.com/threads/networkserver-findlocalobject-networkinstanceid-netid.327114/#post-2121215
- "Just pass the GameObject (or NetworkIdentity) as a parameter to the ClientRpc call and the system will look it up for you."
- Client cannot access an "unowned" network object to run functions on it via GetComponent. To run a function on another object, it must be done by the server. The client, at the point of wanting to access the unowned object, should instead run a Command to make the server access it.
- Example: Client has detected a button via raycast. It wants to use "buttonGO.GetComponent<Button>().Press()" on the button, but can't, even if it's a Command. Instead, replace it with "CmdPressButton(buttonGO.GetComponent<NetworkIdentity>().netId)"
- The code inside the CmdPressButton(NetworkInstanceId id) will contain "GameObject targetGO = NetworkServer.FindLocalObject(id);", and "targetGO.GetComponent<Button>().Press()".
- The server has now called the Press() function on its machine. If the client is hooked to any SyncVars/SyncEvents that change, it can run a hook function, but if not, the Press() ran by the server should call an RpcPress() function.
- Cannot return values (?)
- (RPC)
- http://docs.unity3d.com/Manual/UNetActions.html | http://docs.unity3d.com/Manual/class-NetworkBehaviour.html
- Called from server to clients, function runs on every client.
- Called on "Hosts" also, because they are a local client.
- Must be prefixed Rpc, e.g. RpcFunctionName().
- Client RPC calls are not only invoked on player objects (like Commands are), they can be invoked on any NetworkIdentity object. (http://forum.unity3d.com/threads/unet-clientrpc-not-executing-on-host.320288/#post-2080713)
- (SyncVar/SyncEvent/SyncList<T0>/hooks)
- (General)
- Can only be changed by server/host.
- Type must be simple (int, float, Vector3, bool, struct, GameObject[1], NetworkIdentity[1], etc)
- [1] http://forum.unity3d.com/threads/how-do-i-synchronize-gameobjects-or-components.347241/#post-2247157
- Server value only syncs/updates to clients if not using a hook.
- Creating a "syncedVar" version of your member "var" can be useful.
- (Hooks)
- Hook functions fire when synced var changes. [SyncVar(hook="OnVarChanged")]
- If using a hook, synced var must be set/updated inside the hook function.
- Hook functions receive the updated/new value as a parameter "OnVarChanged(int newValue)", but member variable remains old (so comparisons can be made). Member var must be forced to update like so: "memberVar = newValue".
- Hooks do not run on server, just host/clients. (hosts??)
- (SyncEvent)
- ...
- (SyncLists)
- http://docs.unity3d.com/ScriptReference/Networking.SyncList_1.html
- public SyncListInt ints = new SyncListInt();
- public SyncList<int> ints;
- https://www.reddit.com/r/Unity3D/comments/3bbk1y/help_with_unity_networking/
- (OTHER)
- OnStartServer should be used for safety.
- http://forum.unity3d.com/threads/gotchas-feedback-criticism.339705/
- (SPAWNING)
- http://docs.unity3d.com/Manual/UNetSpawning.html
- A NetworkIdentity must be on the root game object of a spawnable prefab.
- NetworkBehaviour scripts must be on the same game object as the NetworkIdentity, not on child game objects.
- Prefabs can’t be registered with the NetworkManager unless they have a NetworkIdentity on their root object.
- Note that on the host, objects are not spawned for the local client as they already exist on the server. So no spawn handler functions will be called.
- Instantiating/spawning structures have to be read from server's variable.
Add Comment
Please, Sign In to add comment