How can I check if mouse button is pressed after double click?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Apple Horo
:warning: Old Version Published before Godot 3 was released.

Hello there :3

Today I was working on SMG for player to shoot, and get some strange mouse behaviour.

My code is following:

var shoot_cool = 0.0;

...

func _input( event ): # fails on double click
	if( event.is_action_pressed( "shoot" ) ):
		shoot();

func _process( delta ):
	if( shoot_cool > 0 ):
		shoot_cool -= delta;
	else:
		if( Input.is_action_pressed( "shoot" ) ):
			shoot();

...

It works perfect for the keyboard input, and mostly fine for the mouse, except for when I do a double click, it shoot two times and stop shooting, even if the mouse button is still pressed.

So, did I missed something about double click handling or stuff?

I’m having the same problem, except I’m working in 2D with _unhandled_input and a timer to space shots. I press twice quickly, it shoots twice and then Input.is_actualy_press("fire") on where fire is bound to LMB reads false. If I extend the time between shots and press twice farther apart, it works as expected.

Quantum | 2017-07-10 23:53

:bust_in_silhouette: Reply From: Grass H0PEr

WHOAH!

Yeah this seems like a bug.

  ================================================
  Used your code, got the same buggy results in Godot 2.1.4.
  Used your code, and it worked correctly in Godot 3.0 alpha :slight_smile:

  The following is for 2.1.4 :
  ================================================

Upon holding Left Mouse button (after double-click)
it is not returning true for
Input.is_action_pressed( "my_action" )

but after that, when you release Left Mouse it does return true for
event.is_action_released( "my_action" )
 

Compare to using this same code
holding single-click or holding space bar :
returns true for .is_action_pressed( "my_action" )
returns true for .is_action_released( "my_action" )
 

 ----------------------------------------------
Link to .gif on Imgur
Recap of Apple Horo’s problem
enter image description here

 

 ----------------------------------------------
Link to .gif on Imgur
Here is a workaround.
An alternate way to script this in Godot 2.1.4
enter image description here

 

 ----------------------------------------------
Here’s the code from the 2nd .gif
One way to work around this.

Requires Left Mouse Button added to Input Map for “shoot”

# For Godot 2.1.4
extends Node

func _ready():
	set_process_input(true)
	set_process(true)

var shoot_cooldown = 0.0;
var shoot_held_down = false

func _input( event ): # bug workaround
	if( event.is_action_pressed( "shoot" ) ):
		shoot_held_down = true
		shoot();
	if( event.is_action_released( "shoot" ) ):
		shoot_held_down = false

func _process( delta ):
	if( shoot_cooldown > 0 ):
		shoot_cooldown -= delta;
	else:
		if( Input.is_action_pressed( "shoot" ) 
		    or shoot_held_down 
		):
			shoot();

func shoot() :
	print( "lazr -- pew pew" )
	shoot_cooldown = 1

I added this to a Control node in the .gif,
but it’ll work with any Node subclass

  -----------------------------------------------------------------------
  How to format .gd scripts you find online
  to fill in the TAB characters
  -----------------------------------------------------------------------
    1.   Select a tab character in Godot → Ctrl+C
    2.   Ctrl+R → replace 4 spaces with the Godot with Ctrl+V tab character
    3.   click “Replace All” a few times

      ~ have a wonderful day ~

Grass H0PEr | 2017-10-21 20:55