hello again, i have this problem where if i try to spawn my enemy using a node 2d and area 2d it breaks the game, the enemys target(the player) needs to be assigned in the editor and i tried putting $player but the enemy doesnt work and shows error, how do i spawn and still make the enemy follow the player.
heres the code;
@export var enemy: PackedScene
func _ready():
$Area2D.body_entered.connect(_on_indicator_body_entered)
func _on_indicator_body_entered(body):
if body.name == "Player":
print("Player entered indicator")
if enemy:
var new_enemy = enemy.instantiate()
new_enemy.global_position = global_position
get_tree().current_scene.add_child(new_enemy)
You know the tab where you link signals? Well there’s a button at the top labelled groups. In there you can create a new global group named “Player”, assign the first node in your player scene to that group, then either get a reference to that as a var player = get_first_node_in_group(“Player”) or swap your existing if statement to if body.is_in_group(“Player”):
Typing from my phone so the code might not be exactly correct but these are the phrases you want to look for in the documents that were linked.
Look like you spawner generate an infinite number of enemy. You just test if your enemy variable is assign and generate a new one at the spawner location who trigger the body detection who then again generate a new enemy and so one.