godot icon indicating copy to clipboard operation
godot copied to clipboard

C# - Cannot set `Variant` as value of Dictionary

Open Structed opened this issue 3 years ago • 3 comments

Godot version

v4.0.beta1.mono.official [4ba934bf3]

System information

Windows 11 21H2 (OS Build 22000.978)

Issue description

When trying to set the value of a Godot.Collections.Dictionary to a Godot.Variant.Type value (ulong), see line 3:

var d = new Godot.Collections.Dictionary();
d.Add("name", "property_name");
d.Add("type", Variant.Type.String);

it produces the following error:

Argument type 'Godot.Variant.Type' is not assignable to parameter type 'Godot.Variant'

I would expect an implicit conversion.

Steps to reproduce

  1. Create a new C# Class
  2. Create a method containing the following code:
var d = new Godot.Collections.Dictionary();
d.Add("name", "property_name");
d.Add("type", Variant.Type.String);

Minimal reproduction project

variant-repro.zip

Structed avatar Sep 17 '22 23:09 Structed

We implement implicit conversions from marshable types to Variant, enums are marshable as their underlying type but since each enum is considered a different type we'd have to implement implicit conversions for every enum and for user-defined enums this would not be possible.

I think the best thing we can do here is add an analyzer so when you are assigning an enum to a Variant we let you know that you need to cast it (we could also provide a Code Fix that adds the cast for you).

raulsntos avatar Sep 18 '22 08:09 raulsntos

In terms of providing conversions for engine provided enums: would you think it makes sense and is purely not done because it's a lot of work? Or would you say it's not good because it would create inconsistencies with how userland enums would behave?

I like the idea with the source generators and quick fixes! That definitely scales and would make things very obvious for users. Should that be a feature request instead?

Structed avatar Sep 18 '22 15:09 Structed

I think the inconsistency would certainly make it confusing since some users would wonder why it works with some enums and not others, but it's more to avoid a tight coupling between the Variant type and the rest of the engine.

raulsntos avatar Sep 19 '22 16:09 raulsntos