By what logic does event.position exist?

Godot Version

Latest

Question

This docs page has an example that uses “event.position”.

https://docs.godotengine.org/en/stable/tutorials/inputs/mouse_and_input_coordinates.html

By what logic, starting from _input on the Node docs page and using the docs only, can a user come to the conclusion that event.position exists as a thing? What tells you that event has a position available? How do you know you can do event.position?

My starting point is the https://docs.godotengine.org/en/4.2/classes/class_node.html#class-node-private-method-input

Specifically I have:

func _input(event):
    print(event.position)

But I have no idea how to find more event things that I can access like position here.

Hi there!

Basically when you see something like this in the docs:
image

It usually means that the argument is of the given class (in this case InputEvent) or any class inheriting from it.

So the next step is opening the reference for that class. It gives you a list of “Inherited By”. These are all subclasses for events that may happen:

Some of them have even more subclasses. This for example is the full inheritance chain for mouse click events:
image

So where does the event.position come from? It is coming from the InputEventMouse, which in turn has two subclasses: mouse clicks and mouse movements.

Docs: InputEventMouse


Also a tip regarding your code:

func _input(event):
    print(event.position)

This will lead to errors. For example when you press a key on your keyboard, the event will have no position as it is not a mouse event. So what you usually want to do is checking what kind of event you have. For example to only detect mouse clicks:

func _input(event):
    if event is InputEventMouseButton:
        print(event.position)

Or if you want to detect only mouse movements, you would check if it is a InputEventMouseMotion. If you want to allow all mouse events, it would be InputEventMouse and so on.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.