How to detect a released key?

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

Really simple here, I am just trying to learn godot.

I am learning the particle, and wish to create them while pressing a key (easy) but can’t manage to free them while releasing the same key.

here is the code

var is_flamethrowing = false

onready var fire_particle = preload("res://fire_particle.tscn")

func _fixed_process(delta):
	if(Input.is_action_pressed("space_bar")):
		fire = fire_particle.instance()
		add_child(fire)
		is_flamethrowing = true
	if(/* TRYING TO RELEASE HERE */):
		remove_child(fire)
		fire.queue_free()
		is_flamethrowing = false

I already tried other thing, such as Input.action_released().
I know my freeing code work, i tested with an other input. I am just surprised that Godot doesn’t implement an “is_action_released()” method.

:bust_in_silhouette: Reply From: bruteforce

Your code creates an instance in every frame if the space_bar is pressed.
IMO it would be better to have only one instance, and you can call it’s set_emitting function with true/false parameter when necessary (I assume that the root node of the fire_particle scene is a Particles/Particles2D).

If you really need multiple instances, then put them in a list, and you can free them later.

About your question: try to use the _input function, and it’s event parameter!
Something like this:

(...)
onready var fire_particle = preload("res://fire_particle.tscn")
onready var fire = fire_particle.instance()

var is_flamethrowing = false

func _ready():
	fire.set_emitting(false)
	add_child(fire)
	set_fixed_process(true)
	set_process_input(true)

func _input(event):
	if(event.is_action_pressed("space_bar")):
		is_flamethrowing = true
	elif(event.is_action_released("space_bar")):
		is_flamethrowing = false
	
func _fixed_process(delta):
	if(is_flamethrowing):
		fire.set_emitting(true)
	else:
		fire.set_emitting(false)
(...)

I figured out alone for the func input(event), thanks anyway ^^ I just don’t get why I have to do that with another function while is action released can work everywhere. Thanks also for the particle part, I ll try that right away, I am still learning it.

mmarramarco | 2017-11-14 14:20