Two Quick Questions

Hi! Really enjoying the course so far. I have two questions about this lecture.

  1. Why do we need to include an OnDestroy() method that disables the controls?

  2. Why are we saying if (!context.performed) {return;} instead of just saying if (context.performed) and then calling the invoke method after that? In other words, why are we checking if the key was not pressed, then just returning? Wouldn’t the key have to be pressed in order for this method to be called anyway?

Thanks!

Hi,

i dont know about your first question, dont know on which course/part you are.
If i had to guess its to stop the player from being able to control the character after death?

if you put if (!context.performed) {return;} you stop every line under there from being executed,
as it returns out the method it was handling, in some cases you could do it as you said,
by putting it all between the curly brackets of if (context.performed) so it just skips it if its false.

1 Like

Hey @Matt_Beebe . Here are my guesses to your questions :

  1. OnDestroy is also called when you load a new scene (ex: a new level). I guess this would be useful if, for example, in Lvl 1 - you are on the ground and have a specific Control Scheme, and in Lvl 2, you are driving/flying, and you want a different control scheme. It’s always helpful to read the unity docs about built-in functions and what they do. [Unity OnDestroy]

  2. I think it makes more sense to write the shortest amount of code that’s the easiest to read and handles the most exceptions. In this case, if you would start with your If statement with what you want to do, there’s a higher chance you will forget to place the else at the end of the return statement. There’s also the added complexity if you want to check multiple flags before doing something; in that case, I think you will end up with duplicated code

Example 1

    public void OnJump(InputAction.CallbackContext context)
    {
        if (!context.performed) { return;}
        JumpEvent?.Invoke();
    }
    public void OnJump(InputAction.CallbackContext context)
    {
        if (context.performed)
        {
            JumpEvent?.Invoke();
        }
        else
        {
            return;
        }
    }

Example 2

 if (stateMachine.Targeter.CurrentTarget != null && stateMachine.InputReader.IsTargeting) {return;}
 if (movement == Vector3.zero) {return;}
 FaceDirection(movement, Time.deltaTime);

In example 2, I think you would need to write "FaceDirection(movement, Time.deltaTime); " in both checks, which would add a considerable amount of extra code lines in the long run.

My best advice - Get the free trial of JetBrains Rider - Rider: The Cross-Platform .NET IDE from JetBrains
The amount of suggestions it gave me to improve my code is fantastic (compared to Visual Studio)

2 Likes
  • Answer to 1 - This is so that when one of the controls are pressed it does not try to call a Method that does not exist. It is possible for the controls to exist and the Input Reader to no longer exist it depends on how Input Reader gets picked up by the Garbage Collector.

  • Answer to 2 - We have to the check because the Input system calls the Callbacks at three different times. Started, Performed, and Canceled.

    • Started - The Action was Started, the button is first pressed.
    • Performed - The Action was performed, this varies by how the action map is set up, could be the button was pressed down, the button was pressed down for a certain amount of time, the button was pressed and released. In our case it is the button is pressed
    • Canceled - The action was canceled, the button was released.
2 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms