Use for frequently empty parentheses...

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!

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

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

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).

function Update () {
    var hit : RaycastHit;
    if (Physics.Raycast (transform.position, Vector3.down, hit)) {
        distanceToGround = hit.distance;
    }
}
// Raycast up to 100 meters forward
function Update () {
    var hit : RaycastHit;
    if (Physics.Raycast (transform.position, Vector3.down, hit, 100.0)) {
        distanceToGround = hit.distance;
    }
}

"

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! :smile:

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.

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.

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.