I'm dynamically creating collision polygons from images. Every once in a while the source image just doesn't work and needs to be edited. When this happens I need to know which specific image failed, because I'm creating dozens of these at once.
But try/catch does not catch the error. I assume AddChild() doesn't throw exceptions, and only logs to the Godot console so the whole game doesn't crash just because an AddChild() failed... but now I don't know how I'm supposed to find which instance is causing the problem. Is there a function to check if a polygon is valid before I try to add it to the scene?
The error:
NativeCalls.cs:7293 @ void Godot.NativeCalls.godot_icall_3_828(nint, nint, nint, Godot.NativeInterop.godot_bool, int): Convex decomposing failed!
<C++ Source> core/math/geometry_2d.cpp:54 @ decompose_polygon_in_convex()
<Stack Trace> NativeCalls.cs:7293 @ void Godot.NativeCalls.godot_icall_3_828(nint, nint, nint, Godot.NativeInterop.godot_bool, int)
Node.cs:793 @ void Godot.Node.AddChild(Godot.Node, bool, Godot.Node+InternalMode)
The code:
try
{
// Create polygons...
CollisionPolygon2D col = new() { Polygon = polygon };
AddChild(col); // The line causing the error
}
catch (Exception ex)
{
GD.PrintErr($"Couldn't create CollisionPolygon2D for {Name}"); // This does not fire when the error appears
}