Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

[Solved] Unity car tutorial skid marks variable scope magic or something specific to UnityScript

Discussion in 'Scripting' started by DeaD4MaN, Jan 16, 2015.

  1. DeaD4MaN

    DeaD4MaN

    Joined:
    Jun 25, 2014
    Posts:
    7
    Moved this thread/question from the Answers, because a moderator suggested to do so.

    I have decided to look how does the tutorial handles procedural mesh generation for the skid marks and I simply cannot understand why the AddSkidMark() works. Not 'how', but 'why'.

    In Skidmarks.js there is a method:
    Code (JavaScript):
    1. function AddSkidMark(pos : Vector3, normal : Vector3, intensity : float, lastIndex : int)
    2. {
    3.      if(intensity > 1)
    4.          intensity = 1.0;
    5.      if(intensity < 0)
    6.          return -1;
    7.      var curr : markSection = skidmarks[numMarks % maxMarks];
    8.      curr.pos = pos + normal * groundOffset;
    9.      curr.normal = normal;
    10.      curr.intensity = intensity;
    11.      curr.lastIndex = lastIndex;
    12.      if(lastIndex != -1)
    13.      {
    14.          var last : markSection = skidmarks[lastIndex % maxMarks];
    15.          var dir : Vector3 = (curr.pos - last.pos);
    16.          var xDir : Vector3 = Vector3.Cross(dir,normal).normalized;
    17.        
    18.          curr.posl = curr.pos + xDir * markWidth * 0.5;
    19.          curr.posr = curr.pos - xDir * markWidth * 0.5;
    20.          curr.tangent = new Vector4(xDir.x, xDir.y, xDir.z, 1);
    21.        
    22.          if(last.lastIndex == -1)
    23.          {
    24.              last.tangent = curr.tangent;
    25.              last.posl = curr.pos + xDir * markWidth * 0.5;
    26.              last.posr = curr.pos - xDir * markWidth * 0.5;
    27.          }
    28.      }
    29.    
    30.      numMarks++;
    31.      updated = true;
    32.      return numMarks -1;
    33. }
    As I understand 'markSection' class instances 'curr' and 'last' are local and both of them get, but not set values from skidmarks[index]. Then the method calculates some values and does literally nothing with them, since it returns an only integer. Additionally 'numMarks' and 'updated' are the only global variables (with a private modifier) from this class that do actually change. And here's the magic: if I comment out most of the method, leaving only 'updated' and 'numMakrs' related lines - the skid marks generation brakes and 'skidmarks[]' stops being populated/changed. I would understand the logic, if before returning a value, something along the lines of:

    Code (JavaScript):
    1. skidmarks[numMarks % maxMarks] = curr;
    2. skidmarks[lastIndex % maxMarks] = last;
    would be written, but that's not the case.

    What am I missing or not seeing?
     
  2. DeaD4MaN

    DeaD4MaN

    Joined:
    Jun 25, 2014
    Posts:
    7
    Moderators, You can close the thread.

    For anyone interested: I have forgot how Objects work. When using statements like
    Code (CSharp):
    1. MyClass myClassA = new MyClass();
    2. MyClass myClassB = myClassA;
    myClassA class instance looses it reference to myClassA and the new reference points to myClassB. So both instances of myClassA and myClassB reference myClassB. And the reference to myClassA is collected by GC.
     
  3. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483

    myClassB references myClassA, not the other way around. It will be collected by the GC if these variables go out of scope(maybe you created them within a function?).