godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
88.32k stars 20k forks source link

_get_drag_data -> return type wrong in c# #78507

Open IceReaper opened 1 year ago

IceReaper commented 1 year ago

Godot version

4.0.3

System information

Windows 11, Godot 4.0.3, Vulkan

Issue description

Control docs state:

Variant _get_drag_data ( Vector2 at_position ) virtual const

Godot calls this method to get data that can be dragged and dropped onto controls that expect drop data. Returns null if there is no data to drag.

However, using a c# project

<Project Sdk="Godot.NET.Sdk/4.0.3">

    <PropertyGroup>
        <TargetFramework>net7.0</TargetFramework>
        <Nullable>enable</Nullable>
        <ImplicitUsings>enable</ImplicitUsings>
        <EnableDynamicLoading>true</EnableDynamicLoading>
    </PropertyGroup>

</Project>

I cannot return null in this function, as its return type is defined as Variant, and not Variant? Compilation error is [CS0037] Cannot convert null to 'Variant' because it is a non-nullable value type

Steps to reproduce

public override Variant _GetDragData(Vector2 atPosition)
{
    return null;
}

Minimal reproduction project

N/A

raulsntos commented 1 year ago

Variant is a struct so indeed you can't return null. The marshaling code expects to receive a Variant so it can communicate with Godot. It may be possible to support Variant? but that would be a breaking change because this is just syntax sugar for System.Nullable<Variant> which is a different type and changnig the return type of a method breaks compatibility.

What you actually need to do here is create a Variant that represents null. You can do this by using the default keyword or the parameterless constructor:

public override Variant _GetDragData(Vector2 atPosition)
{
    return default;
}