Why is _input(event)'s parameter event not recognised in _process(delta) function?

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

Hi,
Is there a reason why the ‘event’ of _input(event) not recognised in _process(delta) function?
For example, if why can’t I have a construct like this:

func _process(delta):
    _unhandled_input(event)

func _unhandled_input(event):
    if event.is_action('ui_right')

I get an error identifier ‘event’ not recognised.

:bust_in_silhouette: Reply From: DaddyMonster

Variables declared in the argument of methods (functions) are limited in scope to that method. Methods are like Vegas, what happens in them, stays in them. :slight_smile:

What you’re doing is calling the method ‘_unhandled_input’ (which doesn’t make any sense but it’s not what’s crashing first) and telling Godot to pass the argument “event”. But event doesn’t exist in the method _process.

Look at this example

    func my_method(arg):
        var foo
        print(foo, arg)  #<--- this is inside method and works

print(foo, arg) #<--- this is outside and doesn't work.

If you want the variable to be within the scope of all methods then declare it at the beginning.

var foo = "hello world"

func my_method():
    print(foo) #<--- works

This is a good thing. It means you’re safe to say ‘i = 1’ in one method and then use the same variable in another method without having to worry about them interfering with each other. If you want to get a value out of a method then you should use “return”.

Methods that begin with underscore are inbuilt methods. _process is run every frame whereas _unhandled_input will be called every time there’s an input by the user. So there’s no need to call it, just decide what you want to do with the input within that method.