0 votes

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()
MobTimer.gd:10 @ _on_MobTimer_timeout()

...because of this it also says: Invalid set index 'unitoffset' (on base: 'nullinstance') 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!

Godot version 3.3.4
in Engine by (18 points)

1 Answer

0 votes
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).

by (3,870 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.