Class reference
OptionButton
Inherits Button
A button that brings up a dropdown with selectable options when pressed.
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 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
int action_mode = 0
int action_mode = 0int alignment = 0
int alignment = 0bool allow_reselect = false
bool allow_reselect = falseIf true, the currently selected item can be selected again.
bool fit_to_longest_item = true
bool fit_to_longest_item = trueIf 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.
int item_count = 0
int item_count = 0The number of items to select from.
int selected = -1
int selected = -1The index of the currently selected item, or -1 if no item is selected.
bool toggle_mode = true
bool toggle_mode = trueMethods
void add_icon_item(Texture2D texture, String label, int id = -1)
Texture2D texture, String label, int id = -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.
void add_item(String label, int id = -1)
String label, int id = -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.
void add_separator(String text = "")
String text = "")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.
void clear()
Clears all the items in the OptionButton.
int get_item_auto_translate_mode(int idx) const
int get_item_auto_translate_mode(int idx) constReturns the auto translate mode of the item at index idx.
Texture2D get_item_icon(int idx) const
Texture2D get_item_icon(int idx) constReturns the icon of the item at index idx.
int get_item_id(int idx) const
int get_item_id(int idx) constReturns the ID of the item at index idx.
int get_item_index(int id) const
int get_item_index(int id) constReturns the index of the item with the given id.
Variant get_item_metadata(int idx) const
Variant get_item_metadata(int idx) constRetrieves 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.
String get_item_text(int idx) const
String get_item_text(int idx) constReturns the text of the item at index idx.
String get_item_tooltip(int idx) const
String get_item_tooltip(int idx) constReturns the tooltip of the item at index idx.
PopupMenu get_popup() const
PopupMenu get_popup() constReturns 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.
int get_selectable_item(bool from_last = false) const
int get_selectable_item(bool from_last = false) constReturns 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.
int get_selected_id() const
int get_selected_id() constReturns the ID of the selected item, or -1 if no item is selected.
Variant get_selected_metadata() const
Variant get_selected_metadata() constGets the metadata of the selected item. Metadata for items can be set using set_item_metadata().
bool has_selectable_items() const
bool has_selectable_items() constReturns true if this button contains at least one item which is not disabled, or marked as a separator.
bool is_item_disabled(int idx) const
bool is_item_disabled(int idx) constReturns true if the item at index idx is disabled.
bool is_item_separator(int idx) const
bool is_item_separator(int idx) constReturns true if the item at index idx is marked as a separator.
void remove_item(int idx)
int idx)Removes the item at index idx.
void select(int idx)
int idx)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.
void set_disable_shortcuts(bool disabled)
bool disabled)If true, shortcuts are disabled and cannot be used to trigger the button.
void set_item_auto_translate_mode(int idx, int mode)
int idx, int mode)Sets the auto translate mode of the item at index idx. Items use Node.AUTO_TRANSLATE_MODE_INHERIT by default, which uses the same auto translate mode as the OptionButton itself.
void set_item_disabled(int idx, bool disabled)
int idx, bool disabled)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.
void set_item_icon(int idx, Texture2D texture)
int idx, Texture2D texture)Sets the icon of the item at index idx.
void set_item_id(int idx, int id)
int idx, int id)Sets the ID of the item at index idx.
void set_item_metadata(int idx, Variant metadata)
int idx, Variant metadata)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.
void set_item_text(int idx, String text)
int idx, String text)Sets the text of the item at index idx.
void set_item_tooltip(int idx, String tooltip)
int idx, String tooltip)Sets the tooltip of the item at index idx.
void show_popup()
Adjusts popup position and sizing for the OptionButton, then shows the PopupMenu. Prefer this over using get_popup().popup().
Signals
item_focused(int index)
int index)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 selected is passed as argument.
item_selected(int index)
int index)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.