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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Skill System

Discussion in 'Scripting' started by EeanZ, Mar 24, 2020.

  1. EeanZ

    EeanZ

    Joined:
    Nov 11, 2019
    Posts:
    8
    I have 2 players that are currently using the same script for movement, health and shooting. the only difference they will have is they will have different skills. for example one will have dash, one will have teleport. currently, i have it working where the player script has a hasDashSkill, and HasTeleportSkill bool. In the update method, it is constantly checking if bool is true and calling a function to perform the skill.

    The skills are obtained later on in the game, thats the reason why they have a hasSkill check, though each player will only have one skill.

    It feels inefficient to be checking the other skill when it will never obtain it. is there a better way to do this?


    I also plan on adding more skills later on. so that will be a lot of hasSkill checks if I keep doing it this way.
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Why don' you create a general 'skill' class that defines a 'doSkill' method, then subclass that class into 'dashskill' and 'teleportskill' where you implement the actual movement in the respective doSkill() methods. Your character has a skill variable 'mySkill' of type skill, and in editor you can connect the correct subclass. In update you simply call mySkill.doSkill() - and you can even later add many other skills.

    Yup, that's basic object oriented programming :)
     
  3. EeanZ

    EeanZ

    Joined:
    Nov 11, 2019
    Posts:
    8
    Thank you for replying, My main problem is trying to remove the repetition of having "hasSkillNameCheck" for each skill. I'm not sure if i'm understanding your solution if you did solve that

    To clarify, you mentioned to call the doSkill method from Skill class. So my original problem would be how would the game check if the player has picked up the skill if not to do a bunch of if statements for bool checks.

    I think with this method, What I can do is add a skillID that is initialized when skill is picked up. The doSkill will take the skillID as a parameter and inside method will have a switch to see which skill to do.

    I guess now my question is, is this better than my original.
     
  4. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    I don't know if it is 'better', as that is very subjective. But let me try to give you an example. In your player's Update, you always invoke the skill:

    Code (CSharp):
    1. public Skill mySkill; // the current skill
    2. void Start(){
    3.     mySkill = new Skill();
    4. }
    5.  
    6. void Update(){
    7.    ...
    8.    mySkill.doSkill();
    9.  
    10. }
    11.  
    So there is no testing, you always invoke the skill. Now, initially (in Start), the mySkill variable is intialized to the generic skill class that does nothing:

    Code (CSharp):
    1. public class Skill : MonoBehaviour {
    2.     public virtual void doSkill(){
    3.        // do nothing, override to implement some skill.
    4.     }
    5. }
    Now, let's implement a new teleport skill as a child class of Skill.

    Code (CSharp):
    1. public class TeleportSkill : Skill{
    2.     public override doSkill(){
    3.           // do some teleporting
    4.     }
    5. }
    Ok, so how do we make the character to be able to use it? For example by instantiating and assigning in a method that givesthe skill:

    Code (CSharp):
    1. public void giveTeleportSkill(){
    2.      this.mySkill = new TeleportSkill();
    3. }
    Once you invoke 'giveTeleportSkill', your object has that skill and invokes it automatically during update. You can implement vastly different sills this way, and you can even give your character multiple skills (if that makessense) by making mySkill a List<Skill> and iterating all skills.