Dodge the Creeps - Random location for mobs doesn't work

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ZapticTomato

Hello. I’m currently following the Dodge the Creeps poject and don’t seem to find a solution for the correct spawning of mobs. I do get them to spawn with random angels/rotation and random speeds, but they somehow always start from the up left corner. I seems to not find the FollowPath2D - Node, because it gives me the following error message:

E 0:00:01.840 get_node: (Node not found: “MobPath/MobSpawnLocation” (relative to “/root/Main/MobTimer”).)
<C++ Error> Condition “!node” is true. Returned: __null
<C++ Source> scene/main/node.cpp:1371 @ get_node() @ _on_MobTimer_timeout()

…because of this it also says: Invalid set index ‘unit_offset’ (on base: ‘null_instance’) with value of type ‘float’.

Here is my code for the spawning of the mobs (on the Timer node):

extends Timer

export (PackedScene) var mob_scene

func _ready():

func _on_MobTimer_timeout():
    var mob_spawn_location = $MobPath/MobSpawnLocation
    mob_spawn_location.unit_offset = randf()

    var mob = mob_scene.instance()

    var direction = mob_spawn_location.rotation + PI / 2
    direction += rand_range(-PI / 4, PI / 4)
    mob.rotation = direction

    var velocity = Vector2(rand_range(mob.min_speed, mob.max_speed), 0)
    mob.linear_velocity = velocity.rotated(direction)

My Scene Tree is as follows:

Main(root node, just a simple Node)
…ColorRect (thats the backround, but that shouldnt matter)
…MobPath(Path2D node)
…MobSpawnLocation(PathFollow2D Node)
…MobTimer(the timer node with the script)

I hope this can explain the problem enough… and I thank you very much in advance!

:bust_in_silhouette: Reply From: timothybrentwood
var mob_spawn_location = $MobPath/MobSpawnLocation

should be:

var mob_spawn_location = $"../MobPath/MobSpawnLocation"

If you click and drag the node from the SceneTree into the script editor window it will paste in the node’s NodePath relative to the node that the script is attached to.

$MobPath/MobSpawnLocation would work if the script was attached to your Main (root node).