1. Submit bugs to enter our 5.4 Beta Sweepstakes. Read more about it here.
  2. Unity 5.4 public beta is now available for all users. Grab the build and visit the 5.4 beta forum.
  3. Missing a module? More about the new installer in 5.3 here.
  4. 4.7.1 released for Xcode 7.3 support. Thread here.

What is "ref" in C# and its analog in UnityScript?

Discussion in 'Scripting' started by Nikolay116, Oct 7, 2010.

  1. Nikolay116

    Nikolay116

    Joined:
    Mar 21, 2010
    Messages:
    421
    How can I rewrite it in UnityScript?

    Code (csharp):
    1. public static bool FileBrowser( ref string location, ref Vector2 directoryScroll, ref Vector2 fileScroll )
    2.  
    Thanks
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Messages:
    29,659
    You can't; there's no way to specify a reference parameter in Unityscript functions.

    --Eric
     
  3. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Messages:
    26,595
    there is no analog to it in unity script

    if you meet such a thing you can use it with C# only, for UnityScript you would require a function that returns the location.

    ref / out are special parameters to pass reference to references into functions so you can assign new objects to it.
    you can find out more on it in any C# book or through google, there are a few hundred articles on the matter offering you different explaination approaches
     
  4. Nikolay116

    Nikolay116

    Joined:
    Mar 21, 2010
    Messages:
    421
  5. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Messages:
    26,595
    it works in general, but you can not return the complete, instead you would return the path (location) for example
     
  6. Nikolay116

    Nikolay116

    Joined:
    Mar 21, 2010
    Messages:
    421
    Thanks, guys!

    One minor thing, what did you mean by "but you can not return the complete"?
     
  7. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Messages:
    26,595
    means that at the end of the function you would return the location, not a boolean that indicates complete or incomplete
     
  8. KennyW

    KennyW

    Joined:
    Aug 20, 2010
    Messages:
    128
    I guess you can have the ref effect by casting all your parameters as "object", though I don't know much about Unity Script.

    Something like...

    Code (csharp):
    1.  
    2. public static bool FileBrowser( object loc, object dirScroll, object fScroll )
    3. {
    4. stirng location1 = (string) loc;
    5. Vector2 directorySroll1 = (Vector2)dirScroll;
    6. Vector3 fireScroll1 = (Vector2)fScroll;
    7. }
    8.  
    The calling side...

    Code (csharp):
    1.  
    2. string loctaion;
    3. Vector2 directoryScroll;
    4. Vector2 fireScroll;
    5.  
    6. FileBrowser( (object)loc, (object)directoryScroll, (object)fireScroll);
    7.  
    Again, I don't know much about how Unity Script converts primitive values into objects. But I think that there should be a way for you to convert anything into objects. In C#, everything is rooted on the object class naturally that you should be able to convert whatever into an object.
     
    Last edited: Oct 8, 2010
  9. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Messages:
    26,595
    That has nothing to do with ref at all.

    ref allows you to assign a new object to the loc within the function, even generate a whole new object.
    this can not be done through any other meaning (aside of out which is c# only too).

    as such to get a comparable effect with UnityScript, you must change the return of the function to string and return complete; must become return location;
     
  10. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Messages:
    29,659
    That's not how it works, not even in C#. If you want a variable to be passed by reference, you have to specify "ref" or else use a variable type that's inherently passed by reference.

    --Eric
     
  11. Nikolay116

    Nikolay116

    Joined:
    Mar 21, 2010
    Messages:
    421
    I will try all suggestions, thanks
     
  12. laurie

    laurie

    Joined:
    Aug 31, 2009
    Messages:
    638
    Actually, the 'ref' keyword has nothing to do with whether the value being passed is a reference type or not. Its effect is on how assignments to the formal parameter within the called function affect variable that was passed in the function call.

    To the OP: as stated above, there is no equivalent to declare a 'ref' parameter in UnityScript (so you can't write a function that operates like that); but UnityScript does support 'ref' semantics when calling functions defined that way in C#. So you can call the existing C# implementation as it is, without re-writing it in UnityScript, just fine. If you're re-writing it for instructional/learning purposes, then you'll need to make changes to the API to accommodate the limitation in UnityScript. But if you just want a file browser you can call from UnityScript, use the existing C# code as is. Just make sure the script is in a folder where it will be compiled before your calling script (i.e. put it into a folder named Plugins or Standard Assets (amoung others)).
     
  13. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Messages:
    29,659
    I didn't say that it did. A sort of hack which more or less gets the results of using "ref" in JS is to use a reference type instead of a value type.

    Code (csharp):
    1. class RefString {
    2.     var s : String;
    3.     function RefString (value : String) {
    4.         s = value;
    5.     }
    6. }
    Use that instead of a string and you basically get the effect of declaring "ref" with a string parameter in a function, since classes are always passed by reference.

    --Eric
     
  14. initTechsuport

    initTechsuport

    Joined:
    May 16, 2013
    Messages:
    3
    what are variable types that are passed by reference?
    An instance of a class maybe? what else
     
  15. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Messages:
    29,659
    Classes are passed by reference; primitive types and structs are passed by value.

    --Eric
     
  16. initTechsuport

    initTechsuport

    Joined:
    May 16, 2013
    Messages:
    3
    The way i understand it is, even though vanilla c# does this, the ref keyWord cant be used in unity's uniscript to pass primitive types as references. Did i word that accurately?

    I swear i have seen ref used in a second class made inside a script.

    Code (csharp):
    1.  
    2. using UnityStuff
    3.    public class ScriptName:Monobehaviour{
    4.    public int value=stuff;
    5.    public whatKindOfClassIsThis instanceOfSomeKindOfCustomClass;
    6.    void Start(){
    7.    ModReference(value);
    8.       instanceOfSomeKindOfCustomClass.ModReference(value);
    9.    }
    10.    void ModReference(ref int val){//cant use ref here, maybe because it derives from MonoBehaviour?
    11.       val=otherStuff;
    12.    }
    13. }
    14. public class whatKindOfClassIsThis?{
    15.    void ModReference(ref int val){//can use ref here? why?
    16.       val=otherStuff;
    17.    }
    18. }
    19.  
     
  17. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Messages:
    29,659
    You can use ref in C# just fine. You can call C# functions that use ref from Unityscript just fine too; the ref is implied in that case since you can't write it explicitly. (Same deal for "out"; see Physics.Raycast in Unity for example.) What you can't do is declare ref or out in Unityscript functions directly. Note that Unityscript and C# are different languages.

    --Eric