NavigationAgent2d throws errors despite working

Godot Version

v4.2.1-stable_mono_win64

Question

Hello, I am trying to make a character pathfind to the mouse position, and I have managed to make it work, but it throws a couple hundred NullReference errors a second while doing it.


This is the script I am using for pathfinding:

using Godot;
using System;

public partial class KnWarriorBlue : CharacterBody2D
{
	[Export] public NavigationAgent2D navigationAgent2D;

	public Vector2 direction;
	public int speed = 200;
	public double accel = 5.0;
	public Vector2 mousePos;

	public override void _Ready() {
		GD.Print(navigationAgent2D.GetCurrentNavigationPath(), "navigationAgent2D.GetCurrentNavigationPath()"); // this errors out
		GD.Print(GetTransform(), "GetTransform()");
		GD.Print(GetGlobalMousePosition(), "GetGlobalMousePosition()");
		GD.Print(navigationAgent2D.TargetPosition, "(navigationAgent2D.TargetPosition");
		GD.Print(navigationAgent2D.GetNextPathPosition(), "navigationAgent2D.GetNextPathPosition()");
		GD.Print(GlobalPosition, "GlobalPosition");
	}

	public override void _Process(double delta) {
		navigationAgent2D.TargetPosition = GetGlobalMousePosition(); // this errors out

		direction = navigationAgent2D.GetNextPathPosition()- GlobalPosition;
		direction = direction.Normalized();

		Velocity = Velocity.Lerp(direction * speed, (float)(accel * delta));

		MoveAndSlide();
	}

}

The export field is assigned a NavigationAgent2D that is a child node of the CharacterBody2D.
Stuff in ready I just use for debugging, to see if I get errors trying to call the methods. The errors in debug open the script on lines 14 and 23 (the first lines of ready and process).
The line in the process that throws an error seems to get executed exactly half the time, when I used GD.Print in the process before the line to check the values of TargetPosition and GetGlobalMousePosition it only printed TargetPosition skipping a frame.
Screenshot_255
I don’t know what goes wrong, would greatly appreciate any help.

Not sure about C# specific shenanigans but in GDScript you can not just export any node like that because that just turns it into a NodePath. A NodePath can be used to try to receive the Node behind it, e.g. with get_node(), but by itself a NodePath is just a fancy string path for the SceneTree.

The thing is, it does work the way I wanted it to, it sets the target path and the direction correctly, and pathfinds fine when I launch it, just that it does that exactly half the time. I think in C# the export must work like I thought it did, because otherwise it wouldn’t work at all, right?
I thought it may be breaking due to speed for some reason, but it also breaks on _Ready despite being called just once, so it can’t be that.

I am a dum-dum and had the script not just attached to the CharacterBody2D but also in autoload where it obviously didn’t have any navigation agents attached to it, removing it from there fixed everything

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.