I have difficulties with InputEventScreenTouch

Godot Version

4.2.2

Question

I want to create a button just for Android devices for my Pong game.
I have a plugin that allows 2 players to move their paddles on one device at the same time (the plugin was only available as a virtual joystick, I am now trying to create the button myself). But it doesn’t work properly with the button because all players have to let go and then only this one button can be pressed for it to work. I probably won’t get very far with InputEventMouseButton, but it is currently the only reason why the button works at all. If I use InputEventScreenTouch, it somehow doesn’t work at all.
I have attached my script to Control and the node has a TextureRect as a child, and I have subordinated Control to a Canvas_Layer so that it doesn’t move with the player.
Here is a screenshot and my code so far.
Screenshot 2024-06-08 110310

extends Control

class_name VirtuellControllerButton

var click_radius = 300
signal pressed


func _input(event):
	if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
		if (event.position - $TextureRect.position).length() < click_radius:
			pressed.emit()


func _process(delta):
	if not DisplayServer.is_touchscreen_available():
		hide()

Try using a TouchScreenButton instead.

I don’t think you can have multiple InputEventMouseButtons at the same time. You’ll need to use an InputEventScreenTouch event. If it’s not working while testing it on desktop is probably because you need to enable Emulate Touch From Mouse in the project settings under Input Devices / Pointing

Yes, you are right, but apparently I was not clear enough about what my problem is.

I have already set up the project settings correctly, I understand how a MouseButton works, but I don’t know how to change the code so that the TouchScreenButton actually works. I’ve already tried that, but the button doesn’t respond even on the cell phone.

So I don’t know how to write the code. Below is an example that doesn’t work. And I don’t understand anything about the index and how it is used.

Thank you very much if someone can help me again.

func _input(event):
	if event is InputEventScreenTouch:
		if (event.position - $TextureRect.position).length() < click_radius:
			pressed.emit()

The problem is probably because the input event position is in a different coordinate system than the TextureRect position

You can implement the Control._gui_input() in the TextureRect script or connect the signal Control.gui_input of that TextureRect to another script. This way only when the input event is inside the TextureRect it will call the function or fire the signal and it will automatically transform the input event position to be relative to the Control position.