31 / GodotOnReady

A C# Source Generator that adds convenient onready-like features to your C# scripts in Godot Mono (3.x) without any reflection.
https://www.nuget.org/packages/GodotOnReady
MIT License
123 stars 13 forks source link

Throw an enhanced exception for node type mismatch #25

Open 31 opened 3 years ago

31 commented 3 years ago

The generator currently makes something like this:

[OnReadyGet("Face")] private Spatial _face;
...
if (FacePath != null)
{
    _face = GetNode<global::Godot.Spatial>(FacePath);
}
if (_face == null)
{
    throw new NullReferenceException($"'{((Resource)GetScript()).ResourcePath}' member '_face' is unexpectedly null in '{GetPath()}', '{this}'. Ensure 'FacePath' is set correctly, or set [OnReadyGet(OrNull = true)] to allow null.");
}

The GetNode<global::Godot.Spatial>(FacePath) can throw, and it doesn't include much info. I think it would be useful to use GetNodeOrNull(FacePath) and do the checks on our side, so we can throw a detailed exception if it doesn't work.

A specific situation I want to improve is when the target node is the wrong type. It can be hard to think of this as the root cause, sometimes, and more details would help.

(This is what I was attempting to do with the generated throw, but it only helps in very specific situations.)