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

How to run function by searching its name?

Discussion in 'Scripting' started by leegod, Sep 26, 2014.

  1. leegod

    leegod

    Joined:
    May 5, 2010
    Posts:
    2,321
    So my game's character has various skills.

    Let's say my hero character has "Skill1", "Skill2", "Skill3" skill names.

    and I want to run those skill's specific effect function by searching skill's name.

    So I made like,

    Code (CSharp):
    1. public void RunSkill(){
    2.         switch(heroskills.name){
    3.         case "Skill1":
    4.             Skill1();        break;
    5.         }
    6.     }
    7.     public void Skill1(){
    8.  
    9.     }
    10.     public void Skill2(){
    11.        
    12.     }
    13.     public void Skill3(){
    14.        
    15.     }
    But isn't there more neat way with not using switch?

    Because I have many characters (over 40) and each character will has more than 5 skills, then total over 200 skills.
    I don't want to write over 200 switch case.
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
  3. Kogar

    Kogar

    Joined:
    Jun 27, 2013
    Posts:
    80
    I am not sure if i understand your question correctly.
    You want to call different functions and call them depending on a skill name? Or do you want similar skill effects and call them depending on your charactertype?

    For the first delegates could help.
    http://unity3d.com/learn/tutorials/modules/intermediate/scripting/delegates
    You could add heroskills.skill1 heroskills.skill2 and so on to a (multi-)delegate and afterwards call the delegate to start the added skills without having to go through a switch.

    For the second inheritance can help.
    http://unity3d.com/learn/tutorials/modules/intermediate/scripting/inheritance
    Make a parent class person/player with a function skill(basicattack, specialattack).
    Create your character from person/player and give him his personal skill effect. Then you can always call basicattack, specialattack and so on instead of a specific skill name and checking which charactertype it is.
     
  4. Krysalgir

    Krysalgir

    Joined:
    Aug 30, 2010
    Posts:
    95
    If you REALLY want to have the same name yuo can use SendMessage
    http://docs.unity3d.com/ScriptReference/GameObject.SendMessage.html

    Code (csharp):
    1.  
    2. public void RunSkills() {
    3.     SendMessage(heroskills.name, "");
    4. }
    5.  
    6. public void Skill1() {...}
    7.  
    I would not recommend it though... and you have to make sure the names are the very same.
     
  5. leegod

    leegod

    Joined:
    May 5, 2010
    Posts:
    2,321
    Thx.
    First is my intention.
    And I don't want multi-cast all of skills functions at one time. If user use one skill, only specific function needed to be called.
    Then how to do this by multi delegate? (you mean multi as += to delegate and increase functions to delegate?)
    If I should assign which function is for delegate at this time, it will also be grinding works like switch grinding.
     
  6. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Would 'StartCoroutine' be any help to you? There's an overload which accepts a string as name and fires the coroutine.

    As for the names, in order to not make every change in every script all the time, you could make a static helperClass that holds the skill names as a static (or even const) string variable, so you would only need to make sure that the string assigned to the variable matches the coroutines name. Any changes would only need to be applied to the coroutines name and the string.
     
  7. Kogar

    Kogar

    Joined:
    Jun 27, 2013
    Posts:
    80
    Just a question. You say that a user can select the skill. How does he do that? How does heroskills.name get its settings? Why can't that action directly call the function instead of calling a generic RunSkill()? (That would be better with inheritance where you only add the additional custom character runskill code to RunSkill() )
    StartCoroutine(heroskills.name) would be a safer bet when the user selects the skill.

    Just for fun. The following code would be needed when you want to create delegates of functions by calling their names.
    But the script is not fully complete since you have to change "typeof(yourClassName)" to your desired class from which you want to run a method.
    Code (csharp):
    1. //using System.Reflection; //has to be added
    2.  
    3. delegate void delegateSkillsetToRun();
    4. delegateSkillsetToRun characterSkillsetToRun;
    5.  
    6. //http://stackoverflow.com/questions/783108/c-return-a-delegate-given-an-object-and-a-method-name
    7. delegateSkillsetToRun GetByName(object target, string methodName)
    8. {
    9.    if(typeof(yourClassName).GetMethod(methodName,
    10.      BindingFlags.NonPublic | BindingFlags.Instance|BindingFlags.FlattenHierarchy)
    11.      !=null)
    12.      return (delegateSkillsetToRun)delegateSkillsetToRun.CreateDelegate(typeof(delegateSkillsetToRun),
    13.     target, methodName);
    14.    return null;
    15. }
    16.  
    17. public void prepareRunSkill(){
    18. characterSkillsetToRun = GetByName(this, heroskills.name);
    19. //characterSkillsetToRun += GetByName(this, heroskills.name); //otherskills
    20. }
    21.  
    22. public void Start() { prepareRunSkill(); } // i am thinking that a runSkill is not changing if the character is not changed.
    23.  
    24. public void RunSkill(){
    25. if(characterSkillsetToRun!=null)
    26.    characterSkillsetToRun();
    }