Add a fallback way to check if a returned Object is null
raniejade opened this issue · 0 comments
GDNative's api.json
doesn't give any information if a method would return null or not, the binding at the moment assumes that null will never returned hence why we always return a non-nullable type. If Godot returns null, our binding will crash here.
Our current plan to avoid this is to maintain a list of methods that are known to return null, this data will supplement api.json
when we are generating the engine types. However, due to the undocumented nature of Godot, it will be impossible for us to track all methods that can return null all at once. There will be cases were users would be stuck, because of the failure I mentioned above.
I propose that we should change our code generation to check if the return value is null
and just return an empty instance of the expected return type.
val ptr = ...
if (isNull(ptr) {
return Godot.noInitZone { Node() }
}
return TypeManager.wrap(ptr) as Node
This will ensure that Object::ptr
is not initialized - which we can use to check if a given object is "null".
fun <T: Object> T.isNull() = !this::ptr.isInitialized
Note that this api should be used sparingly, only when the user has encountered the problem described above. Therefore we should mark this method as @Deprecated
and set the deprecation message to say that the user should file a ticket in Github about which method needs to return a nullable type.