Simple bind input to function class

EDIT: just use GetButtonDown instead (I didn’t know about it when I wrote this). Only use this script if you want to bind a delegate to a button, it does nothing more than that.

Hello everyone!

I just wanted to share with you an actually simple class that I made that assigns delegates to axis. If you want to give a name to an input in your game (like an inventory toggle button) so that if you want to change that key later you don’t need to refactor your code you will probably create an axis for that key (like I did). But if you use that axis in your update function to (for example) toggle an inventory GUI when that axis’s key is pressed the inventory will open and close 60 times a second. To go around that I made some boolean variables to store the last frame’s key state. That wasn’t a good solution for me because it would just pollute my code so I created this class :).

You just need to create an empty object in your scene, add the script to it and add an “InputHandler” tag to it.
Then to bind an axis to a function in your code just do this (note that you can do this from any class and you don’t need a reference to the InputHandler object):

void Start()
{
    InputHandler.AttachFuncToAxis("myAxis", MyFunc);
}

void MyFunc()
{
    //Do some cool stuff
}

Here’s an axis that I use:

Here’s the gist link: This is a simple class used to bind delegates to a button and invoke that delegate when the button is pressed. · GitHub

PS: I’m not a pro at programming (also not a noob :p) and learned events today just to develop this class, so if I called something the wrong name or made any mistakes please tell me and I’d be happy to re-code the thing.

Hope you all find it useful, Tuni.

Edit
Made changes to the code so now it is easier to read: foreach loop instead of for loop, AxisInfo is now a class instead of a struct to avoid all that weird copying code (suggestion from GroZZler).

Neat idea, but unless I’m missing something, there’s no reason for all that weird copying going on in your Update. This should work just fine and be significantly faster:

       foreach(string key in delegateList.Keys)
       {
           if(Input.GetAxis(key) == 1 && delegateList[key].state == false)
           {
               delegateList[key].del();
               delegateList[key].state = true;
           }
           else if(Input.GetAxis(key) < 1)
           {
               delegateList[key].state = false;
           }
       }

You can’t change field values directly in structs (if you try to do so you get the CS1612 error). To do that AxisInfo would need to be a class. I’m gonna change the code so AxisInfo is a class, that’ll make the code easier to read. Also, I don’t really know why I used for instead of foreach, thanks for letting me know! :slight_smile:

GetButtonUp/Down and GetKeyUp/Down?

Oh wow. I didn’t know that GetButtonDown existed… Anyways this script could be useful just to bind a function to a key rather than checking for that key on the update function. Thanks for letting me know!