ExecuteRPC









How do I use Execute R P C
Below are practical examples compiled from projects for learning and reference purposes

Featured Snippets


File name: NetworkingPeer.cs Copy
1631     public void OnEvent(EventData photonEvent)
1632     {
1633         if (PhotonNetwork.logLevel >= PhotonLogLevel.Informational)
1634             Debug.Log(string.Format("OnEvent: {0}", photonEvent.ToString()));
1635
1636         int actorNr = -1;
1637         PhotonPlayer originatingPlayer = null;
1638
1639         if (photonEvent.Parameters.ContainsKey(ParameterCode.ActorNr))
1640         {
1641             actorNr = (int)photonEvent[ParameterCode.ActorNr];
1642             if (this.mActors.ContainsKey(actorNr))
1643             {
1644                 originatingPlayer = (PhotonPlayer)this.mActors[actorNr];
1645             }
1646             //else
1647             //{
1648             // // the actor sending this event is not in actorlist. this is usually no problem
1649             // if (photonEvent.Code != (byte)LiteOpCode.Join)
1650             // {
1651             // Debug.LogWarning("Received event, but we do not have this actor: " + actorNr);
1652             // }
1653             //}
1654         }
1655
1656         switch (photonEvent.Code)
1657         {
1658             case PunEvent.OwnershipRequest:
1659             {
1660                 int[] requestValues = (int[]) photonEvent.Parameters[ParameterCode.CustomEventContent];
1661                 int requestedViewId = requestValues[0];
1662                 int currentOwner = requestValues[1];
1663                 Debug.Log("Ev OwnershipRequest: " + photonEvent.Parameters.ToStringFull() + " ViewID: " + requestedViewId + " from: " + currentOwner + " Time: " + Environment.TickCount%1000);
1664
1665                 PhotonView requestedView = PhotonView.Find(requestedViewId);
1666                 if (requestedView == null)
1667                 {
1668                     Debug.LogWarning("Can't find PhotonView of incoming OwnershipRequest. ViewId not found: " + requestedViewId);
1669                     break;
1670                 }
1671
1672                 Debug.Log("Ev OwnershipRequest PhotonView.ownershipTransfer: " + requestedView.ownershipTransfer + " .ownerId: " + requestedView.ownerId + " isOwnerActive: " + requestedView.isOwnerActive + ". This client's player: " + PhotonNetwork.player.ToStringFull());
1673
1674                 switch (requestedView.ownershipTransfer)
1675                 {
1676                     case OwnershipOption.Fixed:
1677                         Debug.LogWarning("Ownership mode == fixed. Ignoring request.");
1678                         break;
1679                     case OwnershipOption.Takeover:
1680                         if (currentOwner == requestedView.ownerId)
1681                         {
1682                             // a takeover is successful automatically, if taken from current owner
1683                             requestedView.ownerId = actorNr;
1684                         }
1685                         break;
1686                     case OwnershipOption.Request:
1687                         if (currentOwner == PhotonNetwork.player.ID || PhotonNetwork.player.isMasterClient)
1688                         {
1689                             if ((requestedView.ownerId == PhotonNetwork.player.ID) || (PhotonNetwork.player.isMasterClient && !requestedView.isOwnerActive))
1690                             {
1691                                 SendMonoMessage(PhotonNetworkingMessage.OnOwnershipRequest, new object[] {requestedView, originatingPlayer});
1692                             }
1693                         }
1694                         break;
1695                     default:
1696                         break;
1697                 }
1698             }
1699                 break;
1700
1701             case PunEvent.OwnershipTransfer:
1702                 {
1703                     int[] transferViewToUserID = (int[]) photonEvent.Parameters[ParameterCode.CustomEventContent];
1704                     Debug.Log("Ev OwnershipTransfer. ViewID " + transferViewToUserID[0] + " to: " + transferViewToUserID[1] + " Time: " + Environment.TickCount%1000);
1705
1706                     int requestedViewId = transferViewToUserID[0];
1707                     int newOwnerId = transferViewToUserID[1];
1708
1709                     PhotonView pv = PhotonView.Find(requestedViewId);
1710                     pv.ownerId = newOwnerId;
1711
1712                     break;
1713                 }
1714             case EventCode.GameList:
1715                 {
1716                     this.mGameList = new Dictionary();
1717                     Hashtable games = (Hashtable)photonEvent[ParameterCode.GameList];
1718                     foreach (DictionaryEntry game in games)
1719                     {
1720                         string gameName = (string)game.Key;
1721                         this.mGameList[gameName] = new RoomInfo(gameName, (Hashtable)game.Value);
1722                     }
1723                     mGameListCopy = new RoomInfo[mGameList.Count];
1724                     mGameList.Values.CopyTo(mGameListCopy, 0);
1725                     SendMonoMessage(PhotonNetworkingMessage.OnReceivedRoomListUpdate);
1726                     break;
1727                 }
1728
1729             case EventCode.GameListUpdate:
1730                 {
1731                     Hashtable games = (Hashtable)photonEvent[ParameterCode.GameList];
1732                     foreach (DictionaryEntry room in games)
1733                     {
1734                         string gameName = (string)room.Key;
1735                         RoomInfo game = new RoomInfo(gameName, (Hashtable)room.Value);
1736                         if (game.removedFromList)
1737                         {
1738                             this.mGameList.Remove(gameName);
1739                         }
1740                         else
1741                         {
1742                             this.mGameList[gameName] = game;
1743                         }
1744                     }
1745                     this.mGameListCopy = new RoomInfo[this.mGameList.Count];
1746                     this.mGameList.Values.CopyTo(this.mGameListCopy, 0);
1747                     SendMonoMessage(PhotonNetworkingMessage.OnReceivedRoomListUpdate);
1748                     break;
1749                 }
1750
1751             case EventCode.QueueState:
1752                 // not used anymore
1753                 break;
1754
1755             case EventCode.AppStats:
1756                 // Debug.LogInfo("Received stats!");
1757                 this.mPlayersInRoomsCount = (int)photonEvent[ParameterCode.PeerCount];
1758                 this.mPlayersOnMasterCount = (int)photonEvent[ParameterCode.MasterPeerCount];
1759                 this.mGameCount = (int)photonEvent[ParameterCode.GameCount];
1760                 break;
1761
1762             case EventCode.Join:
1763                 // actorNr is fetched out of event above
1764                 Hashtable actorProperties = (Hashtable)photonEvent[ParameterCode.PlayerProperties];
1765                 if (originatingPlayer == null)
1766                 {
1767                     bool isLocal = this.mLocalActor.ID == actorNr;
1768                     this.AddNewPlayer(actorNr, new PhotonPlayer(isLocal, actorNr, actorProperties));
1769                     this.ResetPhotonViewsOnSerialize(); // This sets the correct OnSerializeState for Reliable OnSerialize
1770                 }
1771
1772                 if (actorNr == this.mLocalActor.ID)
1773                 {
1774                     // in this player's 'own' join event, we get a complete list of players in the room, so check if we know all players
1775                     int[] actorsInRoom = (int[])photonEvent[ParameterCode.ActorList];
1776                     foreach (int actorNrToCheck in actorsInRoom)
1777                     {
1778                         if (this.mLocalActor.ID != actorNrToCheck && !this.mActors.ContainsKey(actorNrToCheck))
1779                         {
1780                             this.AddNewPlayer(actorNrToCheck, new PhotonPlayer(false, actorNrToCheck, string.Empty));
1781                         }
1782                     }
1783
1784                     // joinWithCreateOnDemand can turn an OpJoin into creating the room. Then actorNumber is 1 and callback: OnCreatedRoom()
1785                     if (this.mLastJoinType == JoinType.JoinOrCreateOnDemand && this.mLocalActor.ID == 1)
1786                     {
1787                         SendMonoMessage(PhotonNetworkingMessage.OnCreatedRoom);
1788                     }
1789                     SendMonoMessage(PhotonNetworkingMessage.OnJoinedRoom); //Always send OnJoinedRoom
1790
1791                 }
1792                 else
1793                 {
1794                     SendMonoMessage(PhotonNetworkingMessage.OnPhotonPlayerConnected, this.mActors[actorNr]);
1795                 }
1796                 break;
1797
1798             case EventCode.Leave:
1799                 this.HandleEventLeave(actorNr);
1800                 break;
1801
1802             case EventCode.PropertiesChanged:
1803                 int targetActorNr = (int)photonEvent[ParameterCode.TargetActorNr];
1804                 Hashtable gameProperties = null;
1805                 Hashtable actorProps = null;
1806                 if (targetActorNr == 0)
1807                 {
1808                     gameProperties = (Hashtable)photonEvent[ParameterCode.Properties];
1809                 }
1810                 else
1811                 {
1812                     actorProps = (Hashtable)photonEvent[ParameterCode.Properties];
1813                 }
1814
1815                 this.ReadoutProperties(gameProperties, actorProps, targetActorNr);
1816                 break;
1817
1818             case PunEvent.RPC:
1819                 //ts: each event now contains a single RPC. execute this
1820                 // Debug.Log("Ev RPC from: " + originatingPlayer);
1821                 this.ExecuteRPC(photonEvent[ParameterCode.Data] as Hashtable, originatingPlayer);
1822                 break;
1823
1824             case PunEvent.SendSerialize:
1825             case PunEvent.SendSerializeReliable:
1826                 Hashtable serializeData = (Hashtable)photonEvent[ParameterCode.Data];
1827                 //Debug.Log(serializeData.ToStringFull());
1828
1829                 int remoteUpdateServerTimestamp = (int)serializeData[(byte)0];
1830                 short remoteLevelPrefix = -1;
1831                 short initialDataIndex = 1;
1832                 if (serializeData.ContainsKey((byte)1))
1833                 {
1834                     remoteLevelPrefix = (short)serializeData[(byte)1];
1835                     initialDataIndex = 2;
1836                 }
1837
1838                 for (short s = initialDataIndex; s < serializeData.Count; s++)
1839                 {
1840                     this.OnSerializeRead(serializeData[s] as Hashtable, originatingPlayer, remoteUpdateServerTimestamp, remoteLevelPrefix);
1841                 }
1842                 break;
1843
1844             case PunEvent.Instantiation:
1845                 this.DoInstantiate((Hashtable)photonEvent[ParameterCode.Data], originatingPlayer, null);
1846                 break;
1847
1848             case PunEvent.CloseConnection:
1849                 // MasterClient "requests" a disconnection from us
1850                 if (originatingPlayer == null || !originatingPlayer.isMasterClient)
1851                 {
1852                     Debug.LogError("Error: Someone else(" + originatingPlayer + ") then the masterserver requests a disconnect!");
1853                 }
1854                 else
1855                 {
1856                     PhotonNetwork.LeaveRoom();
1857                 }
1858
1859                 break;
1860
1861             case PunEvent.DestroyPlayer:
1862                 Hashtable evData = (Hashtable)photonEvent[ParameterCode.Data];
1863                 int targetPlayerId = (int)evData[(byte)0];
1864                 if (targetPlayerId >= 0)
1865                 {
1866                     this.DestroyPlayerObjects(targetPlayerId, true);
1867                 }
1868                 else
1869                 {
1870                     if (this.DebugOut >= DebugLevel.INFO) Debug.Log("Ev DestroyAll! By PlayerId: " + actorNr);
1871                     this.DestroyAll(true);
1872                 }
1873                 break;
1874
1875             case PunEvent.Destroy:
1876                 evData = (Hashtable)photonEvent[ParameterCode.Data];
1877                 int instantiationId = (int)evData[(byte)0];
1878                 // Debug.Log("Ev Destroy for viewId: " + instantiationId + " sent by owner: " + (instantiationId / PhotonNetwork.MAX_VIEW_IDS == actorNr) + " this client is owner: " + (instantiationId / PhotonNetwork.MAX_VIEW_IDS == this.mLocalActor.ID));
1879
1880
1881                 PhotonView pvToDestroy = null;
1882                 if (this.photonViewList.TryGetValue(instantiationId, out pvToDestroy))
1883                 {
1884                     this.RemoveInstantiatedGO(pvToDestroy.gameObject, true);
1885                 }
1886                 else
1887                 {
1888                     if (this.DebugOut >= DebugLevel.ERROR) Debug.LogError("Ev Destroy Failed. Could not find PhotonView with instantiationId " + instantiationId + ". Sent by actorNr: " + actorNr);
1889                 }
1890
1891                 break;
1892
1893             case PunEvent.AssignMaster:
1894                 evData = (Hashtable)photonEvent[ParameterCode.Data];
1895                 int newMaster = (int)evData[(byte)1];
1896                 this.SetMasterClient(newMaster, false);
1897                 break;
1898
1899             default:
1900                 if (photonEvent.Code < 200 && PhotonNetwork.OnEventCall != null)
1901                 {
1902                     object content = photonEvent[ParameterCode.Data];
1903                     PhotonNetwork.OnEventCall(photonEvent.Code, content, actorNr);
1904                 }
1905                 else
1906                 {
1907                     // actorNr might be null. it is fetched out of event on top of method
1908                     // Hashtable eventContent = (Hashtable) photonEvent[ParameterCode.Data];
1909                     // this.mListener.customEventAction(actorNr, eventCode, eventContent);
1910                     Debug.LogError("Error. Unhandled event: " + photonEvent);
1911                 }
1912                 break;
1913         }
1914
1915         this.externalListener.OnEvent(photonEvent);
1916     }
File name: NetworkingPeer.cs Copy
1962     public void ExecuteRPC(Hashtable rpcData, PhotonPlayer sender)
1963     {
1964         if (rpcData == null || !rpcData.ContainsKey((byte)0))
1965         {
1966             Debug.LogError("Malformed RPC; this should never occur. Content: " + SupportClass.DictionaryToString(rpcData));
1967             return;
1968         }
1969
1970         // ts: updated with "flat" event data
1971         int netViewID = (int)rpcData[(byte)0]; // LIMITS PHOTONVIEWS&PLAYERS
1972         int otherSidePrefix = 0; // by default, the prefix is 0 (and this is not being sent)
1973         if (rpcData.ContainsKey((byte)1))
1974         {
1975             otherSidePrefix = (short)rpcData[(byte)1];
1976         }
1977
1978         string inMethodName;
1979         if (rpcData.ContainsKey((byte)5))
1980         {
1981             int rpcIndex = (byte)rpcData[(byte)5]; // LIMITS RPC COUNT
1982             if (rpcIndex > PhotonNetwork.PhotonServerSettings.RpcList.Count - 1)
1983             {
1984                 Debug.LogError("Could not find RPC with index: " + rpcIndex + ". Going to ignore! Check PhotonServerSettings.RpcList");
1985                 return;
1986             }
1987             else
1988             {
1989                 inMethodName = PhotonNetwork.PhotonServerSettings.RpcList[rpcIndex];
1990             }
1991         }
1992         else
1993         {
1994             inMethodName = (string)rpcData[(byte)3];
1995         }
1996
1997         object[] inMethodParameters = null;
1998         if (rpcData.ContainsKey((byte)4))
1999         {
2000             inMethodParameters = (object[])rpcData[(byte)4];
2001         }
2002
2003         if (inMethodParameters == null)
2004         {
2005             inMethodParameters = new object[0];
2006         }
2007
2008         PhotonView photonNetview = this.GetPhotonView(netViewID);
2009         if (photonNetview == null)
2010         {
2011             int viewOwnerId = netViewID/PhotonNetwork.MAX_VIEW_IDS;
2012             bool owningPv = (viewOwnerId == this.mLocalActor.ID);
2013             bool ownerSent = (viewOwnerId == sender.ID);
2014
2015             if (owningPv)
2016             {
2017                 Debug.LogWarning("Received RPC \"" + inMethodName + "\" for viewID " + netViewID + " but this PhotonView does not exist! View was/is ours." + (ownerSent ? " Owner called." : " Remote called.") + " By: " + sender.ID);
2018             }
2019             else
2020             {
2021                 Debug.LogWarning("Received RPC \"" + inMethodName + "\" for viewID " + netViewID + " but this PhotonView does not exist! Was remote PV." + (ownerSent ? " Owner called." : " Remote called.") + " By: " + sender.ID + " Maybe GO was destroyed but RPC not cleaned up.");
2022             }
2023             return;
2024         }
2025
2026         if (photonNetview.prefix != otherSidePrefix)
2027         {
2028             Debug.LogError(
2029                 "Received RPC \"" + inMethodName + "\" on viewID " + netViewID + " with a prefix of " + otherSidePrefix
2030                 + ", our prefix is " + photonNetview.prefix + ". The RPC has been ignored.");
2031             return;
2032         }
2033
2034         // Get method name
2035         if (inMethodName == string.Empty)
2036         {
2037             Debug.LogError("Malformed RPC; this should never occur. Content: " + SupportClass.DictionaryToString(rpcData));
2038             return;
2039         }
2040
2041         if (PhotonNetwork.logLevel >= PhotonLogLevel.Full)
2042             Debug.Log("Received RPC: " + inMethodName);
2043
2044
2045         // SetReceiving filtering
2046         if (photonNetview.group != 0 && !allowedReceivingGroups.Contains(photonNetview.group))
2047         {
2048             return; // Ignore group
2049         }
2050
2051         Type[] argTypes = new Type[0];
2052         if (inMethodParameters.Length > 0)
2053         {
2054             argTypes = new Type[inMethodParameters.Length];
2055             int i = 0;
2056             for (int index = 0; index < inMethodParameters.Length; index++)
2057             {
2058                 object objX = inMethodParameters[index];
2059                 if (objX == null)
2060                 {
2061                     argTypes[i] = null;
2062                 }
2063                 else
2064                 {
2065                     argTypes[i] = objX.GetType();
2066                 }
2067
2068                 i++;
2069             }
2070         }
2071
2072         int receivers = 0;
2073         int foundMethods = 0;
2074         MonoBehaviour[] mbComponents = photonNetview.GetComponents(); // NOTE: we could possibly also cache MonoBehaviours per view?!
2075         for (int componentsIndex = 0; componentsIndex < mbComponents.Length; componentsIndex++)
2076         {
2077             MonoBehaviour monob = mbComponents[componentsIndex];
2078             if (monob == null)
2079             {
2080                 Debug.LogError("ERROR You have missing MonoBehaviours on your gameobjects!");
2081                 continue;
2082             }
2083
2084             Type type = monob.GetType();
2085
2086             // Get [RPC] methods from cache
2087             List cachedRPCMethods = null;
2088             if (this.monoRPCMethodsCache.ContainsKey(type))
2089             {
2090                 cachedRPCMethods = this.monoRPCMethodsCache[type];
2091             }
2092
2093             if (cachedRPCMethods == null)
2094             {
2095                 List entries = SupportClass.GetMethods(type, typeof(RPC));
2096
2097                 this.monoRPCMethodsCache[type] = entries;
2098                 cachedRPCMethods = entries;
2099             }
2100
2101             if (cachedRPCMethods == null)
2102             {
2103                 continue;
2104             }
2105
2106             // Check cache for valid methodname+arguments
2107             for (int index = 0; index < cachedRPCMethods.Count; index++)
2108             {
2109                 MethodInfo mInfo = cachedRPCMethods[index];
2110                 if (mInfo.Name == inMethodName)
2111                 {
2112                     foundMethods++;
2113                     ParameterInfo[] pArray = mInfo.GetParameters();
2114                     if (pArray.Length == argTypes.Length)
2115                     {
2116                         // Normal, PhotonNetworkMessage left out
2117                         if (this.CheckTypeMatch(pArray, argTypes))
2118                         {
2119                             receivers++;
2120                             object result = mInfo.Invoke((object)monob, inMethodParameters);
2121                             if (mInfo.ReturnType == typeof(IEnumerator))
2122                             {
2123                                 monob.StartCoroutine((IEnumerator)result);
2124                             }
2125                         }
2126                     }
2127                     else if ((pArray.Length - 1) == argTypes.Length)
2128                     {
2129                         // Check for PhotonNetworkMessage being the last
2130                         if (this.CheckTypeMatch(pArray, argTypes))
2131                         {
2132                             if (pArray[pArray.Length - 1].ParameterType == typeof(PhotonMessageInfo))
2133                             {
2134                                 receivers++;
2135
2136                                 int sendTime = (int)rpcData[(byte)2];
2137                                 object[] deParamsWithInfo = new object[inMethodParameters.Length + 1];
2138                                 inMethodParameters.CopyTo(deParamsWithInfo, 0);
2139                                 deParamsWithInfo[deParamsWithInfo.Length - 1] = new PhotonMessageInfo(sender, sendTime, photonNetview);
2140
2141                                 object result = mInfo.Invoke((object)monob, deParamsWithInfo);
2142                                 if (mInfo.ReturnType == typeof(IEnumerator))
2143                                 {
2144                                     monob.StartCoroutine((IEnumerator)result);
2145                                 }
2146                             }
2147                         }
2148                     }
2149                     else if (pArray.Length == 1 && pArray[0].ParameterType.IsArray)
2150                     {
2151                         receivers++;
2152                         object result = mInfo.Invoke((object)monob, new object[] { inMethodParameters });
2153                         if (mInfo.ReturnType == typeof(IEnumerator))
2154                         {
2155                             monob.StartCoroutine((IEnumerator)result);
2156                         }
2157                     }
2158                 }
2159             }
2160         }
2161
2162         // Error handling
2163         if (receivers != 1)
2164         {
2165             string argsString = string.Empty;
2166             for (int index = 0; index < argTypes.Length; index++)
2167             {
2168                 Type ty = argTypes[index];
2169                 if (argsString != string.Empty)
2170                 {
2171                     argsString += ", ";
2172                 }
2173
2174                 if (ty == null)
2175                 {
2176                     argsString += "null";
2177                 }
2178                 else
2179                 {
2180                     argsString += ty.Name;
2181                 }
2182             }
2183
2184             if (receivers == 0)
2185             {
2186                 if (foundMethods == 0)
2187                 {
2188                     Debug.LogError("PhotonView with ID " + netViewID + " has no method \"" + inMethodName + "\" marked with the [RPC](C#) or @RPC(JS) property! Args: " + argsString);
2189                 }
2190                 else
2191                 {
2192                     Debug.LogError("PhotonView with ID " + netViewID + " has no method \"" + inMethodName + "\" that takes " + argTypes.Length + " argument(s): " + argsString);
2193                 }
2194             }
2195             else
2196             {
2197                 Debug.LogError("PhotonView with ID " + netViewID + " has " + receivers + " methods \"" + inMethodName + "\" that takes " + argTypes.Length + " argument(s): " + argsString + ". Should be just one?");
2198             }
2199         }
2200     }
File name: NetworkingPeer.cs Copy
2835     internal void RPC(PhotonView view, string methodName, PhotonPlayer player, bool encrypt, params object[] parameters)
2836     {
2837         if (this.blockSendingGroups.Contains(view.group))
2838         {
2839             return; // Block sending on this group
2840         }
2841
2842         if (view.viewID < 1) //TODO: check why 0 should be illegal
2843         {
2844             Debug.LogError("Illegal view ID:" + view.viewID + " method: " + methodName + " GO:" + view.gameObject.name);
2845         }
2846
2847         if (PhotonNetwork.logLevel >= PhotonLogLevel.Full)
2848         {
2849             Debug.Log("Sending RPC \"" + methodName + "\" to player[" + player + "]");
2850         }
2851
2852
2853         //ts: changed RPCs to a one-level hashtable as described in internal.txt
2854         Hashtable rpcEvent = new Hashtable();
2855         rpcEvent[(byte)0] = (int)view.viewID; // LIMITS PHOTONVIEWS&PLAYERS
2856         if (view.prefix > 0)
2857         {
2858             rpcEvent[(byte)1] = (short)view.prefix;
2859         }
2860         rpcEvent[(byte)2] = this.ServerTimeInMilliSeconds;
2861
2862         // send name or shortcut (if available)
2863         int shortcut = 0;
2864         if (rpcShortcuts.TryGetValue(methodName, out shortcut))
2865         {
2866             rpcEvent[(byte)5] = (byte)shortcut; // LIMITS RPC COUNT
2867         }
2868         else
2869         {
2870             rpcEvent[(byte)3] = methodName;
2871         }
2872
2873         if (parameters != null && parameters.Length > 0)
2874         {
2875             rpcEvent[(byte) 4] = (object[]) parameters;
2876         }
2877
2878         if (this.mLocalActor == player)
2879         {
2880             this.ExecuteRPC(rpcEvent, player);
2881         }
2882         else
2883         {
2884             RaiseEventOptions options = new RaiseEventOptions() { TargetActors = new int[] { player.ID }, Encrypt = encrypt };
2885             this.OpRaiseEvent(PunEvent.RPC, rpcEvent, true, options);
2886         }
2887     }
File name: NetworkingPeer.cs Copy
2890     /// (byte)0 -> (int) ViewId (combined from actorNr and actor-unique-id)
2891     /// (byte)1 -> (short) prefix (level)
2895     /// (byte)5 -> (byte) method shortcut (alternative to name)
2899     internal void RPC(PhotonView view, string methodName, PhotonTargets target, bool encrypt, params object[] parameters)
2900     {
2901         if (this.blockSendingGroups.Contains(view.group))
2902         {
2903             return; // Block sending on this group
2904         }
2905
2906         if (view.viewID < 1)
2907         {
2908             Debug.LogError("Illegal view ID:" + view.viewID + " method: " + methodName + " GO:" + view.gameObject.name);
2909         }
2910
2911         if (PhotonNetwork.logLevel >= PhotonLogLevel.Full)
2912             Debug.Log("Sending RPC \"" + methodName + "\" to " + target);
2913
2914
2915         //ts: changed RPCs to a one-level hashtable as described in internal.txt
2916         Hashtable rpcEvent = new Hashtable();
2917         rpcEvent[(byte)0] = (int)view.viewID; // LIMITS NETWORKVIEWS&PLAYERS
2918         if (view.prefix > 0)
2919         {
2920             rpcEvent[(byte)1] = (short)view.prefix;
2921         }
2922         rpcEvent[(byte)2] = this.ServerTimeInMilliSeconds;
2923
2924
2925         // send name or shortcut (if available)
2926         int shortcut = 0;
2927         if (rpcShortcuts.TryGetValue(methodName, out shortcut))
2928         {
2929             rpcEvent[(byte)5] = (byte)shortcut; // LIMITS RPC COUNT
2930         }
2931         else
2932         {
2933             rpcEvent[(byte)3] = methodName;
2934         }
2935
2936         if (parameters != null && parameters.Length > 0)
2937         {
2938             rpcEvent[(byte)4] = (object[])parameters;
2939         }
2940
2941         // Check scoping
2942         if (target == PhotonTargets.All)
2943         {
2944             RaiseEventOptions options = new RaiseEventOptions() { InterestGroup = (byte)view.group, Encrypt = encrypt };
2945             this.OpRaiseEvent(PunEvent.RPC, rpcEvent, true, options);
2946
2947             // Execute local
2948             this.ExecuteRPC(rpcEvent, this.mLocalActor);
2949         }
2950         else if (target == PhotonTargets.Others)
2951         {
2952             RaiseEventOptions options = new RaiseEventOptions() { InterestGroup = (byte)view.group, Encrypt = encrypt };
2953             this.OpRaiseEvent(PunEvent.RPC, rpcEvent, true, options);
2954         }
2955         else if (target == PhotonTargets.AllBuffered)
2956         {
2957             RaiseEventOptions options = new RaiseEventOptions() { CachingOption = EventCaching.AddToRoomCache, Encrypt = encrypt };
2958             this.OpRaiseEvent(PunEvent.RPC, rpcEvent, true, options);
2959
2960             // Execute local
2961             this.ExecuteRPC(rpcEvent, this.mLocalActor);
2962         }
2963         else if (target == PhotonTargets.OthersBuffered)
2964         {
2965             RaiseEventOptions options = new RaiseEventOptions() { CachingOption = EventCaching.AddToRoomCache, Encrypt = encrypt };
2966             this.OpRaiseEvent(PunEvent.RPC, rpcEvent, true, options);
2967         }
2968         else if (target == PhotonTargets.MasterClient)
2969         {
2970             if (this.mMasterClient == this.mLocalActor)
2971             {
2972                 this.ExecuteRPC(rpcEvent, this.mLocalActor);
2973             }
2974             else
2975             {
2976                 RaiseEventOptions options = new RaiseEventOptions() { Receivers = ReceiverGroup.MasterClient, Encrypt = encrypt };
2977                 this.OpRaiseEvent(PunEvent.RPC, rpcEvent, true, options);
2978             }
2979         }
2980         else if (target == PhotonTargets.AllViaServer)
2981         {
2982             RaiseEventOptions options = new RaiseEventOptions() { InterestGroup = (byte)view.group, Receivers = ReceiverGroup.All, Encrypt = encrypt };
2983             this.OpRaiseEvent(PunEvent.RPC, rpcEvent, true, options);
2984             if (PhotonNetwork.offlineMode)
2985             {
2986                 this.ExecuteRPC(rpcEvent, this.mLocalActor);
2987             }
2988         }
2989         else if (target == PhotonTargets.AllBufferedViaServer)
2990         {
2991             RaiseEventOptions options = new RaiseEventOptions() { InterestGroup = (byte)view.group, Receivers = ReceiverGroup.All, CachingOption = EventCaching.AddToRoomCache, Encrypt = encrypt };
2992             this.OpRaiseEvent(PunEvent.RPC, rpcEvent, true, options);
2993             if (PhotonNetwork.offlineMode)
2994             {
2995                 this.ExecuteRPC(rpcEvent, this.mLocalActor);
2996             }
2997         }
2998         else
2999         {
3000             Debug.LogError("Unsupported target enum: " + target);
3001         }
3002     }

ExecuteRPC 96 lượt xem

Gõ tìm kiếm nhanh...