So im trying to make a game for my school project. I follow a tutorial on yt and find error. here is my code. hopefully someone can help me solve this error. thanks 🙂

`extends Area2D
class_name Hitbox

export(int) var damage: int = 1
var knockback_direction: Vector2 = Vector2.ZERO
export(int) var knockback_force: int = 10

onready var collision_shape: CollisionShape2D = get_child(0)

func _init() -> void:

warning-ignore:return_value_discarded

connect("body_entered", self, "_on_body_entered")

func _ready() -> void:
assert(collision_shape != null)

func _on_body_entered(body: KinematicBody2D) -> void:
body.take_damage(damage, knockback_force, knockback_direction)

`

Probably because it's calling it on everything it collides with. Check if the body has that method first

i think i already declared the func in the body.

if u find any mistake in the code, pls tell me. thanks

I changed the tags as this is more asking for help 🙂

Also a tip, when writing multiline code blocks, you could use a triple ` instead of a single.

Ah I'm sorry. We've had a misunderstanding. The 'body' variable. Check it to make sure it is what you think it is when you collide with it before calling 'take_damage' on it.

I'm not at my computer. I can show you the right code later this weekend if you still haven't gotten it.

This is what I meant:

func _on_body_entered(body: StaticBody2D) -> void:
	if body.has_method(&"take_damage"):
		body.take_damage(damage, knockback_force, knockback_direction)

@award The error indicates that for some reason body is null. So your has_method call would just fail in the same way.

    Zini That's a good catch, thanks! The typed parameter value is probably causing that issue. Mine would also have that problem.

    func _on_body_entered(body) -> void:
    	if is_instance_valid(body) && body.has_method(&"take_damage"):
    		body.take_damage(damage, knockback_force, knockback_direction)

    This should perform correctly. This will take any body collided with, regardless of subclass, and call take_damage only if it has take_damage