Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Use for frequently empty parentheses...

Discussion in 'Scripting' started by Jessy, Jul 5, 2007.

  1. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    Hi!

    I'm trying to make some code, that was very nicely donated by Eric5h5 in my last topic, my own. In doing this, I, being a newcomer, am wondering if the frequently empty parentheses following "function Update" could be of any use to me. What are those for? I'm finding that kind of thing all over the manual.

    Thanks!
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Conveniently enough, that question has been asked before so hopefully that topic answers the question. I'm not sure if you'd ever use the parentheses for Update in particular since that's kind of a special case, but it's still a function so it has the same syntax.

    --Eric
     
  3. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    Ok, I've gotten a lot of good information out of some books on JavaScript from the library, which lead to a better understanding of parameters, the stuff that goes in those parentheses. However, what I came across was not quite enough to fully explain this excerpt from the Unity scripting reference:

    "Usage
    Code (csharp):
    1. bool Raycast (Vector3 origin, Vector3 direction, out RaycastHit hitInfo, float distance = Mathf.Infinity, int layerMask = kDefaultRaycastLayers)
    Description
    Casts a ray against all colliders in the scene.

    The ray starts at origin and is directed along direction. The length of the ray is distance. Returns true if the ray hits any collider. If true is returned, hitInfo will contain more information about where the collider was hit (See Also: RaycastHit).

    Code (csharp):
    1. function Update () {
    2.     var hit : RaycastHit;
    3.     if (Physics.Raycast (transform.position, Vector3.down, hit)) {
    4.         distanceToGround = hit.distance;
    5.     }
    6. }
    7. // Raycast up to 100 meters forward
    8. function Update () {
    9.     var hit : RaycastHit;
    10.     if (Physics.Raycast (transform.position, Vector3.down, hit, 100.0)) {
    11.         distanceToGround = hit.distance;
    12.     }
    13. }
    "

    I don't get what the word "out" is all about here. Any help?

    Another term I saw in the scripting reference is "Enumerations". I learned the meaning of several other terms, like Classes, Objects, Methods, and Arrays, but not Enumerations. I looked it up on the boards, and what NCarter said made some sense to me, but I don't immediately see how to apply that to the material in the Enumerations section of the reference. If anyone can add to what he posted, I would much appreciate the help.

    http://forum.unity3d.com/viewtopic.php?t=4014&highlight=enumeration

    Thanks so much! :D
     
  4. podperson

    podperson

    Joined:
    Jun 6, 2006
    Posts:
    1,371
    out is just a mistake. Ignore it.

    Enumerations are simply class constants that convey meaning to certain functions. E.g. if you want to establish a convention that, for a specific class the number 0 indicates "friendly" and the number 1 indicates "neutral" and the number 2 indicates "hostile" you might set up an "enumeration" such that

    MyClass.friendly == 0, MyClass.neutral == 1, etc.

    You can then write something like:

    if(MyTarget.faction == MyClass.hostile) FireWeapons(); // edit corrected

    This allows you to write readable code and not have to remember that in this specific case 2 == hostile.
     
  5. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    Not quite. It denotes that this parameter is used to output a value into a variable you've already created. In other words, you create an empty RaycastHit variable, then pass its name to the function, and the function fills it out with the data you need.

    In C#, you actually have to type this keyword in your code (for reasons of clarity), but in Javascript it's not necessary. At present, the scripting documentation gives function definitions using C# syntax, which is why you see it written in this way.
     
  6. podperson

    podperson

    Joined:
    Jun 6, 2006
    Posts:
    1,371
    Ah it's saying it's a variable parameter ... which would be a given in many languages since it's an object. Fair enough.

    This is a case where Pascal clobbers all the C-like languages for readability.