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:

     // returns viewID (combined owner and sub id)
     private static int AllocateViewID(int ownerId)
     {
         if (ownerId == 0)
         {
             // we look up a fresh subId for the owner "room" (mind the "sub" in subId)
             int newSubId = lastUsedViewSubIdStatic;
             int newViewId;
             int ownerIdOffset = ownerId * MAX_VIEW_IDS;
             for (int i = 1; i < MAX_VIEW_IDS; i++)
             {
                 newSubId = (newSubId + 1) % MAX_VIEW_IDS;
                 if (newSubId == 0)
                 {
                     continue; // avoid using subID 0
                 }

                 newViewId = newSubId + ownerIdOffset;
                 if (!networkingPeer.photonViewList.ContainsKey(newViewId))
                 {
                     lastUsedViewSubIdStatic = newSubId;
                     return newViewId;
                 }
             }

             // this is the error case: we didn't find any (!) free subId for this user
             throw new Exception(string.Format("AllocateViewID() failed. Room (user {0}) is out of 'scene' viewIDs. It seems all available are in use.", ownerId));
         }
         else
         {
             // we look up a fresh SUBid for the owner
             int newSubId = lastUsedViewSubId;
             int newViewId;
             int ownerIdOffset = ownerId * MAX_VIEW_IDS;
             for (int i = 1; i < MAX_VIEW_IDS; i++)
             {
                 newSubId = (newSubId + 1) % MAX_VIEW_IDS;
                 if (newSubId == 0)
                 {
                     continue; // avoid using subID 0
                 }

                 newViewId = newSubId + ownerIdOffset;
                 if (!networkingPeer.photonViewList.ContainsKey(newViewId) && !manuallyAllocatedViewIds.Contains(newViewId))
                 {
                     lastUsedViewSubId = newSubId;
                     return newViewId;
                 }
             }

             throw new Exception(string.Format("AllocateViewID() failed. User {0} is out of subIds, as all viewIDs are used.", ownerId));
         }
     }