godotengine / godot-proposals

Godot Improvement Proposals (GIPs)
MIT License
1.11k stars 69 forks source link

Allow adding/removing metadata for items in OptionButton on the Editor #10358

Open videlanicolas opened 1 month ago

videlanicolas commented 1 month ago

Describe the project you are working on

I'm making the options menu for my game where the player can choose the screen resolution. I made an OptionButton Node and populated it with all the available resolutions:

image

When the user selects a new screen resolution I resize the screen:

extends OptionButton

var text_to_value := {
    "1920x1080": Vector2i(1920, 1080),
    "2560x1440": Vector2i(2560, 1440),
    "3840x2160": Vector2i(3840, 2160),
}

func _on_item_selected(index : int):
    var new_resolution := get_item_text(index)
    DisplayServer.window_set_size(text_to_value[new_resolution])

Describe the problem or limitation you are having in your project

I need to update an internal dictionary and update the OptionButton text each time I add a new resolution. Although it's rather infrequent, I think this can be better done with Metadata. Each item can have a metadata field with the Vector2i that corresponds to the resolution the user picked.

But I can't set the metadata per item on the editor, I can only do so via a script.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

The editor should allow adding metadata fields for each item. That way I don't need to set the metadata through a script.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

Add a button + Add Metadata for each item on OptionButton for the Editor. This should trigger a PopupMenu with a TextEdit and an OptionButton to select the appropiate metadata. That is then added to the Item and the user can modify its value. The user can map 0 or more metadata fields per item.

If this enhancement will not be used often, can it be worked around with a few lines of script?

Yes, one can:

Is there a reason why this should be core and not an add-on in the asset library?

The function to set the metadata is there, but it doesn't allow to do that on the Editor. This should be doable through the Editor as well.

Calinou commented 1 month ago

In your particular case, you can also use Vector2i(int("1920x1080".split("x")[0]), int("1920x1080".split("x")[1])) to get a Vector2i from the resolution string. You don't need to add metadata this way.

KoBeWi commented 1 month ago

Metadata is Variant, so https://github.com/godotengine/godot/pull/89324 is required for this feature.

videlanicolas commented 1 month ago

In your particular case, you can also use Vector2i(int("1920x1080".split("x")[0]), int("1920x1080".split("x")[1])) to get a Vector2i from the resolution string. You don't need to add metadata this way.

I could, yes, but it's a coincidence that the text and the metadata I need to use happens to match. If I needed an OptionMenu where each item holds metadata entries that have information not included in the text then I would need a script to manually add the metadata for each item.