OptionButton
Inherits: Button < BaseButton < Control < CanvasItem < Entity < Object
Description
Section titled “Description”A button that brings up a dropdown with selectable options when pressed.
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 BaseButton which contains common properties and methods associated with this node.
Note: The IDs used for items are limited to signed 32-bit integers, not the full 64 bits of int. These have a range of -2^31 to 2^31 - 1, that is, -2147483648 to 2147483647.
Note: The Button.text and Button.icon properties are set automatically based on the selected item. They shouldn’t be changed manually.
Properties
Section titled “Properties”| Type | Name | Default |
|---|---|---|
| BaseButton.ActionMode | action_mode |
0 (overrides BaseButton) |
| HorizontalAlignment | alignment |
0 (overrides Button) |
| bool | allow_reselect |
false |
| bool | fit_to_longest_item |
true |
| int | item_count |
0 |
| bool | popup/item_{index}/disabled |
false |
| Texture2D | popup/item_{index}/icon |
|
| int | popup/item_{index}/id |
0 |
| bool | popup/item_{index}/separator |
false |
| String | popup/item_{index}/text |
"" |
| bool | search_bar_enabled |
false |
| bool | search_bar_fuzzy_search_enabled |
true |
| int | search_bar_fuzzy_search_max_misses |
2 |
| int | search_bar_min_item_count |
0 |
| int | selected |
-1 |
| bool | toggle_mode |
true (overrides BaseButton) |
Methods
Section titled “Methods”Signals
Section titled “Signals”item_focused
item_focused(index: int)
Emitted when the user navigates to an item using the ProjectSettings.input/ui_up or ProjectSettings.input/ui_down input actions. The index of the item focused is passed as argument.
item_selected
item_selected(index: int)
Emitted when the current item has been changed by the user. The index of the item selected is passed as argument.
allow_reselect must be enabled to reselect an item.
Theme items
Section titled “Theme items”arrow_margin
int arrow_margin (constant) = 4
modulate_arrow
int modulate_arrow (constant) = 0
arrow
Texture2D arrow (icon)
Property descriptions
Section titled “Property descriptions”allow_reselect
bool allow_reselect = false
If true, the currently selected item can be selected again.
fit_to_longest_item
bool fit_to_longest_item = true
If true, minimum size will be determined by the longest item’s width, instead of the currently selected one’s. It will also take the popup’s margins into account, making the button match its total width.
Note: For performance reasons, the minimum size doesn’t update immediately when adding, removing or modifying items.
item_count
int item_count = 0
The number of items to select from.
popup/item_{index}/disabled
bool popup/item_{index}/disabled = false
If true, the item at index is disabled.
Note: index is a value in the 0 .. item_count - 1 range.
popup/item_{index}/icon
Texture2D popup/item_{index}/icon
The icon of the item at index.
Note: index is a value in the 0 .. item_count - 1 range.
popup/item_{index}/id
int popup/item_{index}/id = 0
The ID of the item at index.
Note: index is a value in the 0 .. item_count - 1 range.
popup/item_{index}/separator
bool popup/item_{index}/separator = false
If true, the item at index is a separator.
Note: index is a value in the 0 .. item_count - 1 range.
popup/item_{index}/text
String popup/item_{index}/text = ""
The text of the item at index.
Note: index is a value in the 0 .. item_count - 1 range.
search_bar_enabled
bool search_bar_enabled = false
If true, shows a search bar at the top of the PopupMenu for filtering items. See search_bar_min_item_count for dynamically controlling its visibility based on the number of items.
search_bar_fuzzy_search_enabled
bool search_bar_fuzzy_search_enabled = true
If true, enables fuzzy searching in the PopupMenu search bar. This allows the search results to include items that almost match the search query, as well items that match the individual characters of the search query, but not in sequence.
Use search_bar_fuzzy_search_max_misses to set the maximum number of mismatches allowed in the search results.
search_bar_fuzzy_search_max_misses
int search_bar_fuzzy_search_max_misses = 2
Sets the maximum number of mismatches allowed in each search result when fuzzy searching is enabled for the PopupMenu search bar. Any item with more mismatches will be hidden from the search results.
search_bar_min_item_count
int search_bar_min_item_count = 0
Sets the minimum number of items required for the PopupMenu search bar to be visible. search_bar_enabled must be true for this to have any effect.
selected
int selected = -1
The index of the currently selected item, or -1 if no item is selected.
Method descriptions
Section titled “Method descriptions”add_icon_item
void add_icon_item(texture: Texture2D, label: String, id: int = -1)
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.
Note: The item will be selected if there are no other items.
add_item
void add_item(label: String, id: int = -1)
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.
Note: The item will be selected if there are no other items.
add_separator
void add_separator(text: String = "")
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.
clear
void clear()
Clears all the items in the OptionButton.
get_item_auto_translate_mode
Entity.AutoTranslateMode get_item_auto_translate_mode(idx: int) const
Returns the auto translate mode of the item at index idx.
get_item_icon
Texture2D get_item_icon(idx: int) const
Returns the icon of the item at index idx.
get_item_id
int get_item_id(idx: int) const
Returns the ID of the item at index idx.
get_item_index
int get_item_index(id: int) const
Returns the index of the item with the given id.
get_item_metadata
Variant get_item_metadata(idx: int) const
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.
get_item_text
String get_item_text(idx: int) const
Returns the text of the item at index idx.
get_item_tooltip
String get_item_tooltip(idx: int) const
Returns the tooltip of the item at index idx.
get_popup
PopupMenu get_popup() const
Returns the 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 Window.visible property.
get_selectable_item
int get_selectable_item(from_last: bool = false) const
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.
get_selected_id
int get_selected_id() const
Returns the ID of the selected item, or -1 if no item is selected.
get_selected_metadata
Variant get_selected_metadata() const
Gets the metadata of the selected item. Metadata for items can be set using set_item_metadata().
has_selectable_items
bool has_selectable_items() const
Returns true if this button contains at least one item which is not disabled, or marked as a separator.
is_item_disabled
bool is_item_disabled(idx: int) const
Returns true if the item at index idx is disabled.
is_item_separator
bool is_item_separator(idx: int) const
Returns true if the item at index idx is marked as a separator.
remove_item
void remove_item(idx: int)
Removes the item at index idx.
select
void select(idx: int)
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.
set_disable_shortcuts
void set_disable_shortcuts(disabled: bool)
If true, shortcuts are disabled and cannot be used to trigger the button.
set_item_auto_translate_mode
void set_item_auto_translate_mode(idx: int, mode: Entity.AutoTranslateMode)
Sets the auto translate mode of the item at index idx.
Items use Entity.AUTO_TRANSLATE_MODE_INHERIT by default, which uses the same auto translate mode as the OptionButton itself.
set_item_disabled
void set_item_disabled(idx: int, disabled: bool)
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.
set_item_icon
void set_item_icon(idx: int, texture: Texture2D)
Sets the icon of the item at index idx.
set_item_id
void set_item_id(idx: int, id: int)
Sets the ID of the item at index idx.
set_item_metadata
void set_item_metadata(idx: int, metadata: Variant)
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.
set_item_text
void set_item_text(idx: int, text: String)
Sets the text of the item at index idx.
set_item_tooltip
void set_item_tooltip(idx: int, tooltip: String)
Sets the tooltip of the item at index idx.
show_popup
void show_popup()
Adjusts popup position and sizing for the OptionButton, then shows the PopupMenu. Prefer this over using get_popup().popup().
