How do I fix Invalid get index 'button_index' (on base: 'InputEventMouseMotion').

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

I have a container that has other control elements in it that when I scroll it will scroll through the contents smoothly, for the most part when I start the program the scrolling works fine, but after a while or in some cases almost immediately it will crash with the error:
Invalid get index ‘button_index’ (on base: ‘InputEventMouseMotion’).

I am assuming that the problem has something to do with vectors conflicting with the x and y values that are apart of InputEvenMouseButton.

I am still not very sure what match is or what it does, but without it, it doesn’t seem to work any other way.

Thank you in advance :slight_smile:

Here is my code:

extends Container

var v = Vector2(0,0) #current velocity
var just_stop_under = 1
var multi = -4 #speed of one input
var is_grabbed = false

func _process(delta):
	v *= 0.9
	if v.length() <= just_stop_under: v = Vector2(0,0)
	$Origin.rect_position += v
	if $Origin.rect_position.y < -709.8:
		$Origin.rect_position.y = -709.8
	if $Origin.rect_position.y > 0:
		$Origin.rect_position.y = 0

func _gui_input(event):

	#if event is InputEventMouseButton:
		#match event.button_index:
			#BUTTON_MIDDLE:  is_grabbed = event.pressed

	if event is InputEventMouseButton and $Origin.rect_position.y > -709.9 or $Origin.rect_position.x < 0.1:
		match event.button_index:
				v.y += multi
				v.y -= multi
:bust_in_silhouette: Reply From: jgodfrey

I think the logic here isn’t working as you intend due to a lack of parens:

if event is InputEventMouseButton and $Origin.rect_position.y > -709.9 or $Origin.rect_position.x < 0.1:

I assume that needs to be:

if event is InputEventMouseButton and ($Origin.rect_position.y > -709.9 or $Origin.rect_position.x < 0.1):

Note, I just added a set of parens. Without those around the position checks, you’ll get inside that if block unexpectedly…

Specifically, your logic is essentially this:

if thing_one and thing_two or thing_three

Without parens, that’ll be TRUE in either of these cases:

  • thing_one and thing_two are TRUE -or-
  • thing_three is TRUE

With the parens, the logic only returns true in this one case:

  • thing_one is TRUE and (either thing_two or thing_three is TRUE)