Bool seemingly flipping itself between InputEvents?

Godot Version

4.2.1

Question

My game has a small mini-system that allows the player to make crude drawings. To do this, I have a bool that tracks LMB clicks/releases on _input() to determine whether the pen is ‘down’ (ie making contact with paper). If the pen is down, mouse motion events should push event.positions onto an array for _draw() to process.

However, with the below code, all works well for about 10-20 seconds. Then the program will ‘miss’ a LMB-release, and the pen stays permanently down. I have tried printing the state of the bool to log. The LMB-release is working, but one frame later when the next mouse motion event comes in, the bool has flipped back, for seemingly no reason, and the program believes the pen is down. No amount of clicking/releasing will cause the ‘pen-up’ state to properly re-establish itself.

It’s tough to use the debugger for something like this… Anyone have any idea what’s going on here? Many thanks for any advice!

func _input(event):
	if manager.active_tool == manager.TOOLS.PENCIL:
		if (event is InputEventMouseButton):
			if (event.is_pressed() and event.button_index == MOUSE_BUTTON_LEFT):
				print("Pencil Selected, Sheetrock LMB pressed")
				is_pencil_pressed = true
				marks_tool_pencil.push_back(to_local(event.position))
				get_viewport().set_input_as_handled()
				queue_redraw()
			if (event.is_released() and event.button_index == MOUSE_BUTTON_LEFT):
				print("Pencil Selected, Sheetrock LMB Released")
				is_pencil_pressed = false
				print("is_pencil_pressed: " + str(is_pencil_pressed))
				get_viewport().set_input_as_handled()
		if (event is InputEventMouseMotion and is_pencil_pressed):
			print("MME is pencil pressed? " + str(is_pencil_pressed))
			marks_tool_pencil.push_back(to_local(event.position))
			get_viewport().set_input_as_handled()
			queue_redraw()

have you tried elif here?

2 Likes

Ah, that’s a good idea, but I already re-worked the system to be something a little more placeholder for now. I’ll come back to this in a while and let you know how it went. Thanks!