Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a fallback way to check if a returned Object is null #219

Open
raniejade opened this issue Aug 8, 2020 · 0 comments
Open

Add a fallback way to check if a returned Object is null #219

raniejade opened this issue Aug 8, 2020 · 0 comments

Comments

@raniejade
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant