Class OptionButton
- Namespace
- Godot
- Assembly
- GodotSharp.dll
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 Text and Icon properties are set automatically based on the selected item. They shouldn't be changed manually.
public class OptionButton : Button, IDisposable
- Inheritance
-
OptionButton
- Implements
- Inherited Members
Constructors
OptionButton()
public OptionButton()
Properties
AllowReselect
If true
, the currently selected item can be selected again.
public bool AllowReselect { get; set; }
Property Value
FitToLongestItem
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.
public bool FitToLongestItem { get; set; }
Property Value
ItemCount
The number of items to select from.
public int ItemCount { get; set; }
Property Value
Selected
The index of the currently selected item, or -1
if no item is selected.
public int Selected { get; set; }
Property Value
Methods
AddIconItem(Texture2D, string, int)
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.
public void AddIconItem(Texture2D texture, string label, int id = -1)
Parameters
AddItem(string, int)
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.
public void AddItem(string label, int id = -1)
Parameters
AddSeparator(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.
public void AddSeparator(string text = "")
Parameters
text
string
Clear()
Clears all the items in the OptionButton.
public void Clear()
GetItemIcon(int)
Returns the icon of the item at index idx
.
public Texture2D GetItemIcon(int idx)
Parameters
idx
int
Returns
GetItemId(int)
Returns the ID of the item at index idx
.
public int GetItemId(int idx)
Parameters
idx
int
Returns
GetItemIndex(int)
Returns the index of the item with the given id
.
public int GetItemIndex(int id)
Parameters
id
int
Returns
GetItemMetadata(int)
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.
public Variant GetItemMetadata(int idx)
Parameters
idx
int
Returns
GetItemText(int)
Returns the text of the item at index idx
.
public string GetItemText(int idx)
Parameters
idx
int
Returns
GetItemTooltip(int)
Returns the tooltip of the item at index idx
.
public string GetItemTooltip(int idx)
Parameters
idx
int
Returns
GetPopup()
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 Visible property.
public PopupMenu GetPopup()
Returns
GetSelectableItem(bool)
Returns the index of the first item which is not disabled, or marked as a separator. If fromLast
is true
, the items will be searched in reverse order.
Returns -1
if no item is found.
public int GetSelectableItem(bool fromLast = false)
Parameters
fromLast
bool
Returns
GetSelectedId()
Returns the ID of the selected item, or -1
if no item is selected.
public int GetSelectedId()
Returns
GetSelectedMetadata()
Gets the metadata of the selected item. Metadata for items can be set using SetItemMetadata(int, Variant).
public Variant GetSelectedMetadata()
Returns
HasGodotClassMethod(in godot_string_name)
Check if the type contains a method with the given name. This method is used by Godot to check if a method exists before invoking it. Do not call or override this method.
protected override bool HasGodotClassMethod(in godot_string_name method)
Parameters
method
godot_string_nameName of the method to check for.
Returns
HasGodotClassSignal(in godot_string_name)
Check if the type contains a signal with the given name. This method is used by Godot to check if a signal exists before raising it. Do not call or override this method.
protected override bool HasGodotClassSignal(in godot_string_name signal)
Parameters
signal
godot_string_nameName of the signal to check for.
Returns
HasSelectableItems()
Returns true
if this button contains at least one item which is not disabled, or marked as a separator.
public bool HasSelectableItems()
Returns
InvokeGodotClassMethod(in godot_string_name, NativeVariantPtrArgs, out godot_variant)
Invokes the method with the given name, using the given arguments. This method is used by Godot to invoke methods from the engine side. Do not call or override this method.
protected override bool InvokeGodotClassMethod(in godot_string_name method, NativeVariantPtrArgs args, out godot_variant ret)
Parameters
method
godot_string_nameName of the method to invoke.
args
NativeVariantPtrArgsArguments to use with the invoked method.
ret
godot_variantValue returned by the invoked method.
Returns
IsItemDisabled(int)
Returns true
if the item at index idx
is disabled.
public bool IsItemDisabled(int idx)
Parameters
idx
int
Returns
IsItemSeparator(int)
Returns true
if the item at index idx
is marked as a separator.
public bool IsItemSeparator(int idx)
Parameters
idx
int
Returns
RemoveItem(int)
Removes the item at index idx
.
public void RemoveItem(int idx)
Parameters
idx
int
Select(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.
public void Select(int idx)
Parameters
idx
int
SetDisableShortcuts(bool)
If true
, shortcuts are disabled and cannot be used to trigger the button.
public void SetDisableShortcuts(bool disabled)
Parameters
disabled
bool
SetItemDisabled(int, 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.
public void SetItemDisabled(int idx, bool disabled)
Parameters
SetItemIcon(int, Texture2D)
Sets the icon of the item at index idx
.
public void SetItemIcon(int idx, Texture2D texture)
Parameters
SetItemId(int, int)
Sets the ID of the item at index idx
.
public void SetItemId(int idx, int id)
Parameters
SetItemMetadata(int, 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.
public void SetItemMetadata(int idx, Variant metadata)
Parameters
SetItemText(int, string)
Sets the text of the item at index idx
.
public void SetItemText(int idx, string text)
Parameters
SetItemTooltip(int, string)
Sets the tooltip of the item at index idx
.
public void SetItemTooltip(int idx, string tooltip)
Parameters
ShowPopup()
Adjusts popup position and sizing for the OptionButton, then shows the PopupMenu. Prefer this over using get_popup().popup()
.
public void ShowPopup()
Events
ItemFocused
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.
public event OptionButton.ItemFocusedEventHandler ItemFocused
Event Type
ItemSelected
Emitted when the current item has been changed by the user. The index of the item selected is passed as argument.
AllowReselect must be enabled to reselect an item.
public event OptionButton.ItemSelectedEventHandler ItemSelected