How can I count an action?

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

How can I count an action, like how many times i pressed “w” or any key :v
Do you understan me??

:bust_in_silhouette: Reply From: jgodfrey

Basically, you just need to monitor for the keys your interested in and increment variables as those keys are pressed. Here’s a simple example:

extends Node2D
var keyAnyCount = 0
var keyWCount = 0

func _input(event):
	if event is InputEventKey and event.pressed and !event.echo:
		keyAnyCount += 1 
		if event.scancode == KEY_W:
			keyWCount += 1
		print("Any count: " + str(keyAnyCount) + ", W count : " + str(keyWCount))

And if i want to do somethinhg like if w us pressed 4 times: something happens
And if i want do doit eith t’he mouse…

ManiCus | 2020-05-02 16:39

Answer to your 1st question:

if keyCount >= 4:
    #Something to happen

Answer to your 2nd question:
var clickCounter = 0

func _input(event):
	if event is InputEventMouseButton and event.is_pressed():
		clickCounter += 1

Traumaticbean | 2020-05-02 17:42