Search Unity

Return one's own variable as parameter

Discussion in 'Scripting' started by qcw27710, Nov 10, 2019.

  1. qcw27710

    qcw27710

    Joined:
    Jul 9, 2019
    Posts:
    139
    I have to do the following now:
    Code (CSharp):
    1. var color = view.GetCursor();
    2. var location = color.GetShade(color);
    But this sounds more logical:
    Code (CSharp):
    1. view.GetCursor().GetShade(<<<<<< use self);
    Unfortunately the closest I've ever got was:
    Code (CSharp):
    1. color.GetShade(view.GetCursor());
    Is there a way to perform more successive execution? This example is rather simple but sometimes I end up with four or five classes that I need to stack on top of each other. And that would really clean up the code by miles if I actually could stack them in the way the middle one is constructed.

    Is this something remotely doable, or is it me just dreaming too much?
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    If it's methods from Unity, i'm not aware of a way to do what you want. If it's your own methods, then you can make the parameter optional, and use "this" as default, which means you should then be able to simply call color.GetShade() instead of color.GetShade(color).
     
  3. qcw27710

    qcw27710

    Joined:
    Jul 9, 2019
    Posts:
    139
    I noticed that part, but what in case where this, cannot be used because its not the important variable? What if something else needs to passed through? What if it belongs to a different class and this doesn't contain the data?
     
  4. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    You always need to reference it somehow, so if the class does not already know the reference (like with 'this'), then it needs to be passed from the outside as a parameter. For some specific situations there are conveniences you can implement, and you should need to write one specific arrangement of lines more than once, but generally speaking in the end you will pass a missing reference as parameter.
    However, i believe you are doing something wrong if you need to continuously pass class references down the line. This may or may not be avoidable through a different design approach, depending on the situation. As we talked about above, a class shouldnt need a reference to itself for example. Also, your variable names are really not making a whole lot of sense.
     
    Last edited: Nov 11, 2019
  5. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    'GetShade' is a member of your 'color'... inside your implementation of 'GetShade' you should be able to just reference the color as 'this'.

    Why is 'GetShade' a member function but takes a parameter of the color to get the shade of (rather than a static function)?

    What is 'GetShade' supposed to do?

    Can we see the implementation of 'GetShade'?

    ...

    On a tangent...

    I'm super confused by all of these names... why does 'GetCursor' return value get set to a variable named 'color'? Why does 'GetShade' return value get to a variable named 'location'? Why does GetShade take a parameter? What is a 'shade'? What data types are 'view'/'color'/'location'?

    I would assume GetCursor would return... a cursor of some sort (whatever a cursor is). Not a... color.

    I would assume GetShade would return... a shade of some sort (whatever a shade is). Not a... location.

    What are these things...

    Let me just say... if your naming conventions don't make any sense... it's going to make for a really hard time to code.
     
    Last edited: Nov 11, 2019
    eisenpony likes this.
  6. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    974
    The syntax you're asking for is not in C#.

    However, as the others have eluded, this code looks very fishy. Passing an instance of T to a method of T is unusual unless you are doing some kind of comparison or aggregation. The fact that you are finding you want to send an object a reference to itself is evidence you are making unusual choices.
     
    qcw27710 and lordofduct like this.
  7. qcw27710

    qcw27710

    Joined:
    Jul 9, 2019
    Posts:
    139
    It's pseudo-code.

    Welp, time to do it traditional way then. Thanks.

    My fault in pseudo-code, there is not real-life reference. Rather
    GetShare();
    needs result of preceeding:
    view.GetCursor();
     
  8. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    Even when writing pseudo-code, write something that makes sense.

    If I said "What is wrong with my English grammar here?" And then showed you:

    The made up nonsense will get in the way of seeing the problem.

    Here it's a lot clearer what's wrong here. Because we don't have to filter out the nonsense.

    ...

    In your code:

    Code (csharp):
    1. var color = view.GetCursor();
    2. var location = color.GetShade(color);
    The problems that arise are:
    1) GetCursor and color imply data types that are Unity specific. This is a problem because some people may take you to mean "using the Unity API how do I accomplish my task?" When the unity API may have nothing to do with it.

    2) Names like 'location' imply data types that may seem to be significant but aren't. They also imply things like vectors which are again Unity API specific.

    3) The inconsistent match up of names (views returning cursors, cursors being named color, color returning shades, shades being named location) create noise that needs to be filtered out. Like my weird English grammar examples.

    You're literally making it difficult for us to help you.

    ...

    Here's a hint for later when wanting assistance.

    1) Include relevant code that correctly implies or explicitly states the data types that are involved.

    2) Any data types that we wouldn't know about because they are custom to you should be stated as well. Especially if they can be easily confused with known data types (color, location, cursor... all can be confused).

    3) Give us an idea of whose code is what. What can you edit, and what can't you edit? (you can't edit inside the unity API) When modifying the interface of a method/function, this is very critical.

    ...

    As I say in my signature:
     
  9. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    cont'd

    Back at your problem...

    Is 'GetShade' YOUR code, can you edit that function?
    Can you show use the source for 'GetShade'?
    What is 'GetShade' supposed to do?

    With that information we can tell you if you can modify GetShade to get the behaviour you desire. But it hinges on what 'GetShade' (in its TRUE form) actually is.

    With out this information... :shrug:.

    It's no longer a matter of knowing C#... it's a matter of knowing what's in your head. We can't read your mind. We can't see code you haven't shown us.

    Cause despite the pseudo-code nonsense... it still reads weird. As @eisenpony said:
    lastly

    Why though?

    You're calling 'GetShare' ON the result of 'GetCursor'. It already has a reference to that result... because the owner of 'GetShare' IS the reference. Why do you need to pass it a reference to itself?

    This is a code smell.

    If a function doesn't utilize the state of the object it's a member of, then it probably shouldn't be a member function but instead a static function.

    OR

    If a function does utilize the state of the object it's a member of, then it probably shouldn't be taking in a parameter as a reference to itself.
     
    Last edited: Nov 11, 2019