André Bianchini(andrebianchini)

Why would order matter here?

I was following this section, but my character would not jump at all. It worked when, on the "calculate_move_velocity", I ordered the passed parameters as you did. Mine had "speed" before "direction". As soon as I ordered as it is in your code, the character became able to jump. Why would the order of the parameters matter?
  • Nathan Lovato replied

    I would have to see the full project but my guess would be that you swapped the parameters but not the values, and as they're both Vector2 (I think) you didn't get an error.

    The jump requires the direction vector components to be -1.0, 0.0, or 1.0. And with a speed vector that's much longer, you would fall out of the code's expectations, making jump not work.

    But that's just a guess.

  • a
    André Bianchini(andrebianchini) replied

    Thanks for the answer! I'm trying this course again and fell to the same mistake!

    While I understand better your reasoning, why a parameter would be fetching the value of another at all?

  • Nathan Lovato replied

    When you define a function's parameters, for example:

    func move(speed, direction):

    The parameters are just identifiers for the first value and the second value passed to the function. For example, if I call the function like so:

    move("hello", []):

    In the move function, speed will be equal to "hello", and direction will be an empty array.

    The name of the variables you pass to the function do not matter, you're passing the values behind these variables. So if you invert two variable names, you're inverting the two corresponding values in the function call, breaking it.