EventCaching









How do I use Event Caching
Below are practical examples compiled from projects for learning and reference purposes

Featured Snippets


File name: LoadbalancingPeer.cs Copy
463         public virtual bool OpRaiseEvent(byte eventCode, object customEventContent, bool sendReliable, RaiseEventOptions raiseEventOptions)
464         {
465             opParameters.Clear(); // re-used private variable to avoid many new Dictionary() calls (garbage collection)
466             opParameters[(byte)LiteOpKey.Code] = (byte)eventCode;
467             if (customEventContent != null)
468             {
469                 opParameters[(byte) LiteOpKey.Data] = customEventContent;
470             }
471
472             if (raiseEventOptions == null)
473             {
474                 raiseEventOptions = RaiseEventOptions.Default;
475             }
476             else
477             {
478                 if (raiseEventOptions.CachingOption != EventCaching.DoNotCache)
479                 {
480                     opParameters[(byte) LiteOpKey.Cache] = (byte) raiseEventOptions.CachingOption;
481                 }
482                 if (raiseEventOptions.Receivers != ReceiverGroup.Others)
483                 {
484                     opParameters[(byte) LiteOpKey.ReceiverGroup] = (byte) raiseEventOptions.Receivers;
485                 }
486                 if (raiseEventOptions.InterestGroup != 0)
487                 {
488                     opParameters[(byte) LiteOpKey.Group] = (byte) raiseEventOptions.InterestGroup;
489                 }
490                 if (raiseEventOptions.TargetActors != null)
491                 {
492                     opParameters[(byte) LiteOpKey.ActorList] = raiseEventOptions.TargetActors;
493                 }
494                 if (raiseEventOptions.ForwardToWebhook)
495                 {
496                     opParameters[(byte) ParameterCode.EventForward] = true; //TURNBASED
497                 }
498             }
499
500             return this.OpCustom((byte)LiteOpCode.RaiseEvent, opParameters, sendReliable, raiseEventOptions.SequenceChannel, raiseEventOptions.Encrypt);
501         }
File name: NetworkingPeer.cs Copy
2228     internal Hashtable SendInstantiate(string prefabName, Vector3 position, Quaternion rotation, int group, int[] viewIDs, object[] data, bool isGlobalObject)
2229     {
2230         // first viewID is now also the gameobject's instantiateId
2231         int instantiateId = viewIDs[0]; // LIMITS PHOTONVIEWS&PLAYERS
2232
2233         //TODO: reduce hashtable key usage by using a parameter array for the various values
2234         Hashtable instantiateEvent = new Hashtable(); // This players info is sent via ActorID
2235         instantiateEvent[(byte)0] = prefabName;
2236
2237         if (position != Vector3.zero)
2238         {
2239             instantiateEvent[(byte)1] = position;
2240         }
2241
2242         if (rotation != Quaternion.identity)
2243         {
2244             instantiateEvent[(byte)2] = rotation;
2245         }
2246
2247         if (group != 0)
2248         {
2249             instantiateEvent[(byte)3] = group;
2250         }
2251
2252         // send the list of viewIDs only if there are more than one. else the instantiateId is the viewID
2253         if (viewIDs.Length > 1)
2254         {
2255             instantiateEvent[(byte)4] = viewIDs; // LIMITS PHOTONVIEWS&PLAYERS
2256         }
2257
2258         if (data != null)
2259         {
2260             instantiateEvent[(byte)5] = data;
2261         }
2262
2263         if (this.currentLevelPrefix > 0)
2264         {
2265             instantiateEvent[(byte)8] = this.currentLevelPrefix; // photonview's / object's level prefix
2266         }
2267
2268         instantiateEvent[(byte)6] = this.ServerTimeInMilliSeconds;
2269         instantiateEvent[(byte)7] = instantiateId;
2270
2271
2272         RaiseEventOptions options = new RaiseEventOptions();
2273         options.CachingOption = (isGlobalObject) ? EventCaching.AddToRoomCacheGlobal : EventCaching.AddToRoomCache;
2274
2275         this.OpRaiseEvent(PunEvent.Instantiation, instantiateEvent, true, options);
2276         return instantiateEvent;
2277     }
File name: NetworkingPeer.cs Copy
2592     private void ServerCleanInstantiateAndDestroy(int instantiateId, int creatorId, bool isRuntimeInstantiated)
2593     {
2594         Hashtable removeFilter = new Hashtable();
2595         removeFilter[(byte)7] = instantiateId;
2596
2597         RaiseEventOptions options = new RaiseEventOptions() { CachingOption = EventCaching.RemoveFromRoomCache, TargetActors = new int[] { creatorId } };
2598         this.OpRaiseEvent(PunEvent.Instantiation, removeFilter, true, options);
2599         //this.OpRaiseEvent(PunEvent.Instantiation, removeFilter, true, 0, new int[] { actorNr }, EventCaching.RemoveFromRoomCache);
2600
2601         Hashtable evData = new Hashtable();
2602         evData[(byte)0] = instantiateId;
2603         options = null;
2604         if (!isRuntimeInstantiated)
2605         {
2606             // if the view got loaded with the scene, the EvDestroy must be cached (there is no Instantiate-msg which we can remove)
2607             // reason: joining players will load the obj and have to destroy it (too)
2608             options = new RaiseEventOptions();
2609             options.CachingOption = EventCaching.AddToRoomCacheGlobal;
2610             Debug.Log("Destroying GO as global. ID: " + instantiateId);
2611         }
2612         this.OpRaiseEvent(PunEvent.Destroy, evData, true, options);
2613     }
File name: NetworkingPeer.cs Copy
2634     private void OpRemoveFromServerInstantiationsOfPlayer(int actorNr)
2635     {
2636         // removes all "Instantiation" events of player actorNr. this is not an event for anyone else
2637         RaiseEventOptions options = new RaiseEventOptions() { CachingOption = EventCaching.RemoveFromRoomCache, TargetActors = new int[] { actorNr } };
2638         this.OpRaiseEvent(PunEvent.Instantiation, null, true, options);
2639         //this.OpRaiseEvent(PunEvent.Instantiation, null, true, 0, new int[] { actorNr }, EventCaching.RemoveFromRoomCache);
2640     }
File name: NetworkingPeer.cs Copy
2754     public void OpCleanRpcBuffer(int actorNumber)
2755     {
2756         RaiseEventOptions options = new RaiseEventOptions() { CachingOption = EventCaching.RemoveFromRoomCache, TargetActors = new int[] { actorNumber } };
2757         this.OpRaiseEvent(PunEvent.RPC, null, true, options);
2758         //this.OpRaiseEvent(PunEvent.RPC, null, true, 0, new int[] { actorNumber }, EventCaching.RemoveFromRoomCache);
2759     }
File name: NetworkingPeer.cs Copy
2765     public void OpRemoveCompleteCacheOfPlayer(int actorNumber)
2766     {
2767         RaiseEventOptions options = new RaiseEventOptions() { CachingOption = EventCaching.RemoveFromRoomCache, TargetActors = new int[] { actorNumber } };
2768         this.OpRaiseEvent(0, null, true, options);
2769         //this.OpRaiseEvent(0, null, true, 0, new int[] { actorNumber }, EventCaching.RemoveFromRoomCache);
2770     }
File name: NetworkingPeer.cs Copy
2773     public void OpRemoveCompleteCache()
2774     {
2775         RaiseEventOptions options = new RaiseEventOptions() { CachingOption = EventCaching.RemoveFromRoomCache, Receivers = ReceiverGroup.MasterClient };
2776         this.OpRaiseEvent(0, null, true, options);
2777         //this.OpRaiseEvent(0, null, true, 0, EventCaching.RemoveFromRoomCache, ReceiverGroup.MasterClient); // TODO: check who gets this event?
2778     }
File name: NetworkingPeer.cs Copy
2780     /// This clears the cache of any player/actor who's no longer in the room (making it a simple clean-up option for a new master)
2781     private void RemoveCacheOfLeftPlayers()
2782     {
2783         Dictionary opParameters = new Dictionary();
2784         opParameters[ParameterCode.Code] = (byte)0; // any event
2785         opParameters[ParameterCode.Cache] = (byte)EventCaching.RemoveFromRoomCacheForActorsLeft; // option to clear the room cache of all events of players who left
2786
2787         this.OpCustom((byte)OperationCode.RaiseEvent, opParameters, true, 0);
2788     }
File name: NetworkingPeer.cs Copy
2803     public void OpCleanRpcBuffer(PhotonView view)
2804     {
2805         Hashtable rpcFilterByViewId = new Hashtable();
2806         rpcFilterByViewId[(byte)0] = view.viewID;
2807
2808         RaiseEventOptions options = new RaiseEventOptions() { CachingOption = EventCaching.RemoveFromRoomCache };
2809         this.OpRaiseEvent(PunEvent.RPC, rpcFilterByViewId, true, options);
2810         //this.OpRaiseEvent(PunEvent.RPC, rpcFilterByViewId, true, 0, EventCaching.RemoveFromRoomCache, ReceiverGroup.Others);
2811     }
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     }

EventCaching 113 lượt xem

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