0 votes


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

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?



Godot version 3.4.stable
in Engine by (136 points)

1 Answer

0 votes
Best answer

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,
            "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: https://docs.godotengine.org/en/stable/classes/class_object.html#class-object-method-get-property-list
See also this similar question: https://godotengine.org/qa/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: https://docs.godotengine.org/en/stable/tutorials/plugins/editor/inspector_plugins.html

by (28,789 points)
selected by

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.