Search Unity

Why it always show me error [NetTankCtlScript.js]

Discussion in 'Multiplayer' started by northman, Mar 4, 2009.

  1. northman

    northman

    Joined:
    Feb 28, 2008
    Posts:
    144
    Code (csharp):
    1.  
    2. //int currentHealth;
    3. var m_InterpolationBackTime: double = 0.1;
    4. var m_ExtrapolationLimit: double = 0.5;
    5. var TankCtl_Tower: Transform;
    6. var TankCtl_Cannon: Transform;
    7. class TankInfo {
    8.     var timestamp: double;
    9.     var TankBodyPos: Vector3;
    10.     var TankBodyRot: Quaternion;
    11.     var TankTowerPos: Vector3;
    12.     var TankTowerRot: Quaternion;
    13.     var TankCannonPos: Vector3;
    14.     var TankCannonRot: Quaternion;
    15.     var TankSpeed: float;
    16.     var TankWayLR: int;
    17.     var TankWayFB: int;
    18.     var TankOnFire: int;
    19.     var TankOnMachineGun: int;
    20. }
    21.  
    22. private var bufferTankInfo: TankInfo[] = new TankInfo[20];
    23. private var m_TimestampCount: int;
    24.  
    25. //private var iWayFB: int; //the way of tank go to.
    26. //private var iWayLR: int;
    27. //private var iSpeed: float;
    28.  
    29.  
    30. private var bodyP: Vector3 = Vector3.zero;
    31. private var bodyR: Quaternion = Quaternion.identity;
    32. private var towerP: Vector3 = Vector3.zero;
    33. private var towerR: Quaternion = Quaternion.identity;
    34. private var cannoP: Vector3 = Vector3.zero;
    35. private var cannoR: Quaternion = Quaternion.identity;
    36. private var fltSpeed: float = 0.0;
    37. private var w_FB : int = 0;
    38. private var w_LR : int = 0;
    39.  
    40. function Start() {
    41. //  for (var i: int = 0; i<20; i++) {
    42. //      TankInfo[i].timestamp = 0;
    43. //  }
    44. }
    45. //function TankSpeed(i: float) {
    46. //  iSpeed=i;
    47. //}
    48.  
    49. //function TankWayFB(i: int) {
    50. //  iWayFB = i;
    51. //}
    52. //function TankWayLR(i: int) {
    53. //  iWayLR = i;
    54. //}
    55.  
    56.  
    57.  
    58.    
    59. function OnSerializeNetworkView(stream : BitStream, info : NetworkMessageInfo)
    60. {
    61.     if (stream.isWriting)
    62.     {
    63. //      int health = currentHealth;
    64. //      stream.Serialize(health);
    65.         bodyP = transform.position;
    66.         bodyR = transform.rotation;
    67.         towerP = TankCtl_Tower.position;
    68.         towerR = TankCtl_Tower.rotation;
    69.         cannoP = TankCtl_Cannon.position;
    70.         cannoR = TankCtl_Cannon.rotation;
    71.         fltSpeed = TankTransCtl_BodyScript.GoSpeed;
    72.         w_FB = TankTransCtl_BodyScript.iWayFB;
    73.         w_LR = TankTransCtl_BodyScript.iWayLR;
    74.         //Cannon
    75.         //MachineGun
    76.         stream.Serialize(bodyP);
    77.         stream.Serialize(bodyR);
    78.         stream.Serialize(towerP);
    79.         stream.Serialize(towerR);
    80.         stream.Serialize(cannoP);
    81.         stream.Serialize(cannoR);
    82.         stream.Serialize(fltSpeed);
    83.         stream.Serialize(w_FB);
    84.         stream.Serialize(w_LR);
    85.        
    86.     }
    87.     else
    88.     {
    89.         bodyP = Vector3.zero;
    90.         bodyR = Quaternion.identity;
    91.         towerP = Vector3.zero;
    92.         towerR = Quaternion.identity;
    93.         cannoP = Vector3.zero;
    94.         cannoR = Quaternion.identity;
    95.         fltSpeed = 0.0;
    96.         w_FB = 0;
    97.         w_LR = 0;
    98.         //Cannon
    99.         //MachineGun
    100.         stream.Serialize(bodyP);
    101.         stream.Serialize(bodyR);
    102.         stream.Serialize(towerP);
    103.         stream.Serialize(towerR);
    104.         stream.Serialize(cannoP);
    105.         stream.Serialize(cannoR);
    106.         stream.Serialize(fltSpeed);
    107.         stream.Serialize(w_FB);
    108.         stream.Serialize(w_LR);
    109.  
    110.         for (var i=bufferTankInfo.Length-1;i>=1;i--)
    111.         {
    112.             bufferTankInfo[i] = bufferTankInfo[i-1];
    113.         }
    114.  
    115.         var t_Info: TankInfo;
    116.         print("info.timestamp=" + info.timestamp);
    117.         t_Info.timestamp = info.timestamp;
    118.         t_Info.TankBodyPos = bodyP;
    119.         t_Info.TankBodyRot = bodyR;
    120.         t_Info.TankTowerPos = towerP;
    121.         t_Info.TankTowerRot = towerR;
    122.         t_Info.TankCannonPos = cannoP;
    123.         t_Info.TankCannonRot = cannoR;
    124.         t_Info.TankSpeed = fltSpeed;
    125.         t_Info.TankWayFB = w_FB;
    126.         t_Info.TankWayLR = w_LR;
    127.         bufferTankInfo[0] = t_Info;
    128.        
    129.         m_TimestampCount = Mathf.Min(m_TimestampCount + 1, bufferTankInfo.Length);
    130.  
    131.         // Check if states are in order, if it is inconsistent you could reshuffel or
    132.         // drop the out-of-order state. Nothing is done here
    133.         for (var i1=0;i1<m_TimestampCount-1;i1++)
    134.         {
    135.             if (bufferTankInfo[i1].timestamp < bufferTankInfo[i1+1].timestamp){
    136.                 Debug.Log("State inconsistent");
    137.             }
    138.         }  
    139.        
    140.     }
    141. }
    142.  
    143. function Update() {
    144.     // This is the target playback time of the rigid body
    145.     var interpolationTime = Network.time - m_InterpolationBackTime;
    146.        
    147.     // Use interpolation if the target playback time is present in the buffer
    148.     print("NetTankCtlScript.js, Update:" + bufferTankInfo.length +"++++++++++++++++++++++++++++++++");
    149.     var tmpInfo: TankInfo;
    150.     tmpInfo = bufferTankInfo[0];
    151.     //if (bufferTankInfo[0].timestamp > interpolationTime) {
    152.     if(tmpInfo.timestamp > interpolationTime) {
    153.         // Go through buffer and find correct state to play back
    154.         for (var i2=0;i2<m_TimestampCount;i2++) {
    155.             if (bufferTankInfo[i2].timestamp <= interpolationTime || i2 == m_TimestampCount-1) {
    156.                 // The state one slot newer (<100ms) than the best playback state
    157.                 var rhs: TankInfo = bufferTankInfo[Mathf.Max(i2-1, 0)];
    158.                 // The best playback state (closest to 100 ms old (default time))
    159.                 var lhs: TankInfo = bufferTankInfo[i2];
    160.                    
    161.                 // Use the time between the two slots to determine if interpolation is necessary
    162.                 var length = rhs.timestamp - lhs.timestamp;
    163.                 var t: float = 0.0;
    164.                 // As the time difference gets closer to 100 ms t gets closer to 1 in
    165.                 // which case rhs is only used
    166.                 // Example:
    167.                 // Time is 10.000, so sampleTime is 9.900
    168.                 // lhs.time is 9.910 rhs.time is 9.980 length is 0.070
    169.                 // t is 9.900 - 9.910 / 0.070 = 0.14. So it uses 14% of rhs, 86% of lhs
    170.                 if (length > 0.0001)
    171.                     t = (interpolationTime - lhs.timestamp) / length * 1.0;
    172.                    
    173.                 // if t=0 => lhs is used directly
    174.                 transform.localPosition = Vector3.Lerp(lhs.TankBodyPos, rhs.TankBodyPos, t);
    175.                 transform.localRotation = Quaternion.Slerp(lhs.TankBodyRot, rhs.TankBodyRot, t);
    176.                 return;
    177.             }
    178.         }
    179.     }
    180.         // Use extrapolation
    181.     else
    182.     {
    183.         var latest: TankInfo = bufferTankInfo[0];
    184.            
    185.         var extrapolationLength: float = (interpolationTime - latest.timestamp) * 1.0;
    186.         // Don't extrapolation for more than 500 ms, you would need to do that carefully
    187.         if (extrapolationLength < m_ExtrapolationLimit){
    188.             TankTransCtl_BodyScript.GoSpeed = latest.TankSpeed;
    189.             TankTransCtl_BodyScript.iWayFB = latest.TankWayFB;
    190.             TankTransCtl_BodyScript.iWayLR = latest.TankWayLR;
    191.             fltSpeed = latest.TankSpeed;
    192.             w_FB = latest.TankWayFB;
    193.             w_LR = latest.TankWayLR;
    194.             BroadcastMessage ("TankWayFB", w_FB);
    195.             BroadcastMessage ("TankWayLR", w_LR);
    196.             BroadcastMessage ("TankSpeed", fltSpeed);
    197.  
    198.         }
    199.     }
    200. }


    Hello, anybody.
    I wrote the NetTankCtlScript.js to control my tank over network. when I was using networkview to communication, the error message was occure.

    Please help me.

    Shawn. :cry:
     
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    thats because you never ever initialize that array that offers you tmpInfo or fill it.

    Looks very much like you ripped the interpolation out of the network example but forget 2/3 of its working body when doing so (initialization and updating of the interpolation data)
     
  3. northman

    northman

    Joined:
    Feb 28, 2008
    Posts:
    144
    hello, thank you for answer my question, but I try to init array, the same error was occure.

    in function Start()



    I don't know what error. the timestamp was double. but why error?
    :cry: :cry:
     
  4. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    well you don't init the array anywhere there as well.
    you try to fill the cells with content but you can not fill something you have not created first, and especially no array which has no object in the cells

    so what you first need to do is create the array actually and before accessing any of the fields, create a new object instance in the array slot.

    that will do it
     
  5. northman

    northman

    Joined:
    Feb 28, 2008
    Posts:
    144
    :(
    I'm sorry, I don't know how to define a double var ?

    var xxx : double; // is right?

    how to set value to type of double?

    xxx = 0; //is right?


    Thanks.
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    var xxx : System.Double;

    --Eric