Function doesn't trigger in _unhandled_input(event)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ashish

In the following two code snippets two different things happen.

func _unhandled_input(event):
if not event.is_action_pressed(‘click’):
print(‘Inside action pressed’)
return

_update_navigation_path(character.position, get_local_mouse_position())

Output on the debugger

Inside action pressed
Inside action pressed
Inside action pressed
Inside action pressed
Inside action pressed
Inside action pressed
Inside action pressed

But the function _update_navigation_path(character.position, get_local_mouse_position()) doesn’t trigger.

On the other hand when ‘return’ is commented:
output is similar in the debugger but function _update_navigation_path(character.position, get_local_mouse_position()) does trigger.

I want the function _update_navigation_path(character.position, get_local_mouse_position()) to trigger even when return is part of the code.

What am I doing wrong?

Could you edit your post to format your script correctly? See: README: How to use this Q&A? - Archive - Godot Forum

I have no idea how your script is indented so I can’t really tell what’s inside which function or if/else block.

Bernard Cloutier | 2020-10-07 21:52

the return statement is doing what it is supposed to do.

ArthurER | 2020-10-07 22:18

That tells me nothing. Please, just click edit on your answer, highlight the code, and press the curly braces icon. That will format the code in a readable way. Then save your edits.

Bernard Cloutier | 2020-10-08 00:29

I’m not the OP, I commented to the OP .

based on what is posted here the return statement is doing what it is supposed to do, if the return statement is indented in the if ! block the output says the action was not click , it printed and return exited the function and that is what happened , it should not be a surprise. if it is outside the block… then the call to updatenavigation path will never be called from that function.

ArthurER | 2020-10-08 04:09

Right, sorry. So I guess the OP doesn’t understand return.

To OP: the return statement terminates the function call. Any code in the same function below it will not be executed.

Bernard Cloutier | 2020-10-08 13:10

Thanks for your guidance.

func _unhandledinput(event):
         if not event.isactionpressed('click'):
         print('Inside action pressed')
         return
    _update_navigation_path(character.position, get_local_mouse_position())

I understand that anything after the return statement will not execute in the ‘if/else’ block. My question is why is the function _update_navigation_path(character.position, get_local_mouse_position()) not called whether or not the ‘if/else’ statement is true or not. After all, it is part of the function _unhandledinput(event).
My point is that why is _unhandledinput(event) called when return is commented and not called when return statement exists during runtime.

Thanks in advance.

ashish | 2020-10-09 09:18

I understand that anything after the return statement will not execute in the ‘if/else’ block.

Not only in the if/else block, but in the whole function. Executing a return statement inside the _unhandled_input function will cause the function to terminate, and control will be given back to the calling function (in this case, the calling function is internal to the engine). The return statement isn’t dependant on whether or not it is in a if/else block or a loop, it will terminate the whole function either way.

Example:

func test(return_early: bool):
    print("before")
    if return_early:
        return
    print("after")

func _ready():
    print("First test:")
    test(true)
    print("Second test:")
    test(false)

Try this example in your game. The first call to “test” will only print “before”. The second will print “before” and “after”. This is very normal behaviour, I don’t know of any programming language that doesn’t behave this way in regards to the return statement.

If you want _update_navigation_path to be called even when the code inside the if block is executed, simply remove the return statement. Perhaps you mistakenly believe that an if statement must end with a return, but it is not the case.

Bernard Cloutier | 2020-10-09 13:56

Thanks for the clarification. Makes sense. I guess, I was trying to use break and return
interchangeably.
Thanks again.

ashish | 2020-10-09 18:43