This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
+1 vote

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 :

Code done in Godot Script

 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()
Godot version Godot_v3.3.2-stable_win64
in Engine by (15 points)

1 Answer

+1 vote

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.

by (4,088 points)

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!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.