(C#) Cannot convert from 'System.Collections.Generic.KeyValuePair<Godot.Variant, Godot.Variant>' to 'Godot.Variant'

Godot Version

4.2.1

Question

In the following code:

66 foreach (var R in Resolutions)
67 {
68 ResolutionOptions.AddItem(R, ID);
69 if (Resolutions[R] == CurrentResolution)
70 {
71 ResolutionOptions.Select(ID);
72 }
73 ID+=1;
74 }

I get the errors:

Argument 1: cannot convert from ‘System.Collections.Generic.KeyValuePair<Godot.Variant, Godot.Variant>’ to ‘string’ [Line 68]
Argument 1: cannot convert from ‘System.Collections.Generic.KeyValuePair<Godot.Variant, Godot.Variant>’ to ‘Godot.Variant’ [Line 69]

What do I do??

Hello,
which line is 57, 68 and 69?
Share the code with the variable declaration.

I ran into a similar problem while trying to save, I never figured it out and had to use… GDSCRIPT

I’ve edited the post to add line numbers.

1 Like

Forgot the last bit, sorry.

17 [Export] private OptionButton ResolutionOptions { get; set; }

…other code

19 private Godot.Collections.Dictionary Resolutions = new Godot.Collections.Dictionary{
20 {“(3840x2160) - (16:9)”, new Vector2I(3840,2160)},
21 {“(2560x1440) - (16:9)”, new Vector2I(2560,1440)},
22 {“(1920x1080) - (16:9)”, new Vector2I(1920,1080)},
23 {“(1536x864) - (16:9)”, new Vector2I(1536,864)},
24 {“(1280x720) - (16:9)”, new Vector2I(1280,720)},
25 {“(1024x768) - (4:3)”, new Vector2I(1024,768)},
26 {“(800x600) - (4:3)”, new Vector2I(800,600)},
27 {“(640x480) - (4:3)”, new Vector2I(640,480)},
28 {“(320x240) - (4:3)”, new Vector2I(320,240)},
29 };

…other code

64 var CurrentResolution = GetWindow().Size;
65 var ID = 0;
66 foreach (var R in Resolutions)
67 {
68 ResolutionOptions.AddItem(R, ID);
69 if (Resolutions[R] == CurrentResolution)
70 {
71 ResolutionOptions.Select(ID);
72 }
73 ID+=1;
74 }

1 Like

line 68
OptionButton.AddItem first argument is string.

to fix:
ResolutionOptions.AddItem(R.ToString(), ID);

line 69
to compare use (Vector2I)R.Value.Obj == CurrentResolution.
You don’t need to get the value from the Resolutions dictionary because you have what you want in the R variable.
(Vector2I)R.Value.Obj is convertion value to value of type Vector2I because this is type of CurrentResolution variable and value in dictionary.

More information about dictionary Dictionary — Godot Engine (stable) documentation in English

I still don’t see line 57.

This is quite useful, thanks…

…but where did the “resolution” variable come from?

(Ignore the line 57 error, I should get rid of it actually)

Sorry, I copied this code from my test project. Answer is updated. I meant R variable.

Thanks!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.