Godot Version
4.3.1
Question
I have used Area2D a lot in this project that I am working on. For some reason this time it does not want to work. So here is my code for my enemy
extends CharacterBody2D
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
#references ti ither nodes
@onready var animated_sprite_2d = $AnimatedSprite2D
@onready var attack_1a = $AnimatedSprite2D/Hitboxes/HozintalAttack/Attack1A
@onready var attack_1b = $AnimatedSprite2D/Hitboxes/HozintalAttack/Attack1B
@onready var attack_1c = $AnimatedSprite2D/Hitboxes/HozintalAttack/Attack1C
@onready var v_attack_a: CollisionShape2D = $AnimatedSprite2D/Hitboxes/VericalAttack/V_Attack_a
@onready var v_attack_b: CollisionShape2D = $AnimatedSprite2D/Hitboxes/VericalAttack/V_Attack_b
@onready var v_attack_c: CollisionShape2D = $AnimatedSprite2D/Hitboxes/VericalAttack/V_Attack_c
@onready var v_attack_d: CollisionShape2D = $AnimatedSprite2D/Hitboxes/VericalAttack/V_Attack_d
@onready var v_attack_e: CollisionShape2D = $AnimatedSprite2D/Hitboxes/VericalAttack/V_Attack_e
@onready var v_attack_f: CollisionShape2D = $AnimatedSprite2D/Hitboxes/VericalAttack/V_Attack_f
@onready var left = $Raycast/Left
@onready var right = $Raycast/Right
@onready var v_timer = $Timers/vTimer
@onready var h_timer = $Timers/hTimer
@onready var can_attack_timer = $Timers/canAttackTimer
@onready var h_detect = $AnimatedSprite2D/Hitboxes/hDetect
@onready var v_detect = $AnimatedSprite2D/Hitboxes/vDetect
#values can be changed in inspector
@export var isFacingRight : bool = false
@export var minion : CharacterBody2D
@export var lengthOfPath : float = 25.0
@export var stayOnPlatform : bool = true
@export var moveSpeed: float = 45.0
#initalizing variables
var minionInitPosition : float = 0.00
var canAttack : bool = true
var canAt : int = 1
var movementDirection = 1
var minionMovement : int
var player : CharacterBody2D
var directionX : float = 0.00
var directionY : float = 0.00
func _ready():
player = get_tree().get_first_node_in_group("Player")
if player == null:
print("Player node not found!")
func _process(delta):
if animated_sprite_2d.flip_h == true:
movementDirection = -1
elif animated_sprite_2d.flip_h == false:
movementDirection = 1
if velocity.x != 0:
animated_sprite_2d.play("Walking")
#if canAt == 0:
#for canAt in range(3600):
#canAt += 1
#if canAt >= 3600:
#canAttack = true
#canAt = 1
func _physics_process(delta):
move_and_slide()
#minionMovement = minion.moveSpeed * minion.movementDirection
directionX = player.global_position.x- minion.global_position.x
directionY = player.global_position.y - minion.global_position.y
_horizantal_attack_collison_position()
_vertical_attack_collison_position()
_on_h_detect_body_entered()
_on_v_detect_body_entered()
if not is_on_floor():
velocity.y = gravity * delta * 100
#facing
if player.global_position.x > minion.global_position.x and player.position.y:
animated_sprite_2d.flip_h = false
elif player.global_position.x < minion.global_position.x and player.position.y:
animated_sprite_2d.flip_h = true
func _on_v_detect_body_entered(body):
print("Help me")
if body.is_in_group("Player"):
minion._vertical_attack()
func _on_h_detect_body_entered(body):
print("Help me")
if body.is_in_group("Player"):
minion._vertical_attack()
When I do this I get an error for the detect_body_entered functions.
The error reads: too few arguments. Expected at least 1 but received 0.
This does not make any sense because the _horizantal_attack_collison_position() and the _vertical_attack_collison_position() functions or any other Area2d had to have an argument there. And when I put player when I call the function, the enemy attacks forever even if the player character is not nearby.
Also I have the collision shapes turned on for debugging and all the collision shapes are shown except for the two connected with the functions that do not work.
So does anyone know how to fix this?