Search Unity

How to return a function in C#?

Discussion in 'Scripting' started by joeyxiong1994, Jul 29, 2019.

  1. joeyxiong1994

    joeyxiong1994

    Joined:
    Apr 18, 2017
    Posts:
    20
    Hi, I'm trying to build an ability system, where I come up with a structure that in my main script component, I have a function called "DoSkill()", and I have would have variable to store an enum SkillType "currentSkill".

    The SkillType is an enum I defined in another script component called "SkillManager", My SkillManager have a function called FindSkill(SkillType currentSkill).

    So I'm gonna call FindSkill(SkillType currentSkill) in my main script component's DoSkill(), and then in SkillManager's FindSkill(SkillType currentSkill), this would do a switch statement to find the correct skill function I pre-defined in SkillManager to return. And then in my main script component, I can use the function returned by FindSkill(SkillType currentSkill)

    For example: I defined "Vector3 Dash(float currentTime)" in SkillManager, and I defined enum SkillType {Dash}. In my main script component, I want to
    Code (csharp):
    1.  
    2. SkillType currentSkill = SkillManager.SkillType.Dash;
    3. float currentTime = 0.5f;
    4. DoSkill() {
    5.    var theRightFunction = FindSkill(currentSkill);  // Where theRightFunction should be Dash(float currentTime)
    6.  
    7.    // And then I can do like this
    8.    Vector3 answer = theRightFunction(currentTime);
    9. }
    10.  

    I wonder if I'm able to do this. And how should I write my FindSkill. Thanks
     
    Last edited: Jul 29, 2019
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    You can do this with delegates.

    Here is a super bare bones example of passing a function around using the C# default `Action` void delegate:
    Code (CSharp):
    1. public enum SkillTypes
    2. {
    3.     SpecialSkill,
    4. }
    5.  
    6. private Dictionary<SkillTypes, Action> actions;
    7.  
    8. // function to be passed around
    9. private void SpecialSkill()
    10. {
    11.     Debug.Log("SpecialSkill Used");
    12. }
    13.  
    14. // example of assigning it into a dictionary
    15. private void AddSkill()
    16. {
    17.     actions.Add(SkillTypes.SpecialSkill, SpecialSkill); // add the function to the dictionary
    18. }
    19.  
    20. // looking it up and returning it
    21. private Action FindSkill(SkillTypes type)
    22. {
    23.     return actions[type];
    24. }
    25.  
    26. // using it
    27. private void DoSkill(SkillTypes type)
    28. {
    29.     Action theSkill = FindSkill(type);
    30.     theSkill();
    31. }
     
    joeyxiong1994 likes this.
  3. ProtagonistKun

    ProtagonistKun

    Joined:
    Nov 26, 2015
    Posts:
    352
    You should use code tags to make your post a tad more readable. That said, you can use a lambda expression to achieve this. As well as what has been noted above with delegates. It depends on the complexity of your code.
     
    joeyxiong1994 likes this.