+1 vote

I have an OptionButton with Items "c", "d", "b", "t", "a" in this order. How to sort them (all items) alphabetically in code?

Or is it a bug that $OptionButton.items.sort() doesn't work?

in Engine by (382 points)

Have you tried assigning it to a temporary array, sorting that array, and assigning it back to item?

The problem is there is no get_items() function or items.text.


int         get_item_count ( ) const
Texture     get_item_icon ( int idx ) const
int         get_item_id ( int idx ) const
int         get_item_index ( int id ) const
Variant     get_item_metadata ( int idx ) const
String      get_item_text ( int idx ) const

2 Answers

0 votes

Thanks to Becbunzen for pointing out where the methods are in the documentation. To sort them alphabetically, you may have to write it like this:

# Somewhere at the top of the script.
onready var options_button: OptionsButton = $/path/to/OptionsButton

# This function will work if the list is made of *only* String items. It can't manage separators.
func alphabetize():
    # Have a dictionary hold all of the necessary information. Assign the text strings as keys. Note that the text strings *must* be unique from the other items.
    var unorganized_items: Dictionary = {}

    for item_number in options_button.get_item_count():
        # Get all of the information for each item.
        var text: String = options_button.get_item_text( item_number )
        var icon: Texture = options_button.get_item_icon( item_number )
        # Put them into our dictionary.
        unorganized_items[text] = icon
    # Clear out the options button so that we can put in our sorted list.
    # Populate our options button with the newly alphabetized list.
    var sorted_keys: Array = unorganized_items.keys()
    # For some odd reason, this method has to be done separately.
    # Go through the sorted keys and add the new items to our options button.
    for text in sorted_keys:
        options_button.add_icon_item( unorganized_items[text], text )

As I have noted in the function, the code can only take a list of String items; no separators.

by (3,162 points)
–3 votes

Sometimes the more complex problems have the simplest solutions.
You are the programmer you know all the possible items the user can have.

Just hard code an array with all the possible items as strings in alphabetical order
Then just refer to it with a double for for loop.

First loop checking all the possible items, second loop searching the players items. In this case the button name
If theres a match append a temperary array/ dictionary depending on use case
Then assign the temporary variable as the new list after its complete.

Better yet just use a single loop over the array of all the possible items from the alphabetical array then use the has() function on your player items dictionary then do it that way

by (35 points)
edited by
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 Frequently asked questions and 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 [email protected] with your username.