0 votes

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 physicsprocess(delta):
knockback = knockback.movetoward(Vector2.ZERO, 200 * delta)
knockback = move
and_slide(knockback)

func onHurtboxareaentered(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 onHurtboxareaentered(area):
print(area.name)
grassEfect()
queue_free()

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

extends Area2D

var knockback_vector = Vector2.ZERO

in Engine by (12 points)

2 Answers

0 votes

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.

by (334 points)
edited by

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.

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

0 votes

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.

by (14 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.