Attack code error

Godot Version

4.1.2

Question

I keep getting this error when i try to make my slime take damage. Attempt to call function ‘has_method’ in base ‘null instance’ on null instance
here is my script

extends CharacterBody2D

var speed= 40
var Player_chase = false
var Player = null
var health = 100
var player_inattack_zone = false
var can_take_damage = true

func _ready():
$AnimatedSprite2D.play(“Idle”)

func _on_detection_area_body_entered(body):
Player = body
Player_chase = true

func _on_detection_area_body_exited(body):
Player = null
Player_chase = false

func _physics_process(delta):
deal_with_damage()

if Player_chase:
	position += (Player.position - position)/ speed
	$AnimatedSprite2D.play("Walk")
	
	if(Player.position.x - position.x) < 0:
		$AnimatedSprite2D.flip_h = true
	else:
		$AnimatedSprite2D.flip_h = false
	
else:
	$AnimatedSprite2D.play("Idle")

func slime():
pass

func _on_slime_hitbox_body_entered(body):
if body.has_method(“player”):
player_inattack_zone = true

func _on_slime_hitbox_body_shape_exited(body_rid, body, body_shape_index, local_shape_index):
if body.has_method(“player”):
player_inattack_zone = false
func deal_with_damage():
if player_inattack_zone and Gobal.player_current_attack == true:
if can_take_damage == true:
health = health - 20
$take_damage_cooldow.start()
can_take_damage = false
print("slime health = ", health)
if health <= 0:
self.queue_free()

func _on_take_damage_cooldow_timeout():
can_take_damage = true

func _on_slime_hitbox_body_entered(body):
	if body:
		if body.has_method(“player”):
			player_inattack_zone = true

func _on_slime_hitbox_body_shape_exited(body_rid, body, body_shape_index, local_shape_index):
	if body:
		if body.has_method(“player”):
			player_inattack_zone = false

ok thanks it works but could you maybe explain how it works

it’s still strange how an object can be detected by area2d when its collision detected something but having its body to be null or not referenced.

what you can test to see if any other body entered the area2d by print(body) or print(body.name)

this line of code basically checking if the body exists or not null

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.