0 votes

In the following two code snippets two different things happen.

func unhandledinput(event):
if not event.isactionpressed('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 updatenavigationpath(character.position, getlocalmouseposition()) doesn't trigger.

On the other hand when 'return' is commented:
output is similar in the debugger but function updatenavigationpath(character.position, getlocalmouseposition()) does trigger.

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

What am I doing wrong?

in Engine by (100 points)

Could you edit your post to format your script correctly? See: https://godotengine.org/qa/57

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

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

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.

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.

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.

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 updatenavigationpath(character.position, getlocalmouseposition()) 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.

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.

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

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.