Godot-proposals: ItemList add_item and add_icon_item indices

Created on 25 Sep 2019  路  1Comment  路  Source: godotengine/godot-proposals

Describe how this feature / enhancement will help your project:
Makes work with ItemList more obvious by returning added item index.

Describe implementation detail for your proposal (in code), if possible:
var index = $ItemList.add_item(...)
$ItemList.set_item_tooltip(index, ...)

If this enhancement will not be used often, can it be worked around with a few lines of script?:
var index = $ItemList.get_item_count()
$ItemList.add_item(...)
$ItemList.set_item_tooltip(index, ...)

Is there a reason why this should be core and not an add-on in the asset library?:
It's too basic feature.

Currently both add_item and add_icon_item functions return void. It won't break compatibility much, but will make setting up ItemList more obvious. Every item-related function in ItemList works with indices, except for add_item and add_icon_item.

I can make a pull request when I'll have some time.

gui

Most helpful comment

Every single time I add an item to an ItemList, the code ends like this:

var index = item_list.get_item_count()
item_list.add_item(...)
item_list.set_item_metadata(index, ...)
...

Unless the list is not interactive or we use the displayed text as identifier, the first line is always required.

If we do a parallel with Array.append(), this one doesn't return the index, which is fine, it's just a simple container after all. So why ItemList would? Because ItemList.add_item constructs items, it's not adding an item you had in a variable already (which you could entirely specify before adding). So the only way to modify it is by index, using API functions for various of its properties, mostly because we often need to specify more than just text, icon and selectability.

In that regard, returning the index would be helpful. It has only one design downside: it hides the fact items are accessed by index (and not by "some unique ID").

>All comments

Every single time I add an item to an ItemList, the code ends like this:

var index = item_list.get_item_count()
item_list.add_item(...)
item_list.set_item_metadata(index, ...)
...

Unless the list is not interactive or we use the displayed text as identifier, the first line is always required.

If we do a parallel with Array.append(), this one doesn't return the index, which is fine, it's just a simple container after all. So why ItemList would? Because ItemList.add_item constructs items, it's not adding an item you had in a variable already (which you could entirely specify before adding). So the only way to modify it is by index, using API functions for various of its properties, mostly because we often need to specify more than just text, icon and selectability.

In that regard, returning the index would be helpful. It has only one design downside: it hides the fact items are accessed by index (and not by "some unique ID").

Was this page helpful?
0 / 5 - 0 ratings

Related issues

wijat picture wijat  路  3Comments

Xrayez picture Xrayez  路  3Comments

lupoDharkael picture lupoDharkael  路  3Comments

regakakobigman picture regakakobigman  路  3Comments

SpyrexDE picture SpyrexDE  路  3Comments