0 votes

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):

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

I get an error identifier 'event' not recognised.

in Engine by (100 points)

1 Answer

+1 vote

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. :)

What you're doing is calling the method 'unhandledinput' (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 _unhandledinput 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.

by (2,043 points)
Is 'event' not a reserved word?
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 webmaster@godotengine.org with your username.