Equipping skills on buttons

How would you go about assigning skills to buttons?

As in, each button represents a skill slot and when a skill is assigned to that button it is loaded in and activates upon pressing the corresponding button.

My first instinct was to create a controller script that the skills could be loaded onto as a game object, then have it effect a skill controller script that would in turn activate the various functions and assets.

But then I realized this means that I would need to pass on the entire button state (on down, down, up, on up) rather than just a bool.

My second thought was that I could have a script that would set a string variable to store the name of the button and have it written as something like:

public string myButton;

Input.GetButtonDown(myButton){}

But this feels clunky as hell and I’m worried that it’s a bit much for a button press.

Any suggestions?

I take it that by “buttons” you mean keyboard/joystick buttons that you read through the Input class, and not UI buttons that are drawn on the screen and activated via mouse or touch.

It also sounds like you’re saying every skill has its own individual logic for how to interpret input events. That is, it’s not as simple as just telling the skill “start now”, but it needs to process when and how long the button is held according to custom rules that are unique to that skill in order to decide how to behave.

Given those constraints, using a variable to tell your skill script which button to listen to sounds like a totally reasonable idea to me. No reason that getting input using a string variable would be any less efficient than getting it using a string literal.

I would guess that most games use the same input-parsing logic for every skill, though. (Or a small number of different rule sets for different types of skills.) In that case, it might be easier to do the input processing in a single class and send abstracted messages to the individual skills.

1 Like

Hmmm…

So maybe have a place to park the skills as an object, and give it a string for its corresponding button in the menu.

I don’t know, this is how I would handle it:

public class PlayerInput : MonoBehaviour
{
    private FireBall fireBall;

    private void UnlockFireBall()
    {
        // This is called from wherver
        fireBall = new FireBall();

        // Attach this to something more permanent, handle it a bit better somehow
        fireBall.UnlockSkill();
    }

    private void Update()
    {
        if(Input.GetButton("FireBallButton") && fireBall != null)
        {
            fireBall.DoSkill();
        }
    }
}
public class Skill 
{
    public virtual void DoSkill()
    {
        // Common skill logic
    }

    public virtual void UnlockSkill()
    {
        // Common unlock skill logic
    }
}
public class FireBall : Skill
{
    private bool isSkillUnlocked;

    public override void DoSkill()
    {
        base.DoSkill();

        if (this.isSkillUnlocked)
        {
            // FireBall logic here
        }
    }

    public override void UnlockSkill()
    {
        base.UnlockSkill();

        isSkillUnlocked = true;
    }
}

I tried doing it the OO way, I hope it helps out a bit? I didn’t really test it out, I just wrote some quick things in the editor. You will have to make this a bit safer and not temporary. Right now I don’t use any player prefs here so no logic is saved between game instances, but I’m sure you can handle your way around that.

1 Like