-
-
Notifications
You must be signed in to change notification settings - Fork 22k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Document using negative indices in OptionButton's set_item_*()
methods
#104131
Open
Calinou
wants to merge
1
commit into
godotengine:master
Choose a base branch
from
Calinou:doc-optionbutton-set-negative-id
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+39
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -6,8 +6,47 @@ | |||||
<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. | ||||||
Like [PopupMenu], all [code]set_item_*[/code] methods allow negative item indices, i.e. [code]-1[/code] to access the last item, [code]-2[/code] to select the second-to-last item, and so on. | ||||||
[b]Note:[/b] The ID values used for items are limited to 32 bits, not full 64 bits of [int]. This has a range of [code]-2^32[/code] to [code]2^32 - 1[/code], i.e. [code]-2147483648[/code] to [code]2147483647[/code]. | ||||||
[b]Note:[/b] The [member Button.text] and [member Button.icon] properties are set automatically based on the selected item. They shouldn't be changed manually. | ||||||
[b]Example:[/b] Shuffle all existing items in an OptionButton by programmatically getting the items, clearing the list and re-adding the items in a different order: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
[codeblock] | ||||||
extends OptionButton | ||||||
|
||||||
func _ready(): | ||||||
item_selected.connect(_on_item_selected) | ||||||
|
||||||
# Copy current items to dictionary and shuffle their order. | ||||||
var items_copy = [] | ||||||
for item_idx in item_count: | ||||||
items_copy.push_back({ | ||||||
id = get_item_id(item_idx), | ||||||
icon = get_item_icon(item_idx), | ||||||
metadata = get_item_metadata(item_idx), | ||||||
text = get_item_text(item_idx), | ||||||
tooltip = get_item_tooltip(item_idx), | ||||||
disabled = is_item_disabled(item_idx), | ||||||
separator = is_item_separator(item_idx), | ||||||
}) | ||||||
|
||||||
items_copy.shuffle() | ||||||
|
||||||
# Reset items and add new shuffled items. | ||||||
clear() | ||||||
for item in items_copy: | ||||||
if item.separator: | ||||||
add_separator(item.text) | ||||||
else: | ||||||
add_icon_item(item.icon, item.text, item.id) | ||||||
set_item_metadata(-1, item.metadata) | ||||||
set_item_tooltip(-1, item.tooltip) | ||||||
set_item_disabled(-1, item.disabled) | ||||||
|
||||||
func _on_item_selected(index): | ||||||
# When querying the item's index after selection, get its original ID | ||||||
# so you know which item was actually selected regardless of order. | ||||||
print(get_item_id(index)) | ||||||
[/codeblock] | ||||||
</description> | ||||||
<tutorials> | ||||||
</tutorials> | ||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.