Custom classes in Dictionary (C#)

Godot Version

v4.2.1.stable.mono.official [b09f793f5]

Question

I created a “Stat” class in wich I define stuff like baseValue, stat modifiers etc.
Now I want to use it inside of a dictionary, so I did:

[Export]Godot.Collections.Dictionary stats = new Godot.Collections.Dictionary
{
		{"CurrentHealth", new Stat(100)}, //100 is the baseValue
        //ETC.
 }

But now I have a problem when accessing the class’ variables.
I want to do this:

 stats["CurrentHealth"].baseValue -= damage;

However I get the following error:

'Variant' does not contain a definition for 'baseValue' and no accessible extension method 'baseValue' accepting a first argument of type 'Variant' could be found (are you missing a using directive or an assembly reference?)

What am I doing wrong?

You need to cast to Stat.

((Stat)stats["CurrentHealth"]).baseValue -= damage;

1 Like

Thanks! I tried this before posting but I put the parenthesis wrong :person_facepalming:

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