3

I have been attempting to use event handling for a game's input. When seeing others use similar methods, they are able to add a void function to the delegate variable without an error. Whenever I try to add the Move() function to OnAxisChange, I receive the following error:

Cannot implicitly convert type 'void' to 'CharacterView.InputAction'

public class CharacterView : MonoBehaviour {
    public delegate void InputAction();
    public static event InputAction OnAxisChange;

    public Vector2 InputAxis
    {
        get
        {
            float x = Input.GetAxisRaw("Horizontal");
            float y = Input.GetAxisRaw("Vertical");
            return (new Vector2(x, y));
        }
    }

    private void Update()
    {

        Vector2 input = InputAxis;
        if (input.x != 0 || input.y != 0)
        {
            if (OnAxisChange != null)
            {
                OnAxisChange();
            }
        } 
    }
}

The following is the class that handles the event.

public class CharacterController : MonoBehaviour {

    private void OnEnable()
    {
        CharacterView.OnAxisChange += Move();
    }
    private void OnDisable()
    {
        CharacterView.OnAxisChange -= Move();
    }

    public void Move()
    {
        Debug.Log("Entered the move function!");
    }

}

Using delegates for event handling is still a bit foreign to me, so I assume I am misunderstanding something.

  • Instead of creating your own delegates consider using EventHandler. https://msdn.microsoft.com/en-us/library/system.eventhandler(v=vs.110).aspx – Rand Random Jun 29 '17 at 06:29
  • @RandRandom is there some innate advantage for doing so? – Richard Mcilwain Jun 29 '17 at 07:24
  • The only advantage to using `EventHandler` is that it enforces the standard .NET event pattern signature of `object sender, EventArgs e`. Otherwise it is identical to using standard delegates. – Enigmativity Jun 29 '17 at 07:28
  • @RichardMcilwain the only real reason for me is, I am a lazy bastard and prefer to use EventHandler instead of writing my own delegates. I am just trying to infect you with my laziness. :) – Rand Random Jun 29 '17 at 07:47

1 Answers1

3

You need to remove () after Move. Like that you are calling your method and try to add the return type of it which is nothing. Just change it to the following.

CharacterView.OnAxisChange += Move;

CharacterView.OnAxisChange -= Move;
NtFreX
  • 10,379
  • 2
  • 43
  • 63
  • Where did you get the quote `"Event declaration adds a layer of protection on the delegate instance..."` and why did you put it in the answer? What does it have to do with the question? – Enigmativity Jun 29 '17 at 07:11
  • And why are you suggesting using a `EventHandler` rather than a delegate? It is still a delegate. – Enigmativity Jun 29 '17 at 07:12
  • @Enigmativity I have it from another question and I put it in because he is using delegates as event handlers. EventHandlers cannot be directly reset like an delegate. Or don't you agree? – NtFreX Jun 29 '17 at 07:13
  • @Enigmativity but if you think it makes no difference and is even so bad that it deserves an downvote I can take it out again. I just don't uderstand why – NtFreX Jun 29 '17 at 07:14
  • @Enigmativity hope your happy. I still would like to know why you think it was a bad post.... – NtFreX Jun 29 '17 at 07:17
  • I think you've misunderstood the quote. An `EventHandler` **can** be directly reset. An event declaration **cannot** be directly reset. But that doesn't relate to the use of an `EventHandler` or not. – Enigmativity Jun 29 '17 at 07:17
  • @Enigmativity according to [this](https://stackoverflow.com/questions/18170282/c-sharp-delegate-v-s-eventhandler) accepted answer it is the only reason to use events over delegates directly. Or do I misanderstand something? – NtFreX Jun 29 '17 at 07:20
  • Yes, you are misunderstanding. The answer you're linking to is discussing the difference between **delegates and events**, and not **delegates and `EventHandler`s**. An `EventHandler` is not an event. – Enigmativity Jun 29 '17 at 07:26
  • 2
    @Enigmativity hm... ah now I get it. Thanks for explaining.. I feel stupi now of corese `EventHandler` is just a delegate... – NtFreX Jun 29 '17 at 07:27
  • So how would you go about doing the same thing, if Move required parameters? – Richard Mcilwain Jul 07 '17 at 02:27