Up to date

This page is up to date for Godot 4.2. If you still find outdated information, please open an issue.

OptionButton

Inherits: Button < BaseButton < Control < CanvasItem < Node < Object

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 ID values used for items are limited to 32 bits, not full 64 bits of int. This has a range of -2^32 to 2^32 - 1, i.e. -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

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

int

selected

-1

bool

toggle_mode

true (overrides BaseButton)

Methods

void

add_icon_item ( Texture2D texture, String label, int id=-1 )

void

add_item ( String label, int id=-1 )

void

add_separator ( String text="" )

void

clear ( )

Texture2D

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

String

get_item_tooltip ( int idx ) const

PopupMenu

get_popup ( ) const

int

get_selectable_item ( bool from_last=false ) const

int

get_selected_id ( ) const

Variant

get_selected_metadata ( ) const

bool

has_selectable_items ( ) const

bool

is_item_disabled ( int idx ) const

bool

is_item_separator ( int idx ) const

void

remove_item ( int idx )

void

select ( int idx )

void

set_disable_shortcuts ( bool disabled )

void

set_item_disabled ( int idx, bool disabled )

void

set_item_icon ( int idx, Texture2D texture )

void

set_item_id ( int idx, int id )

void

set_item_metadata ( int idx, Variant metadata )

void

set_item_text ( int idx, String text )

void

set_item_tooltip ( int idx, String tooltip )

void

show_popup ( )

Theme Properties

int

arrow_margin

4

int

modulate_arrow

0

Texture2D

arrow


Signals

item_focused ( 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 )

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.


Property Descriptions

bool allow_reselect = false

  • void set_allow_reselect ( bool value )

  • bool get_allow_reselect ( )

If true, the currently selected item can be selected again.


bool fit_to_longest_item = true

  • void set_fit_to_longest_item ( bool value )

  • 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.


int item_count = 0

  • void set_item_count ( int value )

  • int get_item_count ( )

The number of items to select from.


int selected = -1

  • int get_selected ( )

The index of the currently selected item, or -1 if no item is selected.


Method Descriptions

void add_icon_item ( 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.


void add_item ( 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.


void add_separator ( 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.


Texture2D get_item_icon ( int idx ) const

Returns the icon of the item at index idx.


int get_item_id ( int idx ) const

Returns the ID of the item at index idx.