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
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.
Have you tried setting the InteractingComponentArea2D 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 InteractingComponentArea2D.
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()
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.
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)
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?
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.
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()