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

Function that returns boolean and changes one of its parameters?

Discussion in 'Scripting' started by Chapi, Sep 20, 2014.

  1. Chapi

    Chapi

    Joined:
    Aug 27, 2013
    Posts:
    13
    Hi, I'm trying to define a function similar to the function Physics.Raycast(origin: Vector3, direction: Vector3, hitInfo: RaycastHit, distance: float = Mathf.Infinity, layerMask: int = DefaultRaycastLayers): bool;

    Where it if it returns true it will also store information into one of its parameters, the "hitInfo" parameter, so how can I define a function that does this?

    Thanks!
     
  2. BmxGrilled

    BmxGrilled

    Joined:
    Jan 27, 2014
    Posts:
    238
    in C# you define your function like so:

    Code (CSharp):
    1. public bool SomeFunction(out string someVariable) {
    2.     if(Application.isPlaying) {
    3.         someVariable = "Playing!";
    4.         return true;
    5.     }
    6.     someVariable = "Figgidy-do!";
    7.     return false;
    8. }
    hope this helps! :)
     
    Chapi likes this.
  3. Chapi

    Chapi

    Joined:
    Aug 27, 2013
    Posts:
    13
    Thanks! Thats exactly what I'm looking for, but any idea how the "out" can be used in javascript?
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It can't be, but if you use a reference type for the variable, then changing the variable inside the function also changes it outside, since it's a reference rather than a copy.

    --Eric
     
    Chapi likes this.
  5. Chapi

    Chapi

    Joined:
    Aug 27, 2013
    Posts:
    13
    Thanks for the answer, and yeah that will probably work! How do I get a reference and not a copy of the variable then? :S

    I read that a way of doing this is by using a non primitive variable like an object instead of an int, etc but I'm trying to use a Vector3 in here so I'd prefer keeping the variable as a Vector3 rather than an object, can this be done?
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    No, you'd have to make a reference type. Classes are always by reference:

    Code (javascript):
    1. class RefVector3 {
    2.     var v3 : Vector3;
    3.     function RefVector3 (v3 : Vector3) {
    4.         this.v3 = v3;
    5.     }
    6. }
    7.  
    8. function Awake () {
    9.     var blah = new RefVector3(Vector3.one);
    10.     Debug.Log ("Before: " + blah.v3);
    11.     Foo (blah);
    12.     Debug.Log ("After: " + blah.v3);
    13. }
    14.  
    15. function Foo (vec : RefVector3) {
    16.     vec.v3 *= 2;
    17. }
    --Eric
     
  7. Chapi

    Chapi

    Joined:
    Aug 27, 2013
    Posts:
    13
    Damn, I'm having a hard time understanding that code since I've never defined a class or a class with a constructor yet but I have a lil idea of what is going on. But I'm not sure if its exactly what I'm looking for so let me briefly explain the code which I'm trying to use this with.

    I've got two scripts, one called selectionScript which defines the function getTarget() and another one called actionController which calls getTarget().

    Basically getTarget() casts a ray into space and collides with ground and finds the collision point. On the actionController, when I right click, the getTarget() is called and I move an object towards the collision point returned by getTarget(), but the thing is that it is possible to click somewhere where we have no collision point and in that case I'm forced to return null; and then I get an error saying that I cannot convert from null to Vector3 even though I've put an if so that if the return is null, nothing will happen but compiler still takes the possibility of being null and throws me that error.

    So I wanna convert the function to a boolean so that if it returns false nothing will happen, and then information of the hit will be stored in one of there parameters.

    Right now I've bypassed this by returning Vector3(1234,1234,1234) if the raycast hits nothing and in the actionManager I made the condition instead of being null to not being that specific Vector3 which is unlikely to happen unless we don't hit anything
     
  8. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Yes, it sounds like passing a reference is what you want.

    --Eric