Area2D needs an argument?

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?

Can you show the error in the editor and which line is being pointed?

The problem is you can’t call this functions without an argument, if this function is connected to the signals you shouldn’t call them, they will be called automatically when the signal is triggered.

1 Like

Thanks for the help! I thought that could be the case. When I don’t call the functions, the Area2D fails to work. Do you have any idea why that could be the case?

How does it work? I feel like syntax errors are not your intended working scenario. What happens vs what do you expect to happen is very important.

Without the empty calls, does “Help me” ever print? In the script editor do you have a green connected symbol next to the function? Is your player on the same collision layer as the area’s mask?

I basically want a hitbox that if the player enters the area2d the attack will happen. And yes to both of those last questions.

I did not send the whole code. Would it be helpful if I sent the whole thing?

Probably not necissary to send the whole script. I do notice both of these use vertical attack by the way.

If “Help me” isn’t printed then the connection is not made properly, maybe some advanced connect properties are preventing it from establishing, make sure to check your errors and warnings in the debugger, from the last screenshot you have 7 present.

I checked all the errors they have nothing to do with the enemy. Is there any way I can manually make the connections in the code? Or any other suggestions?

Nevermind I figured it out. I was being dumb. I was not moving the hitboxes. I think I’m brain dead :rofl: