How do I add touch support to my game?

Godot Version

4.3

Question

In my first Godot game, I have 3 different UI screens (MainMenu, PauseMenu, GameOverMenu) and a game scene where user only has 1 action (rotation).

In Project Settings > Input Map I only have 1 input (rotation - Spacebar)
In Project Settings > Input Devices > Pointing > emulate_touch_from_mouse is turned on and emulate_mouse_from_touch is turned off.

When I launch the game, with LMB I can navigate through different Menus and also do action with a LMB indicating that the game either has touch support already or LMB is the default input I can’t remove (although it’s not in the Input Map).

In my player.gd I have

func _input(event: InputEvent) -> void:
	if Input.is_action_just_pressed("Rotate"):
		if Animation_Player.is_playing():
			return
		Rotate()
		
	if event is InputEventScreenTouch:
		if event.pressed:
			if not $"/root/Game/ActiveUI/MarginContainer/HBoxContainer/PauseButton".get_global_rect().has_point(event.position):
				if Animation_Player.is_playing():
					return
				Rotate()

Now, the whole idea of touch input here is that the Menu buttons work as usual and once the main game scene starts, user input anywhere on the screen (except the PauseButton) would cause action and landing on PauseButton would call the PauseMenu and pause the game.

Part of pause_button.gd

func _ready() -> void:
	pressed.connect(_on_pause_button_pressed)

func _input(event: InputEvent) -> void:
	if event is InputEventKey and event.keycode == Key.KEY_ESCAPE and event.pressed and not event.is_echo():
		_on_pause_button_pressed()
		accept_event()
		
func _gui_input(event: InputEvent) -> void:
	if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
		if event.pressed:
			_on_pause_button_pressed()
		accept_event()

Exporting the game to itch io and trying on mobile I couldn’t get past the MainMenu screen. Is there something else? Do I need to create a whole overlay of ‘touch buttons’ for every single menu button? How would I connect them? Do I need a completely seperate project that would handle only touch input in order for this to be a mobile game?

Are the buttons Button or TouchScreenButton? If they are normal buttons and emulate_mouse_from_touch is off, then you won’t be able to click the buttons with your fingers on a touch screen.

I used Button Node. I fixed the issue by implementing the following code into all of the scripts that are related to scenes that should have touch support (player.gd, mainmenu.gd, pausemenu.gd, gameovermenu.gd etc.)

func _input(event: InputEvent) -> void:
	if event is InputEventScreenTouch:
		if event.pressed:
			_handle_touch(event.position)

func _handle_touch(touch_position: Vector2) -> void:
	for child in get_children():
		if child is Button:
			if child.get_global_rect().has_point(touch_position):
				child.emit_signal("pressed")
				break

Yeah you can do that, but you can also select your button, look at inspector, go to node, and press one of the signals: pressed(), and it’ll add a new function that activates when you press the button

Couldn’t you just turn on emulate_mouse_from_touch? Or does that not work on web mobile?

That setting is indeed on mobile, i work on mobile

@isabelle_alejar look at inspector, go to node, and press one of the signals: pressed()

I originally did that since I needed functions for my buttons but couldn’t make them touch sensitive. I am on PC though.

@paintsimmon Couldn’t you just turn on emulate_mouse_from_touch? Or does that not work on web mobile?

I did turn that on eventually. Initially, that setting didn’t really make sense for me as I thought it was only for mobile and that it was pointless in my case. I think that emulation is just within the project, when the game is exported the emulation won’t happen so it is pointless for me to do it because I’m on PC and don’t have touch screen.

1 Like