Hi,

I have the following issue. I am working on a project made in Godot, but depended on the time working on my private pc or company pc.

The code which I would like to use is following.

func _input(event):
	 if settings.pause == false:
		mouse_position = event.global_position
		global_x = event.global_position[0]
		global_y = event.global_position[1]

This works perfectly on my private pc and on android but for whatever reasons causing an error on my company pc. Invalid get index 'global_position' (on base: 'InputEventJoypadMotion').

There I have to use this code part (which works on all 3 devices but is more "laggy").

func _input(event):
	if settings.pause == false:
		mouse_position = get_viewport().get_mouse_position()
		global_x = mouse_position[0]
		global_y = mouse_position[1]

I feel like I forgot to enter somewhere something or enable a flag but I can't figure it out.

Thanks for your help.

Hi HolgerM,

I think you should check the event type for InputEventMouseMotion, before handling with it's properties. The error looks like the incoming event is an InputEventJoypadMotion-event. I'm unsure if this event has the property global_position. Maybe the global_position is just empty, so the call of "event.global_position[N]" won't work and you get this index error.

You can try this for working on events of type mouse motion only:


func _input(event):
    if settings.pause == false:
        if event is InputEventMouseMotion:
            mouse_position = event.position
            global_x = event.global_position[0]
            global_y = event.global_position[1]

You can even try this. It's the way I get the mouse position, if the cursor is moved.

func _input(event: InputEvent) -> void:
	if event is InputEventMouseMotion:
		print (event.position)

Cheers Z3R0PTR

Thank you for your answer, I noticed that I also use "if event is InputEventMouseMotion" just a bit below, that why I now added the whole code. With your change, it works on my company pc but on android its very laggy - as its suppose to be an android game this will not work.

I tried making a piano

func _input(event: InputEvent) -> void:
	if settings.pause == false:
		if event is InputEventMouseMotion:
			mouse_position = event.position
			global_x = event.position[0]
			global_y = event.position[1]
			var pos = Vector2(int(global_x/cell_size), int(global_y/cell_size))
			if pos != coord:
				coord = pos
				if coord.y in range(2, 5):
					if coord.x == 1:
						tone = 'c'
						if tone != tone2:
							tone2 = 'c'
#							print('trigger c')
							$Button_c.trigger()
							spawn_particle(mouse_position)
					if coord.x == 2:
						tone = 'd'
						if tone != tone2:
							tone2 = 'd'
#							print('trigger d')
							$Button_d.trigger()
							spawn_particle(mouse_position)
					if coord.x == 3:
						tone = 'e'
						if tone != tone2:
							tone2 = 'e'
#							print('trigger e')
							$Button_e.trigger()
							spawn_particle(mouse_position)
					if coord.x == 4:
						tone = 'f'
						if tone != tone2:
							tone2 = 'f'
#							print('trigger f')
							$Button_f.trigger()
							spawn_particle(mouse_position)
					if coord.x == 5:
						tone = 'g'
						if tone != tone2:
							tone2 = 'g'
#							print('trigger g')
							$Button_g.trigger()
							spawn_particle(mouse_position)
					if coord.x == 6:
						tone = 'a'
						if tone != tone2:
							tone2 = 'a'
#							print('trigger a')
							$Button_a.trigger()
							spawn_particle(mouse_position)
					if coord.x == 7:
						tone = 'h'
						if tone != tone2:
							tone2 = 'h'
#							print('trigger h')
							$Button_h.trigger()
							spawn_particle(mouse_position)
					if coord.x == 8:
						tone = 'c2'
						if tone != tone2:
							tone2 = 'c2'
#							print('trigger c2')
							$Button_c2.trigger()
							spawn_particle(mouse_position)
		else:
			coord = Vector2()
			tone2 = 'none'

Hi,

if your target platform is Android you could try to work with the InputEventScreenTouch. All events will be translated from the platform related apis to the godot event system. Maybe the InputEventMouseMotion will be translated from an InputEventScreenTouch additionally, so the usage of it will slow down anything on Android (or other touch related devices). It's just an assumption. I am relatively new to Godot and never worked with Android, but your code looks ok for me. What if you try to use the Buttons and "catch" their input in a parented nodes script. If they are real UI based nodes (can't see it in your code), they can handle their input. You can listen to their signals within the script to execute whatever you want.

a year later

hey i had a similar / same problem. the reason was that my controller was plugged in and godot automatically processed the event as joypad input, not as mouse input. once i unplugged the joypad the project stopped crashing...

2 years later