0 votes

Hi,

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:
        print("Clicked")

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

asked Aug 8 in Engine by Christoffer Schindel (26 points)

2 Answers

0 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()) {
            GD.Print("Clicked");
        }
    }
answered Aug 9 by Christoffer Schindel (26 points)
selected Aug 10 by Christoffer Schindel
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) {
        GD.Print("Clicked");
    }
}

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.

answered Aug 9 by Zylann (14,313 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.