Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

_UnhandledInput method not working as I expect

I’m new learner of Godot Mono and I couldn’t understand why _UnhandledInput method does not work. I don’t get any error or warnings either.

using Godot;
using System;

public class GameScene : Node2D
{
    PackedScene towerPackedScene;

    public override void _Ready()
    {
        towerPackedScene = (PackedScene)GD.Load("res://Tower.tscn");
    }

    public override void _Process(float delta)
    {
                    
        
    }

    public override void _UnhandledInput(InputEvent @event)
    {
        if (@event is InputEventMouseButton mouseButton)
        {
            GD.Print("clicked"); // does not show message on output
            KinematicBody2D tower = (KinematicBody2D)towerPackedScene.Instance();
            tower.Position = mouseButton.Position;
            this.AddChild(tower);
        }
    }
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Presumably the input is not unhandled. In other words, some other node is taking the input (for example with _input).

You are looking for a mouse event. Is there some Control taking the mouse input? Be aware that Controls will take mouse input before other nodes, even if they are behind them. You need to set their mouse_filter to Ignore.

A common gotcha is using a ColorRect or TextureRect for background, and leave it with mouse_filter set to Stop (which is the default), and then being unable to get input.

See Using InputEvent

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading