Godot Version
4.2.1
Question
I try to spawn an object near another object, so I use NavigationServer3D.MapGetClosestPoint to get it, but when it runs it gives out an error that the parameter map is null, but it can’t be null.
Here’s my script:
Node3D target;
NavigationRegion3D navMesh;
public override void _Ready()
{
target = GetTree().Root.GetNode("Game").GetNode("Map").GetNode("floor1").GetNode("objects").GetNode("grabber").GetNode<Node3D>("Target");
navMesh = GetTree().Root.GetNode("Game").GetNode("Map").GetNode("floor1").GetNode<NavigationRegion3D>("NavRegion");
}
public override void _Process(double delta)
{
Vector3 movePos = NavigationServer3D.MapGetClosestPoint(navMesh.GetRid(), target.Position);
this.GetParent<CharacterBody3D>().Position = movePos;
}
Note that it doesn’t run every frame, I deleted some of the code that wasn’t needed to answer my questions.
Could anybody help me out?
navMesh = GetTree().Root.GetNode("Game").GetNode("Map").GetNode("floor1").GetNode<NavigationRegion3D>("NavRegion");
Does that even return a valid node instance?
Also im stupid, I didn’t add the 2 errors:
E 0:00:11:0299 NativeCalls.cs:6100 @ Godot.Vector3 Godot.NativeCalls.godot_icall_2_683(nint, nint, Godot.Rid, Godot.Vector3*): Parameter “map” is null.
<C+±Quelle> modules/navigation/godot_navigation_server.cpp:241 @ map_get_closest_point()
NativeCalls.cs:6100 @ Godot.Vector3 Godot.NativeCalls.godot_icall_2_683(nint, nint, Godot.Rid, Godot.Vector3*)
NavigationServer3D.cs:274 @ Godot.Vector3 Godot.NavigationServer3D.MapGetClosestPoint(Godot.Rid, Godot.Vector3)
eddie.cs:45 @ void eddie._Process(double)
Node.cs:2111 @ bool Godot.Node.InvokeGodotClassMethod(Godot.NativeInterop.godot_string_name&, Godot.NativeInterop.NativeVariantPtrArgs, Godot.NativeInterop.godot_variant&)
eddie_ScriptMethods.generated.cs:68 @ bool eddie.InvokeGodotClassMethod(Godot.NativeInterop.godot_string_name&, Godot.NativeInterop.NativeVariantPtrArgs, Godot.NativeInterop.godot_variant&)
CSharpInstanceBridge.cs:24 @ Godot.NativeInterop.godot_bool Godot.Bridge.CSharpInstanceBridge.Call(nint, Godot.NativeInterop.godot_string_name*, Godot.NativeInterop.godot_variant**, int, Godot.NativeInterop.godot_variant_call_error*, Godot.NativeInterop.godot_variant*)
the above one is fixed, and
E 0:00:11:0300 object System.Runtime.CompilerServices.CastHelpers.ChkCastAny(System.Void*, object): System.InvalidCastException: Unable to cast object of type ‘Godot.Node3D’ to type ‘Godot.CharacterBody3D’.
<C+±Fehler> System.InvalidCastException
<C+±Quelle> :0 @ object System.Runtime.CompilerServices.CastHelpers.ChkCastAny(System.Void*, object)
:0 @ object System.Runtime.CompilerServices.CastHelpers.ChkCastAny(System.Void*, object)
NodeExtensions.cs:182 @ T Godot.Node.GetParent()
eddie.cs:46 @ void eddie._Process(double)
Node.cs:2111 @ bool Godot.Node.InvokeGodotClassMethod(Godot.NativeInterop.godot_string_name&, Godot.NativeInterop.NativeVariantPtrArgs, Godot.NativeInterop.godot_variant&)
eddie_ScriptMethods.generated.cs:68 @ bool eddie.InvokeGodotClassMethod(Godot.NativeInterop.godot_string_name&, Godot.NativeInterop.NativeVariantPtrArgs, Godot.NativeInterop.godot_variant&)
CSharpInstanceBridge.cs:24 @ Godot.NativeInterop.godot_bool Godot.Bridge.CSharpInstanceBridge.Call(nint, Godot.NativeInterop.godot_string_name*, Godot.NativeInterop.godot_variant**, int, Godot.NativeInterop.godot_variant_call_error*, Godot.NativeInterop.godot_variant*)
Vector3 movePos = NavigationServer3D.MapGetClosestPoint(navMesh.GetRid(), target.Position);
According docs, is <NavigationRegion3D>.GetRegionRid()
(get_region_rid
for gdscript) for godot 4
System.InvalidCastException: Unable to cast object of type ‘Godot.Node3D’ to type ‘Godot.CharacterBody3D’.
Sounds like this.GetParent<CharacterBody3D>()
is invalid?
I referenced the CharacterBody3D incorrectly.
Replacing navMesh.GetRid() with navMesh.GetRegionRid() didn’t solve the issue, same error code unfortunately.
sancho2
February 26, 2024, 12:58am
7
I think the error is telling you that the parent here is not a CharacterBody3D.
I would check that the parent is what you think it is.
This is not necessarily related to your posted issue.
Without a shadow of doubt, these GetNode chains are a problem waiting to happen.
This screams “design flaw” and surely there is a better way to handle this.