Is default array element possible?

Godot Version

v4.2.1.stable

Question

Is it possible to set a default variable in array or dictionary when adding a new element.
For example, an array for strings has a default variable of string so when pressing “Add Element”, it creates an element that already has a string.

I believe you are talking about @export?
For arrays you can add a type specifier:
@export var a:Array[int]

For dictionaries there is no type specifier in 4.3. However typed dictionaries are ready and will be in the next version (4.4 I think).

If this isn’t what you mean could you elaborate some more please?

when you go with @export var arr : Array[String] and click on “Add Element” in your editor’s inspector, it will create a new string that’s empty. It’s not possible to preset a value for new array items.

If you don’t really need the actual value of the String and merely require some preset identifiers, you could go with enums instead

enum VALUES { yourDefault, option1, option2, option3 }
@export var arr: Array[VALUES]

that way, everytime you add a new array element, it will preset “your Default” but it restricts you from choosing any other value that’s not in the enum which is most likely not what you want.
grafik

1 Like