Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Unity Multiplayer Games

You're reading from   Unity Multiplayer Games Take your gaming development skills into the online multiplayer arena by harnessing the power of Unity 4 or 3. This is not a dry tutorial ‚Äì it uses exciting examples and an enthusiastic approach to bring it all to life.

Arrow left icon
Product type Paperback
Published in Dec 2013
Publisher Packt
ISBN-13 9781849692328
Length 242 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Alan R. Stagner Alan R. Stagner
Author Profile Icon Alan R. Stagner
Alan R. Stagner
Arrow right icon
View More author details
Toc

Table of Contents (9) Chapters Close

Preface 1. Unity Networking – The Pong Game FREE CHAPTER 2. Photon Unity Networking – The Chat Client 3. Photon Server – Star Collector 4. Player.IO – Bot Wars 5. PubNub – The Global Chatbox 6. Entity Interpolation and Prediction 7. Server-side Hit Detection Index

Using RPCs

RPCs are useful for single, self-contained messages that need to be sent, such as a character firing a gun, or a player saying something in chat.

In Unity, RPCs are methods marked with the [RPC] attribute. This can be called by name via networkView.RPC( "methodName", … ). For example, the following script prints to the console on all machines when the space key is pressed.

using UnityEngine;
using System.Collections;

public class ExampleUnityNetworkCallRPC : MonoBehavior
{
  void Update()
  {
    // important – make sure not to run if this networkView is notours
    if( !networkView.isMine )
      return;

    // if space key is pressed, call RPC for everybody
    if( Input.GetKeyDown( KeyCode.Space ) )
      networkView.RPC( "testRPC", RPCMode.All );
  }

  [RPC]
  void testRPC( NetworkMessageInfo info )
  {
    // log the IP address of the machine that called this RPC
    Debug.Log( "Test RPC called from " + info.sender.ipAddress );
  }
}

Also note the use of NetworkView.isMine to determine ownership of an object. All scripts will run 100 percent of the time regardless of whether your machine owns the object or not, so you have to be careful to avoid letting some logic run on remote machines; for example, player input code should only run on the machine that owns the object.

RPCs can either be sent to a number of players at once, or to a specific player. You can either pass an RPCMode to specify which group of players to receive the message, or a specific NetworkPlayer to send the message to. You can also specify any number of parameters to be passed to the RPC method.

RPCMode includes the following entries:

  • All (the RPC is called for everyone)
  • AllBuffered (the RPC is called for everyone, and then buffered for when new players connect, until the object is destroyed)
  • Others (the RPC is called for everyone except the sender)
  • OthersBuffered (the RPC is called for everyone except the sender, and then buffered for when new players connect, until the object is destroyed)
  • Server (the RPC is sent to the host machine)

Note

Note that, with the exception of RPCMode.All and RPCMode.AllBuffered, a client cannot send an RPC to itself.

You have been reading a chapter from
Unity Multiplayer Games
Published in: Dec 2013
Publisher: Packt
ISBN-13: 9781849692328
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image