My input event function is not working

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

I’ve just started practicing with Godot Engine, im trying some basic scripts to get used to the syntax,node systems and how everything should work.

I’m doing just some basic script in wich the enemy area emits a signal and identifies the player,if the player press a key ( something similar to attack ) the enemy will die ( get out of the scene ).

Here is my script so far :

 extends KinematicBody2D

#var player = load("res://SCENE_001_PLAYER.tscn")
#var node_player = player.instance()
# getting a node 
# var node = get_node("/root/scene_main_node/node_wanted")
# getting the node of the node
onready var node_player = get_tree().get_root().get_node("GAME").get_node("PLAYER")
onready var area = $Area2D


func _input(event):
	if event.is_action_pressed("ui_ATTACK"):
		print("key for attack was pressed")

func _ready():
	area
 #emits a sign for ENEMY node that something has entered in the Area2D
func _on_Area2D_body_entered(body):
	var object = body
	print(object," entered area")
	if object == node_player :
	#	_on_Area2D_input_event()
		if Input.is_action_just_pressed("ui_ATTACK") :
			print("enemy was attacked")
			queue_free()
	
	#if object == node_player and Input.is_action_pressed("ui_ATTACK") :
	#	print("enemy was attacked")
	#	queue_free()
:bust_in_silhouette: Reply From: klaas

Hi
This wont work.

Those two events have to happen in the exact same moment, the body enters and the key is just pressed.

You want to keep track of the objects entering and leaving the area. When the player hits the attack key every enemy stored as “in the area” are hit.

Hi Klaas,

I’ve tried something as you’re saying, i put the both conditions :

if object == node_player and Input.is_action_just_pressed("ui_ATTACK") :

but didn’t get the results, the area identifies the object when it enters,but the key button isnt working somehow.If you can exemplify i would appreciate, and thanks!

Pedro Henrique | 2021-08-06 18:59