Skip to content
This repository was archived by the owner on Apr 27, 2026. It is now read-only.
This repository was archived by the owner on Apr 27, 2026. It is now read-only.

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

@raniejade

Description

@raniejade

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions