Photon Unity Networking 2 2.45

Public Member Functions | Static Public Attributes | Protected Member Functions | Protected Attributes | List of all members
PhotonHandler Class Reference

Internal MonoBehaviour that allows Photon to run an Update loop. More...

Inherits ConnectionHandler, IInRoomCallbacks, and IMatchmakingCallbacks.

Public Member Functions

void OnCreatedRoom ()
 Called when this client created a room and entered it. OnJoinedRoom() will be called as well. More...
 
void OnRoomPropertiesUpdate (Hashtable propertiesThatChanged)
 Called when room properties changed. The propertiesThatChanged contain only the keys that changed. More...
 
void OnPlayerPropertiesUpdate (Player targetPlayer, Hashtable changedProps)
 Called when custom player-properties are changed. More...
 
void OnMasterClientSwitched (Player newMasterClient)
 Called after switching to a new MasterClient when the current one leaves. More...
 
void OnFriendListUpdate (System.Collections.Generic.List< FriendInfo > friendList)
 
void OnCreateRoomFailed (short returnCode, string message)
 Called when the server couldn't create a room (OpCreateRoom failed). More...
 
void OnJoinRoomFailed (short returnCode, string message)
 Called when a previous OpJoinRoom call failed on the server. More...
 
void OnJoinRandomFailed (short returnCode, string message)
 Called when a previous OpJoinRandom (or OpJoinRandomOrCreateRoom etc.) call failed on the server. More...
 
void OnJoinedRoom ()
 Called when the LoadBalancingClient entered a room, no matter if this client created it or simply joined. More...
 
void OnLeftRoom ()
 Called when the local user/client left a room, so the game's logic can clean up it's internal state. More...
 
void OnPlayerEnteredRoom (Player newPlayer)
 Called when a remote player entered the room. This Player is already added to the playerlist. More...
 
void OnPlayerLeftRoom (Player otherPlayer)
 Called when a remote player left the room or became inactive. Check otherPlayer.IsInactive. More...
 
- Public Member Functions inherited from ConnectionHandler
void StartFallbackSendAckThread ()
 
void StopFallbackSendAckThread ()
 
bool RealtimeFallbackThread ()
 A thread which runs independent from the Update() calls. Keeps connections online while loading or in background. See KeepAliveInBackground. More...
 
- Public Member Functions inherited from IMatchmakingCallbacks
void OnFriendListUpdate (List< FriendInfo > friendList)
 Called when the server sent the response to a FindFriends request. More...
 

Static Public Attributes

static int MaxDatagrams = 10
 Limits the number of datagrams that are created in each LateUpdate. More...
 
static bool SendAsap
 Signals that outgoing messages should be sent in the next LateUpdate call. More...
 
- Static Public Attributes inherited from ConnectionHandler
static bool AppQuits
 Indicates that the app is closing. Set in OnApplicationQuit(). More...
 
static bool AppPause
 
static bool AppPauseRecent
 
static bool AppOutOfFocus
 
static bool AppOutOfFocusRecent
 

Protected Member Functions

override void Awake ()
 
virtual void OnEnable ()
 
void Start ()
 
override void OnDisable ()
 
void FixedUpdate ()
 Called in intervals by UnityEngine. Affected by Time.timeScale. More...
 
void LateUpdate ()
 Called in intervals by UnityEngine, after running the normal game code and physics. More...
 
void Dispatch ()
 Dispatches incoming network messages for PUN. Called in FixedUpdate or LateUpdate. More...
 

Protected Attributes

List< int > reusableIntList = new List<int>()
 

Additional Inherited Members

- Static Public Member Functions inherited from ConnectionHandler
static bool IsNetworkReachableUnity ()
 When run in Unity, this returns Application.internetReachability != NetworkReachability.NotReachable. More...
 
- Public Attributes inherited from ConnectionHandler
bool DisconnectAfterKeepAlive = false
 Option to let the fallback thread call Disconnect after the KeepAliveInBackground time. Default: false. More...
 
int KeepAliveInBackground = 60000
 Defines for how long the Fallback Thread should keep the connection, before it may time out as usual. More...
 
bool ApplyDontDestroyOnLoad = true
 Keeps the ConnectionHandler, even if a new scene gets loaded. More...
 
- Properties inherited from ConnectionHandler
LoadBalancingClient Client [get, set]
 Photon client to log information and statistics from. More...
 
int CountSendAcksOnly [get]
 Counts how often the Fallback Thread called SendAcksOnly, which is purely of interest to monitor if the game logic called SendOutgoingCommands as intended. More...
 
bool FallbackThreadRunning [get]
 True if a fallback thread is running. Will call the client's SendAcksOnly() method to keep the connection up. More...
 

Detailed Description

Internal MonoBehaviour that allows Photon to run an Update loop.

Member Function Documentation

◆ Dispatch()

void Dispatch ( )
protected

Dispatches incoming network messages for PUN. Called in FixedUpdate or LateUpdate.

It may make sense to dispatch incoming messages, even if the timeScale is near 0. That can be configured with PhotonNetwork.MinimalTimeScaleToDispatchInFixedUpdate.

Without dispatching messages, PUN won't change state and does not handle updates.

◆ FixedUpdate()

void FixedUpdate ( )
protected

Called in intervals by UnityEngine. Affected by Time.timeScale.

◆ LateUpdate()

void LateUpdate ( )
protected

Called in intervals by UnityEngine, after running the normal game code and physics.

◆ OnCreatedRoom()

void OnCreatedRoom ( )

Called when this client created a room and entered it. OnJoinedRoom() will be called as well.

This callback is only called on the client which created a room (see OpCreateRoom).

As any client might close (or drop connection) anytime, there is a chance that the creator of a room does not execute OnCreatedRoom.

If you need specific room properties or a "start signal", implement OnMasterClientSwitched() and make each new MasterClient check the room's state.

Implements IMatchmakingCallbacks.

◆ OnCreateRoomFailed()

void OnCreateRoomFailed ( short  returnCode,
string  message 
)

Called when the server couldn't create a room (OpCreateRoom failed).

Creating a room may fail for various reasons. Most often, the room already exists (roomname in use) or the RoomOptions clash and it's impossible to create the room.

When creating a room fails on a Game Server: The client will cache the failure internally and returns to the Master Server before it calls the fail-callback. This way, the client is ready to find/create a room at the moment of the callback. In this case, the client skips calling OnConnectedToMaster but returning to the Master Server will still call OnConnected. Treat callbacks of OnConnected as pure information that the client could connect.

Parameters
returnCodeOperation ReturnCode from the server.
messageDebug message for the error.

Implements IMatchmakingCallbacks.

◆ OnJoinedRoom()

void OnJoinedRoom ( )

Called when the LoadBalancingClient entered a room, no matter if this client created it or simply joined.

When this is called, you can access the existing players in Room.Players, their custom properties and Room.CustomProperties.

In this callback, you could create player objects. For example in Unity, instantiate a prefab for the player.

If you want a match to be started "actively", enable the user to signal "ready" (using OpRaiseEvent or a Custom Property).

Implements IMatchmakingCallbacks.

◆ OnJoinRandomFailed()

void OnJoinRandomFailed ( short  returnCode,
string  message 
)

Called when a previous OpJoinRandom (or OpJoinRandomOrCreateRoom etc.) call failed on the server.

The most common causes are that a room is full or does not exist (due to someone else being faster or closing the room).

This operation is only ever sent to the Master Server. Once a room is found by the Master Server, the client will head off to the designated Game Server and use the operation Join on the Game Server.

When using multiple lobbies (via OpJoinLobby or a TypedLobby parameter), another lobby might have more/fitting rooms.

Parameters
returnCodeOperation ReturnCode from the server.
messageDebug message for the error.

