How to access enums in C#

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Adam Cigánek

Hey there,

When scripting in C#, what is the correct syntax to accessing enum values, e.g. ACCESS_USERDATA, ACCESS_FILESYSTEM, etc… ( FileDialog — Godot Engine (latest) documentation in English )?

:bust_in_silhouette: Reply From: Adam Cigánek

Nevermind, figured it out myself. It’s FileDialog.AccessEnum.Filesystem. There is a gotcha here, because that enum in GDScript is called just Access, but in C# it’s AccessEnum. This is because the name Access is already taken by a field in FileDialog. If it weren’t taken, the enum would be called just Access. For example in Control, the size flags are accessed as Control.SizeFlag.Fill, etc…, and not Control.SizeFlagEnum.Fill.

This should probably be mentioned in the docs.

What you can do to figure out what name the enum value names is to give a random value to a variable of the type you want, the see the “Cannot convert bla into Godot.bla” error. The enum name will be that. In my case, I wanted to change the format of an AudioStreamSample. This has enum values. I made a new variable of type AudioStreamSample, the changed its Format to a random integer like so:

AudioStreamSample sample = (AudioStreamSample)ResourceLoader.Load("res://fart");
sample.Format = 3;

Next, an error ocurred:

Cannot convert type ‘int’ to ‘Godot.AudioStreamSample.FormatEnum’

And there you have it. Just change the 3 with Godot.AudioStreamPlayer.FormatEnum.ImaAdpcm

(The last thing is whatever the value is in c# format, so capital letters to replace the underscores)

jgee_23_ | 2021-05-21 05:44