+2 votes


I'm trying to figure out how to detect what is being clicked on... it's easy to find solutions for this online, e.g.:

extends Area2D

func _input_event(viewport, event, shape_idx):
    if event.type == InputEvent.MOUSE_BUTTON \
    and event.button_index == BUTTON_LEFT \
    and event.pressed:

But how would that get converted to C#? I've been trying to play around with it and can't seem to find the same arguments (event and shape_idx specifically).

Could anyone lighten this up? :)

in Engine by (120 points)

2 Answers

+3 votes
Best answer

With the help of Zylann, I came up with this:

private void _on_Area2D_input_event(Viewport viewport, InputEvent @event, int shape_idx)
        if (@event is InputEventMouseButton btn && btn.ButtonIndex == (int)ButtonList.Left && @event.IsPressed()) {
by (120 points)
selected by
0 votes

The code you found was for Godot 2. It has changed in Godot 3.

In C# it would be something like:

void _InputEvent(Viewport viewport, InputEvent ev, int shape_idx) {

    var btn = ev as InputEventMouseButton;

    if(btn != null && ev.ButtonIndex == ButtonList.Left && ev.Pressed) {

Note: maybe you will need to cast ButtonList.Left to int, but I'm not sure since I don't use C# in Godot yet.

by (27,966 points)

will try this asap :) thanks

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