Im not sure if I understand the entire idea correctly. So in the new course (2d topdown combat RPG) we have a way to control Sword swing by mouse click. This is handled by the new input system, we create mouseclick event, and we subscribe to Event invoked by the Input System class in out code, like this:
private void Start()
{
playerControls.Combat.Attack.started += _ => Attack();
}
Why do we even use lambda expression here, and not “normal” subscription to event? So far I understand, that we use the lambda expression to kinda strip the callback context from the event, because we just don’t need it, but this seems quite strange.
Another question is - how do we unsubscribe? According to the C# documentation:
You cannot easily unsubscribe from an event if you used an anonymous function to subscribe to it. To unsubscribe in this scenario, go back to the code where you subscribe to the event, store the anonymous function in a delegate variable, and then add the delegate to the event. We recommend that you don’t use anonymous functions to subscribe to events if you have to unsubscribe from the event at some later point in your code.
Do we just take for granted that the sword will never be destroyed? (But as a matter of fact, there will be times, where we don’t use the sword, so I think it would make sense to unsubscribe from that event.)
Or do I miss the point entirely and this is some different system with similar syntax?