Changing export_enum choices from script

Is it possible to change the choices shown for an exported enum property in the editor via script?

I have a string property that I would like to export using @export_enum so you can see the useful values. However, I would like that list of useful values to change based on the value of another property.

E.g. Property wall_type is an enum of dungeon, castle, house, etc. Then there is a string var, variation, which could be anything, but is only useful if set to a value that is appropriate for the wall type. The Dungeon type might have variations “cracked”, “archway”, etc, while the House type might have variations “flat”, “window”, “half”, etc.)

Is this possible to achieve in Godot 4?

You’ll have to implement the function Object._get_property_list() and make the script a @tool script.

Example:

@tool
extends Node

const WALL_VARIATIONS = {
	"Dungeon": [
		"Dungeon variation 1",
		"Dungeon variation 2",
		"Dungeon variation 3",
	],
	"House": [
		"House variation 1",
		"House variation 2",
	],
	"Castle": [
		"Castle variation 1",
		"Castle variation 2",
		"Castle variation 3",
		"Castle variation 4",
	]
}

var wall_type:String = "Dungeon":
	set(value):
		if not value == wall_type:
			variation = WALL_VARIATIONS.get(value, []).front()
			notify_property_list_changed()
		wall_type = value

var variation:String = ""


func _get_property_list() -> Array[Dictionary]:
	var properties:Array[Dictionary] = []

	properties.push_back({
		"name": "wall_type",
		"type": TYPE_STRING,
		"hint": PROPERTY_HINT_ENUM,
		"hint_string": ",".join(WALL_VARIATIONS.keys()),
		"usage": PROPERTY_USAGE_DEFAULT
	})

	var variations = WALL_VARIATIONS.get(wall_type, [])
	if not variations.is_empty():
		properties.push_back({
			"name": "variation",
			"type": TYPE_STRING,
			"hint": PROPERTY_HINT_ENUM,
			"hint_string": ",".join(variations),
			"usage": PROPERTY_USAGE_DEFAULT
		})

	return properties
2 Likes

This works perfectly, thanks!! :slight_smile:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.