Search code examples
godotgdscript

Passing array as hint in string export?


I'm trying to pass an array as a hint in string export, something like this:

extends Node


var things_list=["thing_1", "thing_2", "thing_3"]
export(String,*things_list) var current_thing=things_list[0]

why am I doing this?

I want to reuse the things_list & to do so I have to tediously write all the same values twice,
once in this list itself & once in export like this:

extends Node


var things_list=["thing_1", "thing_2", "thing_3"]
export(String,"thing_1", "thing_2", "thing_3") var current_thing=things_list[0]

this becomes really annoying with longer lists,
So is there anything like ...array in js
or something like *args in python? (as shown in the first example)

or are the values somehow stored in the export variable itself?
maybe like current_thing.export_hints[1] #thing_2


Solution

  • Those values are stored internally and there is no scripting access. And no, the parser does not understand any syntax for an array in export (nor array spread elsewhere for that matter).

    However, there is something similar. You can define an enum:

    enum things {thing_1, thing_2, thing_3}
    export(things) var current_thing = things.thing_1
    

    As you might remember, if you have an enum, you have a Dictionary. So you can still get the list of things like this:

    var things_list = things.keys()
    

    However, the enum will be of int, not of String. Which I don't know if it will be an issue. You can still get the corresponding String like this:

    var things_list = things.keys()
    print(things_list[current_thing])
    

    And, of course, your other option is to use _get_property_list. It is not overly complicated:

    var things_list := ["thing_1", "thing_2", "thing_3"]
    var current_thing:String = things_list[0]
    
    func _get_property_list() -> Array:
        return [
            {
                "name": "current_thing",
                "type": TYPE_STRING,
                "hint": PROPERTY_HINT_ENUM,
                "hint_string": PoolStringArray(things_list).join(",")
            }
        ]