Hey,
i am a completely new to Godot and after following the Tutorial (Dodge the Creeps) i've tried to adjust the Code to make something myself. Currently i have a CharacterBody2D that can move around and Mobs that get spawned along a path like it was done in the tutorial. But they are RigidBody2D now. I've made them so, that they follow me around by getting the players position. For this i had to get the player object in a variable in the mob.gd. But i am really confused now, because it worked earlier, but after i changed something in the way, the mobs follow me it does not work anymore, but i did not change something in the way i get the node?

This is the error:

E 0:00:01:0023   mob.gd:8 @ _ready(): Node not found: "../CharacterBody2D" (relative to "/root/Main").
  <C++ Error>    Method/function failed. Returning: nullptr
  <C++ Source>   scene/main/node.cpp:1364 @ get_node()
  <Stack Trace>  mob.gd:8 @ _ready()
                 main.gd:63 @ _on_mob_timer_timeout()

This is my Mob.gd:

extends RigidBody2D
var followSpeed: float = 1000.0
var player: CharacterBody2D
var nextToPlayer = false
# Called when the node enters the scene tree for the first time.
func _ready():
	player = get_parent().get_node("../CharacterBody2D")
	get_node("AnimatedSprite2D").play("default")


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	var direction = (player.global_position - global_position).normalized()
	if player and !nextToPlayer:
		linear_velocity = direction * followSpeed * delta
	else:
		linear_velocity = -direction * followSpeed * delta
	pass


func _on_visible_on_screen_notifier_2d_screen_exited():
	queue_free()


func _on_area_2d_body_entered(body):
	if body.is_in_group("Player"):
		nextToPlayer = true


func _on_area_2d_body_exited(body):
	if body.is_in_group("Player"):
		nextToPlayer = false

And here is a Picture of the main tree (hope this is displayed right).

  • RedPaw Yes, it should be fine. You can make it get_parent().get_node("CharacterBody2D") or just get_node("../CharacterBody2D"), that should be same thing, so you can choose what you like more.

Where mobs are spawned? As child of MobSpawnLocation? It looks more like as child of MobPath maybe.

    GlyphTheWolf Hmm, no, not even as child of MobPath. According to error it have to be directly child of Main node.

    This expression is confusing:
    get_parent().get_node("../CharacterBody2D")

    Is the script Mob.gd attached to the node Main?

      Basically error tells you want to get node with path /root/CharacterBody2D, but proper path would be /root/Main/CharacterBody2D, so either mobs are spawned as child of wrong node or in your code get_parent() is not required before get_node("../CharacterBody2D").

      DaveTheCoder
      The Script Mob.gd is not attached to Main, it is in the Mob Scene that gets spawned along this Mobpath. I also had this in mind, that it would cause errors because its not in the Main scene.

      The Mobs get added like this in the main.gd

      func _on_mob_timer_timeout():
      	var mob = mob_scene.instantiate()
      	var mob_spawn_location = get_node("MobPath/MobSpawnLocation")
      	mob_spawn_location.progress_ratio = randf()
      	mob.position = mob_spawn_location.position
      	add_child(mob)

      so if they are added as a child, in the main scene, it should be right if i write this in my mob.gd?

      player = get_parent().get_node("CharacterBody2D")

      right?

        RedPaw Yes, it should be fine. You can make it get_parent().get_node("CharacterBody2D") or just get_node("../CharacterBody2D"), that should be same thing, so you can choose what you like more.