Implements IMatchmakingCallbacks.

◆ OnJoinRoomFailed()

void OnJoinRoomFailed ( short  returnCode,
string  message 
)

Called when a previous OpJoinRoom call failed on the server.

Joining a room may fail for various reasons. Most often, the room is full or does not exist anymore (due to someone else being faster or closing the room).

When joining a room fails on a Game Server: The client will cache the failure internally and returns to the Master Server before it calls the fail-callback. This way, the client is ready to find/create a room at the moment of the callback. In this case, the client skips calling OnConnectedToMaster but returning to the Master Server will still call OnConnected. Treat callbacks of OnConnected as pure information that the client could connect.

Parameters
returnCodeOperation ReturnCode from the server.
messageDebug message for the error.

Implements IMatchmakingCallbacks.

◆ OnLeftRoom()

void OnLeftRoom ( )

Called when the local user/client left a room, so the game's logic can clean up it's internal state.

When leaving a room, the LoadBalancingClient will disconnect the Game Server and connect to the Master Server. This wraps up multiple internal actions.

Wait for the callback OnConnectedToMaster, before you use lobbies and join or create rooms.

OnLeftRoom also gets called, when the application quits. It makes sense to check static ConnectionHandler.AppQuits before loading scenes in OnLeftRoom().

Implements IMatchmakingCallbacks.

◆ OnMasterClientSwitched()

void OnMasterClientSwitched ( Player  newMasterClient)

Called after switching to a new MasterClient when the current one leaves.

This is not called when this client enters a room. The former MasterClient is still in the player list when this method get called.

Implements IInRoomCallbacks.

◆ OnPlayerEnteredRoom()

void OnPlayerEnteredRoom ( Player  newPlayer)

Called when a remote player entered the room. This Player is already added to the playerlist.

If your game starts with a certain number of players, this callback can be useful to check the Room.playerCount and find out if you can start.

Implements IInRoomCallbacks.

◆ OnPlayerLeftRoom()

void OnPlayerLeftRoom ( Player  otherPlayer)

Called when a remote player left the room or became inactive. Check otherPlayer.IsInactive.

If another player leaves the room or if the server detects a lost connection, this callback will be used to notify your game logic.

Depending on the room's setup, players may become inactive, which means they may return and retake their spot in the room. In such cases, the Player stays in the Room.Players dictionary.

If the player is not just inactive, it gets removed from the Room.Players dictionary, before the callback is called.

Implements IInRoomCallbacks.

◆ OnPlayerPropertiesUpdate()

void OnPlayerPropertiesUpdate ( Player  targetPlayer,
Hashtable  changedProps 
)

Called when custom player-properties are changed.

Changing properties must be done by Player.SetCustomProperties, which causes this callback locally, too.

Parameters
targetPlayerContains Player that changed.
changedPropsContains the properties that changed.

Implements IInRoomCallbacks.

◆ OnRoomPropertiesUpdate()

void OnRoomPropertiesUpdate ( Hashtable  propertiesThatChanged)

Called when room properties changed. The propertiesThatChanged contain only the keys that changed.

In most cases, this method gets called when some player changes the Room Properties. However, there are also "Well Known Properties" (which use byte keys) and this callback may include them. Especially when entering a room, the server will also send the required Well Known Properties and they are not filtered out for the OnRoomPropertiesUpdate callback.

You can safely ignore the byte typed keys in propertiesThatChanged.

Changing properties is usually done by Room.SetCustomProperties.

Parameters
propertiesThatChanged

Implements IInRoomCallbacks.

Member Data Documentation

◆ MaxDatagrams

int MaxDatagrams = 10
static

Limits the number of datagrams that are created in each LateUpdate.

Helps spreading out sending of messages minimally.

◆ SendAsap

bool SendAsap
static

Signals that outgoing messages should be sent in the next LateUpdate call.

Up to MaxDatagrams are created to send queued messages.