Godot Version
4.3.stable.mono
Question
I am writing a simple game in C# where you need to click on objects to change their state.
I connect the signal to Area2D:
InputEvent += OnInputEvent;
private void OnInputEvent(Node viewport, InputEvent @event, long shapeIdx)
{
if (@event.IsActionPressed("UITouch"))
{
double objCount = Performance.GetMonitor(Performance.Monitor.ObjectCount);
GD.Print(objCount);
}
}
Everything compiles and works.
However, I noticed in the debugger that when moving the mouse over Area2D and clicking on it, objects started to appear:
After reading the internet, I added an explicit indication that the user input has been handled:
private void OnInputEvent(Node viewport, InputEvent @event, long shapeIdx)
{
if (@event.IsActionPressed("UITouch"))
{
double objCount = Performance.GetMonitor(Performance.Monitor.ObjectCount);
GD.Print(objCount);
GetTree().GetRoot().SetInputAsHandled();
}
else
{
GetTree().GetRoot().SetInputAsHandled();
}
}
But objects continue to be created when moving and clicking the mouse.
After that, I tried another option for explicitly indicating that the user input was handled.
private void OnInputEvent(Node viewport, InputEvent @event, long shapeIdx)
{
if (@event.IsActionPressed("UITouch"))
{
double objCount = Performance.GetMonitor(Performance.Monitor.ObjectCount);
GD.Print(objCount);
GetViewport().SetInputAsHandled();
}
else
{
GetViewport().SetInputAsHandled();
}
}
But objects continue to be created when moving and clicking the mouse.
I also tried writing it a bit differently:
private void OnInputEvent(Node viewport, InputEvent @event, long shapeIdx)
{
if (@event is InputEventMouseButton)
{
double objCount = Performance.GetMonitor(Performance.Monitor.ObjectCount);
GD.Print(objCount);
// With different options for explicitly indicating that the user input was handled (GetViewport() // GetTree().GetRoot()).
}
else
{
// With different options for explicitly indicating that the user input was handled (GetViewport() // GetTree().GetRoot()).
}
}
But objects continue to be created when moving and clicking the mouse.
As a result, I decided to try writing this code in GDScript:
extends Area2D
func _ready():
connect("input_event", Callable(_on_input_event))
func _on_input_event(viewport, event, shape_idx):
if event.is_action_pressed("UITouch"):
var objCount = Performance.get_monitor(Performance.Monitor.OBJECT_COUNT)
print(objCount)
Or
func _on_input_event(viewport, event, shape_idx):
if event is InputEventMouseButton:
var objCount = Performance.get_monitor(Performance.Monitor.OBJECT_COUNT)
print(objCount)
And everything is fine! Objects are not created either when clicking or when moving the mouse if written in GDScript!
What am I doing wrong? How do I fix the problem in C#?