Unity Community


Results 1 to 17 of 17

  1. Posts
    421

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

    How can I rewrite it in UnityScript?

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

    Thanks

  2. Volunteer Moderator
    Posts
    23,708
    You can't; there's no way to specify a reference parameter in Unityscript functions.

    --Eric
    SpriteTile: new tile system that works seamlessly with Unity 4.3 sprites
    FlyingText3D: dynamic 3D text with TTF fonts | Vectrosity: fast & easy line drawing
    Nifty utilities! Stitch terrains together - runtime model importing - file browser - fractal landscapes


  3. Location
    Zürich, Switzerland
    Posts
    26,572
    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. Posts
    421
    Ah ok, basically I am trying to rewrite the file browser code in javascript.
    http://www.unifycommunity.com/wiki/i...le=FileBrowser
    Is it possible in principle or what is there is possible only with C#?


  5. Location
    Zürich, Switzerland
    Posts
    26,572
    it works in general, but you can not return the complete, instead you would return the path (location) for example


  6. Posts
    421
    Quote Originally Posted by dreamora View Post
    it works in general, but you can not return the complete, instead you would return the path (location) for example
    Thanks, guys!

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


  7. Location
    Zürich, Switzerland
    Posts
    26,572
    means that at the end of the function you would return the location, not a boolean that indicates complete or incomplete


  8. Posts
    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:  
    1. public static bool FileBrowser( object loc, object dirScroll, object fScroll )
    2. {
    3. stirng location1 = (string) loc;
    4. Vector2 directorySroll1 = (Vector2)dirScroll;
    5. Vector3 fireScroll1 = (Vector2)fScroll;
    6. }

    The calling side...

    Code:  
    1. string loctaion;
    2. Vector2 directoryScroll;
    3. Vector2 fireScroll;
    4.  
    5. FileBrowser( (object)loc, (object)directoryScroll, (object)fireScroll);

    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 by KennyW; 10-07-2010 at 11:21 PM.


  9. Location
    Zürich, Switzerland
    Posts
    26,572
    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. Volunteer Moderator
    Posts
    23,708
    Quote Originally Posted by KennyW View Post
    I guess you can have the ref effect by casting all your parameters as "object", though I don't know much about Unity Script.
    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
    SpriteTile: new tile system that works seamlessly with Unity 4.3 sprites
    FlyingText3D: dynamic 3D text with TTF fonts | Vectrosity: fast & easy line drawing
    Nifty utilities! Stitch terrains together - runtime model importing - file browser - fractal landscapes


  11. Posts
    421
    I will try all suggestions, thanks


  12. Posts
    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. Volunteer Moderator
    Posts
    23,708
    Quote Originally Posted by laurie View Post
    Actually, the 'ref' keyword has nothing to do with whether the value being passed is a reference type or not.
    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:  
    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
    SpriteTile: new tile system that works seamlessly with Unity 4.3 sprites
    FlyingText3D: dynamic 3D text with TTF fonts | Vectrosity: fast & easy line drawing
    Nifty utilities! Stitch terrains together - runtime model importing - file browser - fractal landscapes


  14. Posts
    3
    what are variable types that are passed by reference?
    An instance of a class maybe? what else

  15. Volunteer Moderator
    Posts
    23,708
    Classes are passed by reference; primitive types and structs are passed by value.

    --Eric
    SpriteTile: new tile system that works seamlessly with Unity 4.3 sprites
    FlyingText3D: dynamic 3D text with TTF fonts | Vectrosity: fast & easy line drawing
    Nifty utilities! Stitch terrains together - runtime model importing - file browser - fractal landscapes


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

  17. Volunteer Moderator
    Posts
    23,708
    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
    SpriteTile: new tile system that works seamlessly with Unity 4.3 sprites
    FlyingText3D: dynamic 3D text with TTF fonts | Vectrosity: fast & easy line drawing
    Nifty utilities! Stitch terrains together - runtime model importing - file browser - fractal landscapes

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •