My game crashes when two hurtboxes collide: Invalid get index 'knockback_vector'(on base: 'Area2D')

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By adrinx

hi guys, I’ve following the action rpg series from heartbeast and i have this issue: when the enemy hurtbox collides with the grass hurtbox the game crashes, i checked the layers and masks and everything seems right, just like in the tutorials.
this is the link of this particular tutorial: https://www.youtube.com/watch?v=mm4D1ETclAc&list=PL9FzW-m48fn2SlrW0KoLT4n5egNdX-W9a&index=13&ab_channel=HeartBeast

bat script:

extends KinematicBody2D

var knockback = Vector2.ZERO

func _physics_process(delta):
knockback = knockback.move_toward(Vector2.ZERO, 200 * delta)
knockback = move_and_slide(knockback)

func _on_Hurtbox_area_entered(area):
knockback = area.knockback_vector * 120
print(area.name)

grass script:

extends Node2D

func grassEfect():

	var Grasscut = load("res://World/Grasscut.tscn")
	var grasscut = Grasscut.instance()
	var world = get_tree().current_scene
	world.add_child(grasscut)
	grasscut.global_position = global_position

func _on_Hurtbox_area_entered(area):
print(area.name)
grassEfect()
queue_free()

Sword Hitbox Script(where the variable knockback_vector is declared):

extends Area2D

var knockback_vector = Vector2.ZERO

:bust_in_silhouette: Reply From: bloodsign

As you said, when the bat’s collider hits the grass collider, it would throw an error, because
You see on your bat script:

func onHurtboxareaentered(area):
   knockback = area.knockback_vector * 120

The area in that instance would be the grass’s collider. The area entered function of your bat calls knockback_vector, where it doesn’t exist in your grass’s script.

grass.gd

extends Node2D
func grassEfect():

    var Grasscut = load("res://World/Grasscut.tscn")
    var grasscut = Grasscut.instance()
    var world = get_tree().current_scene
    world.add_child(grasscut)
    grasscut.global_position = global_position
func onHurtboxareaentered(area):
	print(area.name)
	grassEfect()
	queue_free()

It works fine when the sword hit box’s area collider hits the bat simply because knockback_vector exists in the swords hit box’s script:

extends Area2D
var knockback_vector = Vector2.ZERO

There are several ways to fix this, first would be to separate the hurtbox of your sword and bat in to their own layer. Say we have the following layers:

Layer 1(world):    
Layer 2(player):  
Layer 3(playerHurtBox):
Layer 4(enemy):
Layer 5(enemyHurtBox): 

and Masks

Mask 1(world):    
Mask 2(player):  
Mask 3(playerHurtBox):
Mask 4(enemy):
Mask 5(enemyHurtBox): 

you could put your grass on the 1st Layer, and remove it from the any Mask Layer.

Layer 1(world):    : Grass

This means that the grass is on layer 1 and it’s not looking in any other Layer(Masks)
Of course, assuming your Grass can be destroyed or attacked. Then if your grass has a HurtBox, you put it in to:

Layer 5(enemyHurtBox): GrassHurtBox

And remove the Grass HurtBox, from any other Masks or Layers.
Then on your bat’s HURTBOX mask and layers.

Layer 5(enemyHurtBox): BatHurtBox

This means that the Bat’s hurtbox(not the bat itself-assuming the bat and it’s hurtbox is separate nodes) layer is only on the Layer 5(enemyHurtBox) only. this should ignore any other colliders not in those mask layer

Then you put the sword hitbox mask/layer into

Mask 5(enemyHurtBox): Sword HitBox

Which means, the playerHitBox will only look for objects/colliders in the Layer 5(Mask: Enemy). And it won’t need to appear in any other layer, because the sword hit box only purpose is to interact with enemyHurtBox colliders or colliders found in that Layer, including the grass.

TDLR: Make’s sure your Grass HurtBox and Bat HurtBax, should not have any MASK layer turned on and should only be on their Hurt Layer.

Thanks for your answer, but… it did’nt work, i have already set the layers and masks as you said, but the bug continues, and its pretty weird because Heartbeast did’nt had this problem in his project, and there are several people with this same issue.

adrinx | 2021-02-22 22:54

Hmm, you are following heartbeast tutorial right? could you post your projecgt zip, I could take a look at it.

bloodsign | 2021-02-23 08:43

:bust_in_silhouette: Reply From: Mr.Meow

Hey there, I’m super new to this but also had this problem. You chose a great tutorial by the way. Been trying to follow it with a spin of my own added to it. Anyway, when two of my assests collided it would cause the same error and crash. I did fix it though. Using the layers method. You may have to check carefully and thoroughly both the collisions on the 2d object as well as the collisions of the Hurtbox on that object. I think the conflict was happening there. Good luck.

i too have the same problem but my game crashes when the swordhitbox collides with batHitbox please can anyone give the correct solution