Copying and Pasting cs Code

In cs, like in almost any computer programming language, reading data from a file can be tricky. You add extra lines of code to tell the computer what to do. Sometimes you can copy and paste these lines from other peoples’ code.

For example, you can follow the pattern in this listing:

     internal GameObject DoInstantiate(Hashtable evData, PhotonPlayer photonPlayer, GameObject resourceGameObject)
     {
         // some values always present:
         string prefabName = (string)evData[(byte)0];
         int serverTime = (int)evData[(byte)6];
         int instantiationId = (int)evData[(byte)7];

         Vector3 position;
         if (evData.ContainsKey((byte)1))
         {
             position = (Vector3)evData[(byte)1];
         }
         else
         {
             position = Vector3.zero;
         }

         Quaternion rotation = Quaternion.identity;
         if (evData.ContainsKey((byte)2))
         {
             rotation = (Quaternion)evData[(byte)2];
         }

         int group = 0;
         if (evData.ContainsKey((byte)3))
         {
             group = (int)evData[(byte)3];
         }

         short objLevelPrefix = 0;
         if (evData.ContainsKey((byte)8))
         {
             objLevelPrefix = (short)evData[(byte)8];
         }

         int[] viewsIDs;
         if (evData.ContainsKey((byte)4))
         {
             viewsIDs = (int[])evData[(byte)4];
         }
         else
         {
             viewsIDs = new int[1] { instantiationId };
         }

         object[] incomingInstantiationData;
         if (evData.ContainsKey((byte)5))
         {
             incomingInstantiationData = (object[])evData[(byte)5];
         }
         else
         {
             incomingInstantiationData = null;
         }

         // SetReceiving filtering
         if (group != 0 && !this.allowedReceivingGroups.Contains(group))
         {
             return null; // Ignore group
         }

         // load prefab, if it wasn't loaded before (calling methods might do this)
         if (resourceGameObject == null)
         {
             if (!NetworkingPeer.UsePrefabCache || !NetworkingPeer.PrefabCache.TryGetValue(prefabName, out resourceGameObject))
             {
                 resourceGameObject = (GameObject)Resources.Load(prefabName, typeof(GameObject));
                 if (NetworkingPeer.UsePrefabCache)
                 {
                     NetworkingPeer.PrefabCache.Add(prefabName, resourceGameObject);
                 }
             }

             if (resourceGameObject == null)
             {
                 Debug.LogError("PhotonNetwork error: Could not Instantiate the prefab [" + prefabName + "]. Please verify you have this gameobject in a Resources folder.");
                 return null;
             }
         }

         // now modify the loaded "blueprint" object before it becomes a part of the scene (by instantiating it)
         PhotonView[] resourcePVs = resourceGameObject.GetPhotonViewsInChildren();
         if (resourcePVs.Length != viewsIDs.Length)
         {
             throw new Exception("Error in Instantiation! The resource's PhotonView count is not the same as in incoming data.");
         }

         for (int i = 0; i < viewsIDs.Length; i++)
         {
             // NOTE instantiating the loaded resource will keep the viewID but would not copy instantiation data, so it's set below
             // so we only set the viewID and instantiationId now. the instantiationData can be fetched
             resourcePVs[i].viewID = viewsIDs[i];
             resourcePVs[i].prefix = objLevelPrefix;
             resourcePVs[i].instantiationId = instantiationId;
             resourcePVs[i].isRuntimeInstantiated = true;
         }

         this.StoreInstantiationData(instantiationId, incomingInstantiationData);

         // load the resource and set it's values before instantiating it:
         GameObject go = (GameObject)GameObject.Instantiate(resourceGameObject, position, rotation);

         for (int i = 0; i < viewsIDs.Length; i++)
         {
             // NOTE instantiating the loaded resource will keep the viewID but would not copy instantiation data, so it's set below
             // so we only set the viewID and instantiationId now. the instantiationData can be fetched
             resourcePVs[i].viewID = 0;
             resourcePVs[i].prefix = -1;
             resourcePVs[i].prefixBackup = -1;
             resourcePVs[i].instantiationId = -1;
             resourcePVs[i].isRuntimeInstantiated = false;
         }

         this.RemoveInstantiationData(instantiationId);

         // Send OnPhotonInstantiate callback to newly created GO.
         // GO will be enabled when instantiated from Prefab and it does not matter if the script is enabled or disabled.
         go.SendMessage(PhotonNetworkingMessage.OnPhotonInstantiate.ToString(), new PhotonMessageInfo(photonPlayer, serverTime, null), SendMessageOptions.DontRequireReceiver);
         return go;
     }