- Edited
I want to create a class called DynamicEnum which contains an internal array and shows it like a dropdown list inside the inspector when you write: @export var my_list:DynamicEnum = DynamicEnum.new(["item 1","item 2"])
I've worked on it and already know how to make a different inspector for type int (the documentation tutorial).
This is the code for the dynamic_enum:
extends EditorProperty
#The main control for editing the property.
var property_control = OptionButton.new()
#An internal value of the property.
var current_value
#A guard against internal changes when the property is updated.
var updating = false
var selected_item
var _array:Array[String]
func _init(arr:Array[String]):
_array = arr
var callable = Callable(self,"on_item_selected")
property_control.connect("item_selected",callable)
#Add the control as a direct child of EditorProperty node.
add_child(property_control)
#Make sure the control is able to retain the focus.
add_focusable(property_control)
#Setup the initial state and connect to the signal to track changes.
refresh_list(_array)
func _update_property():
#Read the current value from the property.
#var new_value = get_edited_object()[get_edited_property()]
var new_value = get_edited_property()
if (new_value == current_value):
return
#Update the control with the new value.
updating = true
current_value = new_value
updating = false
func refresh_list(arr:Array[String]):
for i in arr:
property_control.add_item(i)
func on_item_selected(id):
#Ignore the signal if the property is currently being updated.
if (updating):
return
selected_item = property_control.get_item_text(id)
emit_changed(get_edited_property(), selected_item)
#current_value = str(property_control.get_item_text(id))
And this is the EditorInspectorPlugin code:
extends EditorInspectorPlugin
var dynamic_enum = preload("res://addons/my_inspector_plugin/dynamic_enum.gd")
func _can_handle(object):
#We support all objects in this example.
return true
func _parse_property(object, type, name, hint_type, hint_string, usage_flags, wide):
if type == TYPE_OBJECT:
#the class name
if hint_string == "DynamicEnum":
#It's my custom class.Do what you are supposed to do:
add_property_editor(name, dynamic_enum.new(["1","2","3"]))
#return true
else:
return false
The 1, 2, 3 items are shown as a dropdown. But @export var _items:DynamicEnum
is always null. No matter what. I can't use new here, because it says: "Parser Error: Class "DynamicEnum" cannot be constructed as it is based on abstract native class "EditorProperty"."
After 3 hours of trying to learn Custom Inspectors my brain has just freezed and have no clue what I should do. Back in Unity, the custom inspector class (EditorProperty in this case I guess) was used to "refer" to another class which was the actual data to be shown. But in Godot I'm not sure what is going on...