How to properly use InputEventScreenTouch for Mobile

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Nano95

Hello everyone,

I’m new to Godot. I began trying to move my project from Unity over to Godot and im slowly making progress. I am finally able to export to my Android phone without an App not installed error!

However the moment I began testing on my phone I noticed that tapping wasa not doing anything. When i tap, a monster should be spawned at the top of the screen (the monster then moves toward the player – but that’s aside from the point)

So originally, what I had was:

func _input(event):
	if (event.is_action_pressed('click') or event.is_action_pressed('touch')):
		var scene_instance = scene.instance()
		scene_instance.set_name("enemy")
		scene_instance.position.x += rand_range(0, get_viewport_rect().size.x)
		scene_instance.position.y -= 40
		add_child(scene_instance)

Where touch is defined in the Input map and it has Device0 - left pressed

This works fine on my laptop. I click with my mouse on the game screen and then a monster spawns. However, when I try it with my phone… nothing happens. So i did some research and found InputEventScreenTouch.

So then I had this:

func _input(event):
	if (event.is_action_pressed('click') or event.is_action_pressed('touch')):
		var scene_instance = scene.instance()
		scene_instance.set_name("enemy")
		scene_instance.position.x += rand_range(0, get_viewport_rect().size.x)
		scene_instance.position.y -= 40
		add_child(scene_instance)
	if event.InputEventScreenTouch.is_pressed() == true:
		var scene_instance = scene.instance()
		scene_instance.set_name("enemy")
		scene_instance.position.x += rand_range(0, get_viewport_rect().size.x)
		scene_instance.position.y -= 40
		add_child(scene_instance)

So I do this on my computer and the moment my mouse enters the game screen, it crashes with this error:

Invalid get index ‘InputEventScreenTouch’ (on base: ‘InputEventMouseMotion’).

Well that will be inconvenient to keep testing on my laptop if i have to comment that event check and then uncomment it out each time I want to export it.

So i try on my phone, and as soon as I tap… the game crashes… So im assuming that maybe im just doing something wrong.

Does anyone have any suggestions? I’m still learning so maybe it’s something silly that i am missing.

Thanks,


UPDATE:
Turns out the issue was actually how I was loading my scene to spawn the enemies. As soon as i deleted the code to instantiate the scene the crashes were gone.

I messed around with the way I spawn and it worked.

this is the working code:

var scene = load("res://Scenes/Enemy.tscn")
var count = 0
onready var taplbl = $tapLabel

func _unhandled_input(event):
	if event is InputEventScreenTouch:
		if event.is_pressed():
			count += 1
			taplbl.text = str(count)
			var scene_instance = scene.instance()
			scene_instance.position.x += rand_range(0, get_viewport_rect().size.x)
			scene_instance.position.y -= 40
			add_child(scene_instance)

Your solution is not working for me for the same scenario.

Your solution is

func _unhandled_input(event):
    if event is InputEventScreenTouch:
        if event.is_pressed():

The above code is not working for mobile TAP action.

HERE IS MY WORKING SOLUTION.

func _input(event):
    if event is InputEventScreenTouch:
	    if event.is_pressed():

anbarasu chinna | 2020-09-12 12:53