GAme crashes after entering an enemy's collision shape 2d

Godot Version

4.6.1

Question

I have a interacting component in my character’s node that code and tree look like this:
extends Node2D

extends Node2D
@onready var interact_label: Label = $InteractLabel

var current_interactions: Array = []
var can_interact: bool = true
func _input(event: InputEvent) -> void:
	if event.is_action_pressed("interact") and can_interact:
		if current_interactions:
			can_interact = false
			interact_label.hide()
			await current_interactions[0].interact.call()
			can_interact = true

#Branie tego triggera któ©y jest bliżej
func _process(_delta: float) -> void:
	if current_interactions and can_interact:
		current_interactions.sort_custom(_sort_by_nearest)
		if current_interactions[0].is_interactable:
			interact_label.text = current_interactions[0].interact_name
			interact_label.show()
	else:
		#interact_label.hide()
		pass	
func _sort_by_nearest(area1, area2):
	var area1_dist = global_position.distance_to(area1.global_position)
	var area2_dist = global_position.distance_to(area2.global_position)
	return area1_dist < area2_dist

func _on_interact_range_area_entered(area: Area2D) -> void:
	current_interactions.push_back(area)


func _on_interact_range_area_exited(area: Area2D) -> void:
	current_interactions.erase(area)



And I have an enemy that code and tree look like this:

extends CharacterBody2D
var speed: int =  45
var chase: bool = false
var player = null
var health: int = 3

###### Interactable Node is REQUIRED for every collision shape that you can enter or the entire game will crash by pressing E for some reason... I'm impressed
func _physics_process(_delta: float) -> void:
	if chase:
		position += (player.position - position)/speed #Player's pos
		


func _on_detection_area_body_entered(body: Node2D) -> void:
	player = body
	chase = true
	


func _on_detection_area_body_exited(body: Node2D) -> void:
	player = null
	chase = false
	

And when I walk into the detections area it throws me this error in interactive_component’s script:

For some time it was working until I was pressing the interact button but now it’s crashing just by touching it with enemy’s collision box with interactive_component’s one.

It looks like you are trying to access is_interactable on the area-node but perhaps the is_interact bool exists in the parent of the area script?

Either way i would rewrite this and put it outside of process in a function you call when you want it to get called. Right now you call it every frame. Probably that is why it crashes the moment you enter such an area. The process function will try to do something before the rest of your code has decided what and how.

I’m not trying… Like I don’t even know why these two are even causing it. I’ll try to do something with it

Have you tried setting the InteractingComponent Area2D collision masks?

If i can see it from the pictures, the Interacting Component’s Area2D is crashing, because when it enters the detection_area, it’s looking for an is_interactable bool.

in your enemy’s code, you said that you have an Interactable Node, which if i can guess is only gonna be used for your Player’s InteractingComponent Area2D.

you can probably set the interacting component’s Area2D collision mask to detect only of what collision layer your Interactable node is.

Also, is the Interactable node an Area2D? what variables does it have?

Here you add Area2D in your current_interactions array.

And this is your interacting component. Do you see where Area2D is in the node tree? The name of that node is InteractRange. So you add those nodes to your current_interactions array. But your script is attached to the parent node (InteractingComponent) of InteractRange (Area2D). And I assume your is_interactable variable is also on this script.

I hope you see the problem now. The node you are adding to the array and the node you ‘think’ you are adding to the array are not the same type of nodes.

One simple solution is adding get_parent() to your code so you can actually access to the node.

		if current_interactions[0].get_parent().is_interactable:
			interact_label.text = current_interactions[0].interact_name
			interact_label.show()

That worked. Do you know also maybe have an idea why does the ai go through the walls?

theese are the collision layers for it’s hitbox layer 1 are walls layer 2 is the player

are you talking about the enemy’s CollisionShape2D?

if the picture your showing me is the CharacterBody2D of the enemy in the inspector, then its collision_layer should be something its own, like enemy, and then the collision_mask should collide with the player, and the walls.

Yes, I’m talking about collision shape 2d I have one and no matter if I set layer to terrain or mask or even both it still pshases through the walls

did you also set the collision layer of your walls that you are referring to?
also, are your walls made of StaticBody2D?

no, wall are made of tile sets with collisions added to them

if they are made of tiles then you should probably check the physics_layer for that particular one.
maybe this picture can help:

just click on the current tile_set that you are using, then make a new Physics Layer.
did you adjust the collision_layer and collision_mask to your preference?

oh, also did you paint them with the collisions? (TileSet - Paint - Paint Properties)

Yes I did paint them like that, but It still doesn’t work

can you remove the collision_mask of the tile set you are currently using, then for the ai’s scene, make the collision_mask detect in the the layer the tile map is in? (1)

Did you also adjust their their shape in Paint Properties?
in the current scene that they are currently instantiated in, Does the ai and the tileset have the proper physics_layer?

For a CharacterBody2D, do not directly change its position. Instead, set its velocity and call move_and_slide().

I don’t really know where to put tht in the code

in your enemy script, apply the velocity just like the last reply said:

the reason the ai was going through the walls was because you were modifying by position and not by velocity, so it’s my fault for not seeing clearly, so sorry.


extends CharacterBody2D
var speed: int =  45
var chase: bool = false
var player = null
var health: int = 3

func _physics_process(_delta: float) -> void:
	if chase:
		velocity += (player.position - position)/speed
      #or                                        
        velocity = (player.position - position).normalized() * speed
    move_and_slide()


func _on_detection_area_body_entered(body: Node2D) -> void:
	player = body
	chase = true
	


func _on_detection_area_body_exited(body: Node2D) -> void:
	player = null
	chase = false

Also, this post is getting out of topic, so feel free to make a new thread or message if you need to ask something.

He is still going through walls and now he is sliding everywherelike on ice

After reading some information I have ended up with this:

func _physics_process(_delta: float) -> void:
	if chase and player:
		var direction = global_position.direction_to(player.global_position)
		velocity = direction * speed 
	else:
		velocity = Vector2.ZERO
	move_and_slide()