Godot Version
v4.3.stable.mono.official [77dcf97d8]
Question
I have two functions: void OnTouch(Vector2I cellPosition)
and void OnHold(Vector2I cellPosition)
. I want to call first one when the player is pressing and releasing a tile, and I want to call second one when player is holding a tile for certain period of time.
This is my implementation:
private Vector2 initialTapPos;
private List<Vector2> touchPositions = new List<Vector2>();
public override void _Input(InputEvent @event)
{
if (@event is not InputEventScreenTouch e)
return;
var tilesetEventPosition = this.LocalToMap(e.Position);
if (!(0 <= tilesetEventPosition.X && tilesetEventPosition.X <= this.Game.Width && 0 <= tilesetEventPosition.Y && tilesetEventPosition.Y <= this.Game.Height))
return;
if (e.IsPressed())
{
this.initialTapPos = e.Position;
touchPositions.Add(e.Position);
Task.Run(() =>
{
OS.DelayMsec(1000);
if (this.touchPositions.Contains(e.Position))
{
OnHold(tilesetEventPosition);
}
});
}
else if (e.IsReleased())
{
if (this.touchPositions.Contains(e.Position))
this.touchPositions.Remove(e.Position);
if (this.initialTapPos == e.Position)
{
OnTouch(tilesetEventPosition);
}
}
}
OnTouch
method works just fine. But OnHold
is not working properly. It at times either doesn’t work at all, or it works but affects a tile in the wrong position. How do I fix this?