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:

     void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
     {
         // Always send transform (depending on reliability of the network view)
         if (stream.isWriting)
         {
             Vector3 pos = transform.localPosition;
             Quaternion rot = transform.localRotation;
             stream.Serialize(ref pos);
             stream.Serialize(ref rot);
         }
         // When receiving, buffer the information
         else
         {
    // Receive latest state information
             Vector3 pos = Vector3.zero;
             Quaternion rot = Quaternion.identity;
             stream.Serialize(ref pos);
             stream.Serialize(ref rot);

             // Shift buffer contents, oldest data erased, 18 becomes 19, ... , 0 becomes 1
             for (int i = m_BufferedState.Length - 1; i >= 1; i--)
             {
                 m_BufferedState[i] = m_BufferedState[i - 1];
             }


             // Save currect received state as 0 in the buffer, safe to overwrite after shifting
             State state;
             state.timestamp = info.timestamp;
             state.pos = pos;
             state.rot = rot;
             m_BufferedState[0] = state;

             // Increment state count but never exceed buffer size
             m_TimestampCount = Mathf.Min(m_TimestampCount + 1, m_BufferedState.Length);

             // Check integrity, lowest numbered state in the buffer is newest and so on
             for (int i = 0; i < m_TimestampCount - 1; i++)
             {
                 if (m_BufferedState[i].timestamp < m_BufferedState[i + 1].timestamp)
                     Debug.Log("State inconsistent");
             }
   }
     }