Search Unity

return ref out variable

Discussion in 'Scripting' started by kilik128, Sep 13, 2017.

  1. kilik128

    kilik128

    Joined:
    Jul 15, 2013
    Posts:
    909
    hi i try make fonction for return variable

    for exemple

    void ref int check ( ){

    return ref variable;

    }
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    ref is an argument keyword. You pass a primitive to a method as a reference, not return one. What it is you're trying to do?
     
  3. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    KelsoMRK likes this.
  4. kilik128

    kilik128

    Joined:
    Jul 15, 2013
    Posts:
    909
    ok thank's

    i try found the easy way to get variable from class via on inspector and go back with him in another fonction


    what's the unity c# version for information ?
     
  5. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    remember your return type is the first word after public or private, so if you put
    void myfunction(){}

    It means there is no return.
     
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    I'm not sure I understand what you're trying to do.
     
  7. kilik128

    kilik128

    Joined:
    Jul 15, 2013
    Posts:
    909
    just select variable in inspector by enum or reflexion
     
  8. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    What does "select" mean?
     
  9. kilik128

    kilik128

    Joined:
    Jul 15, 2013
    Posts:
    909
    one selection like delegate fonction
     
  10. kilik128

    kilik128

    Joined:
    Jul 15, 2013
    Posts:
    909
  11. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    There are probably better ways to do it instead of breaking encapsulation in such a way.
     
  12. kilik128

    kilik128

    Joined:
    Jul 15, 2013
    Posts:
    909
    yes any idea please ?
     
  13. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Yes, use normal OOP techniques. Getter/Setter would be enough for that, I don't see any benefit from your choice using the ref-keyword in returns (besides that it's not officially supported any way), as it just breaks the encapsulation without any further advantage.

    If you've got a reference to the object that you'd call your "Cool()" - method on, you could as well just call "SetXYZ(int value)" and the instance would still be in control of the internal logic, validation and assigment. The changes would aswell be reflected in the inspector.

    Besides that, your int variable is public anyway. This is kinda redundant to having the Cool() method, as you could manipulate it directly.

    The ref-return feature has got its usages, but this is apparently none of the ones you'd use it for. It's just a way to reference values you don't want to copy all the time directly and IMO you shouldn't expose such a functionality in any way, because literally everyone calling this function and using the result as 'ref local' is able change the state from the outside and corrupt an object's state. It's just evil if used incorrectly.
     
  14. kilik128

    kilik128

    Joined:
    Jul 15, 2013
    Posts:
    909
    sure without encapsulation advantage is less fun.

    what's you call ?
    "SetXYZ(int value)" and the instance would still be in control of the internal logic
     
  15. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Set the value with a method.