Search Unity

Simple bind input to function class

Discussion in 'Scripting' started by ItsaMeTuni, Aug 30, 2017.

  1. ItsaMeTuni

    ItsaMeTuni

    Joined:
    Jan 19, 2015
    Posts:
    44
    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):

    Code (CSharp):
    1. void Start()
    2. {
    3.     InputHandler.AttachFuncToAxis("myAxis", MyFunc);
    4. }
    5.  
    6. void MyFunc()
    7. {
    8.     //Do some cool stuff
    9. }
    Here's an axis that I use:


    Here's the gist link: https://gist.github.com/ItsaMeTuni/f418b78f498cff8acb8f9234debfb613

    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).
     
    Last edited: Oct 6, 2017
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    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:

    Code (csharp):
    1.  
    2.        foreach(string key in delegateList.Keys)
    3.        {
    4.            if(Input.GetAxis(key) == 1 && delegateList[key].state == false)
    5.            {
    6.                delegateList[key].del();
    7.                delegateList[key].state = true;
    8.            }
    9.            else if(Input.GetAxis(key) < 1)
    10.            {
    11.                delegateList[key].state = false;
    12.            }
    13.        }
    14.  
     
  3. ItsaMeTuni

    ItsaMeTuni

    Joined:
    Jan 19, 2015
    Posts:
    44
    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! :)
     
    Last edited: Oct 5, 2017
  4. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,912
    GetButtonUp/Down and GetKeyUp/Down?
     
  5. ItsaMeTuni

    ItsaMeTuni

    Joined:
    Jan 19, 2015
    Posts:
    44
    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!