Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Add 0 parameter Method to an Action<T>

Discussion in 'Scripting' started by CBuckington, Oct 1, 2023.

  1. CBuckington

    CBuckington

    Joined:
    Sep 28, 2020
    Posts:
    20
    Hey :)

    I was wondering if it was somehow possible to call a 0 parameter Method alongside other functions in an Action<int>. The obvious solution to me is to just overload the Method, add the parameter, but never use it. Then again... this feels wrong. Can someone help me out here? :)


    Code (CSharp):
    1.  
    2. public static Action<int> OnGameOverEvent;
    3.  
    4. OnGamerOverEvent += CalculateStuff;
    5. OnGamerOverEvent += ToggleStuff;
    6.  
    7. public void CalculateStuff(int someValue){
    8.     // do stuff with value
    9. }
    10.  
    11. public void ToggleStuff(){
    12.    // just toggle a bool flag
    13.    // might also get called by itself from other scripts
    14. }
    15.  
    16.  
    17. // My Thought to overload
    18. public void ToggleStuff(int unusedValue){
    19.    ToggleStuff();
    20. }
    21.  
    22.  
     
    Last edited: Oct 3, 2023
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,899
    You mean like so?

    public static Action OnEventWithoutParameters;

    But if you cannot or don't want to use a separate event ignoring the parameter is just fine.
     
  3. CBuckington

    CBuckington

    Joined:
    Sep 28, 2020
    Posts:
    20
    Thanks for the fast reply.

    Yeah, I did not want to create a seperate event for that, but since I use the Toggle function from different scripts aswell it doesnt really need a parameter.

    If there are no other "smoother" ways to do it then thats gonna be it :) Thanks
     
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,495
    Why is that? Since you want to react to an event that has an argument but you're not interested in that argument, there's no way around that. Note that when you just subscribe your method through code, you can use a closure by using the lambda operator. However doing that means you can not unsubscribe your method unless you store the actual closure somewhere. Though in that case it would be much cleaner to create an explitic method like you've done.

    Code (CSharp):
    1. OnGamerOverEvent += i=>ToggleStuff();
    Here our lambda expression takes the argument as "i" but doesn't use it when calling ToggleStuff. It's just the same as your explicit method, just with an implicitly created method. So as I said, creating an explicit method is much cleaner.

    Also from your code it's not clear what argument you actually pass to the event. You really should give your arguments more descriptive names. "value" is the most generic name and is even a keyword in some contexts (like property setters).
     
    CBuckington, Nad_B and CodeRonnie like this.
  5. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    303
    Also you can use _ which means in C# "ignore this";

    Code (CSharp):
    1. OnGamerOverEvent += _ =>ToggleStuff();
     
    CBuckington likes this.
  6. CBuckington

    CBuckington

    Joined:
    Sep 28, 2020
    Posts:
    20

    Thank you all for your insights :) I already use quite a lot of lambda expressions but since the functions in question need to be unsubbed from the event throughout the code lambdas seemed like the wrong approach. And dont worry, my example above was just something I wrote to demonstrate my point, I am obviously not naming my functions "CalculateStuff" and my parameters "value" haha <3

    Things like that happen to me quite a lot. I start thinking about better or cleaner ways to do something that already works just fine :')

    tldr: my first instinct was totally sufficient!

    Best regards!
     
    Bunny83 likes this.