:github_url: hide .. meta:: :keywords: select .. DO NOT EDIT THIS FILE!!! .. Generated automatically from Godot engine sources. .. Generator: https://github.com/godotengine/godot/tree/master/doc/tools/make_rst.py. .. XML source: https://github.com/godotengine/godot/tree/master/doc/classes/OptionButton.xml. .. _class_OptionButton: OptionButton ============ **Inherits:** :ref:`Button` **<** :ref:`BaseButton` **<** :ref:`Control` **<** :ref:`CanvasItem` **<** :ref:`Node` **<** :ref:`Object` A button that brings up a dropdown with selectable options when pressed. .. rst-class:: classref-introduction-group Description ----------- **OptionButton** is a type of button that brings up a dropdown with selectable items when pressed. The item selected becomes the "current" item and is displayed as the button text. See also :ref:`BaseButton` which contains common properties and methods associated with this node. \ **Note:** The ID values used for items are limited to 32 bits, not full 64 bits of :ref:`int`. This has a range of ``-2^32`` to ``2^32 - 1``, i.e. ``-2147483648`` to ``2147483647``. \ **Note:** The :ref:`Button.text` and :ref:`Button.icon` properties are set automatically based on the selected item. They shouldn't be changed manually. .. rst-class:: classref-reftable-group Properties ---------- .. table:: :widths: auto +-------------------------------------------------------------------+-----------------------------------------------------------------------------+-------------------------------------------------------------------------------+ | :ref:`ActionMode` | action_mode | ``0`` (overrides :ref:`BaseButton`) | +-------------------------------------------------------------------+-----------------------------------------------------------------------------+-------------------------------------------------------------------------------+ | :ref:`HorizontalAlignment` | alignment | ``0`` (overrides :ref:`Button`) | +-------------------------------------------------------------------+-----------------------------------------------------------------------------+-------------------------------------------------------------------------------+ | :ref:`bool` | :ref:`allow_reselect` | ``false`` | +-------------------------------------------------------------------+-----------------------------------------------------------------------------+-------------------------------------------------------------------------------+ | :ref:`bool` | :ref:`fit_to_longest_item` | ``true`` | +-------------------------------------------------------------------+-----------------------------------------------------------------------------+-------------------------------------------------------------------------------+ | :ref:`int` | :ref:`item_count` | ``0`` | +-------------------------------------------------------------------+-----------------------------------------------------------------------------+-------------------------------------------------------------------------------+ | :ref:`int` | :ref:`selected` | ``-1`` | +-------------------------------------------------------------------+-----------------------------------------------------------------------------+-------------------------------------------------------------------------------+ | :ref:`bool` | toggle_mode | ``true`` (overrides :ref:`BaseButton`) | +-------------------------------------------------------------------+-----------------------------------------------------------------------------+-------------------------------------------------------------------------------+ .. rst-class:: classref-reftable-group Methods ------- .. table:: :widths: auto +-----------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`add_icon_item`\ (\ texture\: :ref:`Texture2D`, label\: :ref:`String`, id\: :ref:`int` = -1\ ) | +-----------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`add_item`\ (\ label\: :ref:`String`, id\: :ref:`int` = -1\ ) | +-----------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`add_separator`\ (\ text\: :ref:`String` = ""\ ) | +-----------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`clear`\ (\ ) | +-----------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Texture2D` | :ref:`get_item_icon`\ (\ idx\: :ref:`int`\ ) |const| | +-----------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`int` | :ref:`get_item_id`\ (\ idx\: :ref:`int`\ ) |const| | +-----------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`int` | :ref:`get_item_index`\ (\ id\: :ref:`int`\ ) |const| | +-----------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Variant` | :ref:`get_item_metadata`\ (\ idx\: :ref:`int`\ ) |const| | +-----------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`String` | :ref:`get_item_text`\ (\ idx\: :ref:`int`\ ) |const| | +-----------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`String` | :ref:`get_item_tooltip`\ (\ idx\: :ref:`int`\ ) |const| | +-----------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`PopupMenu` | :ref:`get_popup`\ (\ ) |const| | +-----------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`int` | :ref:`get_selectable_item`\ (\ from_last\: :ref:`bool` = false\ ) |const| | +-----------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`int` | :ref:`get_selected_id`\ (\ ) |const| | +-----------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Variant` | :ref:`get_selected_metadata`\ (\ ) |const| | +-----------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`bool` | :ref:`has_selectable_items`\ (\ ) |const| | +-----------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`bool` | :ref:`is_item_disabled`\ (\ idx\: :ref:`int`\ ) |const| | +-----------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`bool` | :ref:`is_item_separator`\ (\ idx\: :ref:`int`\ ) |const| | +-----------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`remove_item`\ (\ idx\: :ref:`int`\ ) | +-----------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`select`\ (\ idx\: :ref:`int`\ ) | +-----------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`set_disable_shortcuts`\ (\ disabled\: :ref:`bool`\ ) | +-----------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`set_item_disabled`\ (\ idx\: :ref:`int`, disabled\: :ref:`bool`\ ) | +-----------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`set_item_icon`\ (\ idx\: :ref:`int`, texture\: :ref:`Texture2D`\ ) | +-----------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`set_item_id`\ (\ idx\: :ref:`int`, id\: :ref:`int`\ ) | +-----------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`set_item_metadata`\ (\ idx\: :ref:`int`, metadata\: :ref:`Variant`\ ) | +-----------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`set_item_text`\ (\ idx\: :ref:`int`, text\: :ref:`String`\ ) | +-----------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`set_item_tooltip`\ (\ idx\: :ref:`int`, tooltip\: :ref:`String`\ ) | +-----------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`show_popup`\ (\ ) | +-----------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ .. rst-class:: classref-reftable-group Theme Properties ---------------- .. table:: :widths: auto +-----------------------------------+-------------------------------------------------------------------------+-------+ | :ref:`int` | :ref:`arrow_margin` | ``4`` | +-----------------------------------+-------------------------------------------------------------------------+-------+ | :ref:`int` | :ref:`modulate_arrow` | ``0`` | +-----------------------------------+-------------------------------------------------------------------------+-------+ | :ref:`Texture2D` | :ref:`arrow` | | +-----------------------------------+-------------------------------------------------------------------------+-------+ .. rst-class:: classref-section-separator ---- .. rst-class:: classref-descriptions-group Signals ------- .. _class_OptionButton_signal_item_focused: .. rst-class:: classref-signal **item_focused**\ (\ index\: :ref:`int`\ ) :ref:`🔗` Emitted when the user navigates to an item using the :ref:`ProjectSettings.input/ui_up` or :ref:`ProjectSettings.input/ui_down` input actions. The index of the item selected is passed as argument. .. rst-class:: classref-item-separator ---- .. _class_OptionButton_signal_item_selected: .. rst-class:: classref-signal **item_selected**\ (\ index\: :ref:`int`\ ) :ref:`🔗` Emitted when the current item has been changed by the user. The index of the item selected is passed as argument. \ :ref:`allow_reselect` must be enabled to reselect an item. .. rst-class:: classref-section-separator ---- .. rst-class:: classref-descriptions-group Property Descriptions --------------------- .. _class_OptionButton_property_allow_reselect: .. rst-class:: classref-property :ref:`bool` **allow_reselect** = ``false`` :ref:`🔗` .. rst-class:: classref-property-setget - |void| **set_allow_reselect**\ (\ value\: :ref:`bool`\ ) - :ref:`bool` **get_allow_reselect**\ (\ ) If ``true``, the currently selected item can be selected again. .. rst-class:: classref-item-separator ---- .. _class_OptionButton_property_fit_to_longest_item: .. rst-class:: classref-property :ref:`bool` **fit_to_longest_item** = ``true`` :ref:`🔗` .. rst-class:: classref-property-setget - |void| **set_fit_to_longest_item**\ (\ value\: :ref:`bool`\ ) - :ref:`bool` **is_fit_to_longest_item**\ (\ ) If ``true``, minimum size will be determined by the longest item's text, instead of the currently selected one's. \ **Note:** For performance reasons, the minimum size doesn't update immediately when adding, removing or modifying items. .. rst-class:: classref-item-separator ---- .. _class_OptionButton_property_item_count: .. rst-class:: classref-property :ref:`int` **item_count** = ``0`` :ref:`🔗` .. rst-class:: classref-property-setget - |void| **set_item_count**\ (\ value\: :ref:`int`\ ) - :ref:`int` **get_item_count**\ (\ ) The number of items to select from. .. rst-class:: classref-item-separator ---- .. _class_OptionButton_property_selected: .. rst-class:: classref-property :ref:`int` **selected** = ``-1`` :ref:`🔗` .. rst-class:: classref-property-setget - :ref:`int` **get_selected**\ (\ ) The index of the currently selected item, or ``-1`` if no item is selected. .. rst-class:: classref-section-separator ---- .. rst-class:: classref-descriptions-group Method Descriptions ------------------- .. _class_OptionButton_method_add_icon_item: .. rst-class:: classref-method |void| **add_icon_item**\ (\ texture\: :ref:`Texture2D`, label\: :ref:`String`, id\: :ref:`int` = -1\ ) :ref:`🔗` Adds an item, with a ``texture`` icon, text ``label`` and (optionally) ``id``. If no ``id`` is passed, the item index will be used as the item's ID. New items are appended at the end. .. rst-class:: classref-item-separator ---- .. _class_OptionButton_method_add_item: .. rst-class:: classref-method |void| **add_item**\ (\ label\: :ref:`String`, id\: :ref:`int` = -1\ ) :ref:`🔗` Adds an item, with text ``label`` and (optionally) ``id``. If no ``id`` is passed, the item index will be used as the item's ID. New items are appended at the end. .. rst-class:: classref-item-separator ---- .. _class_OptionButton_method_add_separator: .. rst-class:: classref-method |void| **add_separator**\ (\ text\: :ref:`String` = ""\ ) :ref:`🔗` Adds a separator to the list of items. Separators help to group items, and can optionally be given a ``text`` header. A separator also gets an index assigned, and is appended at the end of the item list. .. rst-class:: classref-item-separator ---- .. _class_OptionButton_method_clear: .. rst-class:: classref-method |void| **clear**\ (\ ) :ref:`🔗` Clears all the items in the **OptionButton**. .. rst-class:: classref-item-separator ---- .. _class_OptionButton_method_get_item_icon: .. rst-class:: classref-method :ref:`Texture2D` **get_item_icon**\ (\ idx\: :ref:`int`\ ) |const| :ref:`🔗` Returns the icon of the item at index ``idx``. .. rst-class:: classref-item-separator ---- .. _class_OptionButton_method_get_item_id: .. rst-class:: classref-method :ref:`int` **get_item_id**\ (\ idx\: :ref:`int`\ ) |const| :ref:`🔗` Returns the ID of the item at index ``idx``. .. rst-class:: classref-item-separator ---- .. _class_OptionButton_method_get_item_index: .. rst-class:: classref-method :ref:`int` **get_item_index**\ (\ id\: :ref:`int`\ ) |const| :ref:`🔗` Returns the index of the item with the given ``id``. .. rst-class:: classref-item-separator ---- .. _class_OptionButton_method_get_item_metadata: .. rst-class:: classref-method :ref:`Variant` **get_item_metadata**\ (\ idx\: :ref:`int`\ ) |const| :ref:`🔗` Retrieves the metadata of an item. Metadata may be any type and can be used to store extra information about an item, such as an external string ID. .. rst-class:: classref-item-separator ---- .. _class_OptionButton_method_get_item_text: .. rst-class:: classref-method :ref:`String` **get_item_text**\ (\ idx\: :ref:`int`\ ) |const| :ref:`🔗` Returns the text of the item at index ``idx``. .. rst-class:: classref-item-separator ---- .. _class_OptionButton_method_get_item_tooltip: .. rst-class:: classref-method :ref:`String` **get_item_tooltip**\ (\ idx\: :ref:`int`\ ) |const| :ref:`🔗` Returns the tooltip of the item at index ``idx``. .. rst-class:: classref-item-separator ---- .. _class_OptionButton_method_get_popup: .. rst-class:: classref-method :ref:`PopupMenu` **get_popup**\ (\ ) |const| :ref:`🔗` Returns the :ref:`PopupMenu` contained in this button. \ **Warning:** This is a required internal node, removing and freeing it may cause a crash. If you wish to hide it or any of its children, use their :ref:`Window.visible` property. .. rst-class:: classref-item-separator ---- .. _class_OptionButton_method_get_selectable_item: .. rst-class:: classref-method :ref:`int` **get_selectable_item**\ (\ from_last\: :ref:`bool` = false\ ) |const| :ref:`🔗` Returns the index of the first item which is not disabled, or marked as a separator. If ``from_last`` is ``true``, the items will be searched in reverse order. Returns ``-1`` if no item is found. .. rst-class:: classref-item-separator ---- .. _class_OptionButton_method_get_selected_id: .. rst-class:: classref-method :ref:`int` **get_selected_id**\ (\ ) |const| :ref:`🔗` Returns the ID of the selected item, or ``-1`` if no item is selected. .. rst-class:: classref-item-separator ---- .. _class_OptionButton_method_get_selected_metadata: .. rst-class:: classref-method :ref:`Variant` **get_selected_metadata**\ (\ ) |const| :ref:`🔗` Gets the metadata of the selected item. Metadata for items can be set using :ref:`set_item_metadata`. .. rst-class:: classref-item-separator ---- .. _class_OptionButton_method_has_selectable_items: .. rst-class:: classref-method :ref:`bool` **has_selectable_items**\ (\ ) |const| :ref:`🔗` Returns ``true`` if this button contains at least one item which is not disabled, or marked as a separator. .. rst-class:: classref-item-separator ---- .. _class_OptionButton_method_is_item_disabled: .. rst-class:: classref-method :ref:`bool` **is_item_disabled**\ (\ idx\: :ref:`int`\ ) |const| :ref:`🔗` Returns ``true`` if the item at index ``idx`` is disabled. .. rst-class:: classref-item-separator ---- .. _class_OptionButton_method_is_item_separator: .. rst-class:: classref-method :ref:`bool` **is_item_separator**\ (\ idx\: :ref:`int`\ ) |const| :ref:`🔗` Returns ``true`` if the item at index ``idx`` is marked as a separator. .. rst-class:: classref-item-separator ---- .. _class_OptionButton_method_remove_item: .. rst-class:: classref-method |void| **remove_item**\ (\ idx\: :ref:`int`\ ) :ref:`🔗` Removes the item at index ``idx``. .. rst-class:: classref-item-separator ---- .. _class_OptionButton_method_select: .. rst-class:: classref-method |void| **select**\ (\ idx\: :ref:`int`\ ) :ref:`🔗` Selects an item by index and makes it the current item. This will work even if the item is disabled. Passing ``-1`` as the index deselects any currently selected item. .. rst-class:: classref-item-separator ---- .. _class_OptionButton_method_set_disable_shortcuts: .. rst-class:: classref-method |void| **set_disable_shortcuts**\ (\ disabled\: :ref:`bool`\ ) :ref:`🔗` If ``true``, shortcuts are disabled and cannot be used to trigger the button. .. rst-class:: classref-item-separator ---- .. _class_OptionButton_method_set_item_disabled: .. rst-class:: classref-method |void| **set_item_disabled**\ (\ idx\: :ref:`int`, disabled\: :ref:`bool`\ ) :ref:`🔗` Sets whether the item at index ``idx`` is disabled. Disabled items are drawn differently in the dropdown and are not selectable by the user. If the current selected item is set as disabled, it will remain selected. .. rst-class:: classref-item-separator ---- .. _class_OptionButton_method_set_item_icon: .. rst-class:: classref-method |void| **set_item_icon**\ (\ idx\: :ref:`int`, texture\: :ref:`Texture2D`\ ) :ref:`🔗` Sets the icon of the item at index ``idx``. .. rst-class:: classref-item-separator ---- .. _class_OptionButton_method_set_item_id: .. rst-class:: classref-method |void| **set_item_id**\ (\ idx\: :ref:`int`, id\: :ref:`int`\ ) :ref:`🔗` Sets the ID of the item at index ``idx``. .. rst-class:: classref-item-separator ---- .. _class_OptionButton_method_set_item_metadata: .. rst-class:: classref-method |void| **set_item_metadata**\ (\ idx\: :ref:`int`, metadata\: :ref:`Variant`\ ) :ref:`🔗` Sets the metadata of an item. Metadata may be of any type and can be used to store extra information about an item, such as an external string ID. .. rst-class:: classref-item-separator ---- .. _class_OptionButton_method_set_item_text: .. rst-class:: classref-method |void| **set_item_text**\ (\ idx\: :ref:`int`, text\: :ref:`String`\ ) :ref:`🔗` Sets the text of the item at index ``idx``. .. rst-class:: classref-item-separator ---- .. _class_OptionButton_method_set_item_tooltip: .. rst-class:: classref-method |void| **set_item_tooltip**\ (\ idx\: :ref:`int`, tooltip\: :ref:`String`\ ) :ref:`🔗` Sets the tooltip of the item at index ``idx``. .. rst-class:: classref-item-separator ---- .. _class_OptionButton_method_show_popup: .. rst-class:: classref-method |void| **show_popup**\ (\ ) :ref:`🔗` Adjusts popup position and sizing for the **OptionButton**, then shows the :ref:`PopupMenu`. Prefer this over using ``get_popup().popup()``. .. rst-class:: classref-section-separator ---- .. rst-class:: classref-descriptions-group Theme Property Descriptions --------------------------- .. _class_OptionButton_theme_constant_arrow_margin: .. rst-class:: classref-themeproperty :ref:`int` **arrow_margin** = ``4`` :ref:`🔗` The horizontal space between the arrow icon and the right edge of the button. .. rst-class:: classref-item-separator ---- .. _class_OptionButton_theme_constant_modulate_arrow: .. rst-class:: classref-themeproperty :ref:`int` **modulate_arrow** = ``0`` :ref:`🔗` If different than ``0``, the arrow icon will be modulated to the font color. .. rst-class:: classref-item-separator ---- .. _class_OptionButton_theme_icon_arrow: .. rst-class:: classref-themeproperty :ref:`Texture2D` **arrow** :ref:`🔗` The arrow icon to be drawn on the right end of the button. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)` .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)` .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)` .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)` .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)` .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)` .. |bitfield| replace:: :abbr:`BitField (This value is an integer composed as a bitmask of the following flags.)` .. |void| replace:: :abbr:`void (No return value.)`