RPC equivalent methods in UNET?

Hello Unity Rep,

My game is currently modeled to use RPC calls (old way) for communication between the Clients and Server; however I’d like to rework my current model to use UNET (new way). I am wondering if there is any easy way of doing this without having to rewrite all of my structures and interfaces.

Is there a RPC equivalent method call for UNET that I could simply replace my current model with?

Are there any examples of a projects that shows the old RPC method and the same project using the new UNET?

Thanks for any and all help!

@aengesse

Sadly the new RPC system very different, and quite a bit more difficult than the old one, so there’s not really any good way to upgrade without rewriting a lot of things.

I would still recommend to update though, since the new system is faster, more reliable and also way more secure from potential hackers.

First of, all scripts using the new Network behaviours now have to arrive from the class NetworkBehaviour and use UnityEngine.Networking. A script’s top should look like this:

using UnityEngine;
using System.Collections;
using UnityEngine.Networking;

public class MyNetworkClass : NetworkBehaviour {

Also RPCs now have to start with the prefix “rpc”, and have [ClientRpc] written above them.
A new RPC-like function has also been added called a Command. These have to start with the prefix “cmd”, and have [Command] written above them.

Another very useful new feature is syncvars.

These are created by writing [SyncVar] above or infront of a variable. This variable’s value on the server will be synced across the network to all clients.

A (hook=“”) can be added to these, causing a function to be called whenever this variable is called.

Example:

using UnityEngine;
using System.Collections;
using UnityEngine.Networking;

public class MyNetworkExample : NetworkBehaviour 
{
	[SyncVar] private string MySyncString; // This string will sync across the network. It's value on the server will overide the value on all clients. Therefore this variable can only be changed only by the server, but it'll be the same on all clients

	[SyncVar] private int MySyncInt; // This int will sync across the network. It's value on the server will overide the value on all clients. Therefore this variable can only be changed only by the server, but it'll be the same on all clients

	[SyncVar] private float MySyncFloat;// This float will sync across the network. It's value on the server will overide the value on all clients. Therefore this variable can only be changed only by the server, but it'll be the same on all clients

	[SyncVar] private bool MySyncBool;// This bool will sync across the network. It's value on the server will overide the value on all clients. Therefore this variable can only be changed only by the server, but it'll be the same on all clients

	[SyncVar(hook="MyHookFunction")] private string MySyncStringWithHook; // This is a hook, and the function "MyHookFunction" will be called whenever the variable changes.



	void MyHookFunction(string hook) //This function will be called when "MySyncStringWithHook" changes.
	{
		Debug.Log("MySyncStringWithHook changed");
	}

	[ClientRpc]
	void rpc_MyRPC () // how to set up a RPC
	{

	}

	[Command]
	void cmd_MyCommmand() // How to set up a Command
	{

	}

}

It’s difficult to explain exactly how the new system works, but this series of tutorials does a great job at explaining the basics:

It’s worth noting that in the new UNET system only objects owned by the local client is able to send RPC’s on that client. This means that you as an example won’t be able to send RPC’s for the other players.

While this may seem like a bug, it’s done intentional as security against hackers, although if you want to bypass this, you can, in unity 5.2 by calling -

NetworkIdentity n = gameObject.GetComponent<NetworkIdentity>();

n.localPlayerAuthority = true;

On all the objects not owned by the local player that you want to send RPCs from.

I hope that this helps :slight_smile: