How to add in a tool script values to export var?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By MikeMikeMike

Hi…,

how to add by a tool script a new string to my export var?

tool
extends Node

export(String, "Rebecca", "Mary", "Leah") var character_name

func _ready():
	if Engine.editor_hint:
		# how to add by script a fourth value "Sandy" to the character_name list?

Thanks

Mike

:bust_in_silhouette: Reply From: Zylann

You can’t add a string this way. If you use export, the list of values will be fixed.

If you want the list of values to be dynamic, there are at least two ways I can think of, but they will require a lot more work:

Way 1: _get_properties_list
Instead of using export, override _get_properties_list so you can generate and return export options manually. This means you can run any code you want to output the array of strings you want shown.
If the array needs to change, you may then call property_list_changed() to tell Godot to update the inspector.

func _get_property_list() -> Array:
	return [
		{
			"name": "character_name",
			"type": TYPE_STRING,
			"usage": PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_STORAGE,
			"hint_string": "16, 32" # <-- your list of strings
		}
	]

(note, I adapted this from code that used TYPE_INT instead, so I don’t know if it is exact)
See documentation: Object — Godot Engine (stable) documentation in English
See also this similar question: https://forum.godotengine.org/78795/is-possible-set-append-type-in-_get_property_list-as-enum?show=78795#q78795

Way 2: EditorInspectorPlugin
This is more involved but the idea is that you can completely override the GUI of the inspector for that specific property. I don’t have time for an example but I mention this anyways for completeness. Although this could open the possibility for a plugin to add support for dynamic dropdowns in a generic way.
See documentation: Inspector plugins — Godot Engine (stable) documentation in English

hi @Zylann, thank you for your detailed answer. This looks advanced, I will take a look. Thanks again, Mike

MikeMikeMike | 2022-03-24 10:45