How to customise the ItemList class?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By djangocoyote

I’m coding a data based game, where I need to be able to display objects and rearrange them to the user’s whim but still be able to call their methods and resources. I have a big ItemList I populate with an array of objects, but I rearrange them with different criteria (alphabetically, for example), but the ItemList only holds a String so when they’re rearranged their IDs shift.

I figured the simplest way to do this would be to have a custom ItemList where:

a) I store two values, the object id and the String, or
b) I store a reference to the object and display a string with a method call

but I’m having issues finding in the documentation how to extend the class and which methods I need to override or what property holds the display values. Is there a way to do this or am I missing a simpler answer that’s supported in GDScript without having to do this extension in the first place?

:bust_in_silhouette: Reply From: Sween123

You can extend the class.
Create a new ItemList node, and attach a script to it. Automatically the first line will be “extends ItemList”. If you want to extend something else, definitely okay. You can also extends your own script: “extends —Your gdscript Path—”
You can create a method that ItemList already has, and this way you will override this method.
Also, in ItemList, there’s a method “set_metadata(index, metadata)” where metadata can be anything, a node, a dictionary, and others. It might be helpful.
About the id in ItemList, it’s actually index. So when you rearranged the items, the index changes. You can use (set_metadata(index, metadata)) to record the item id (But you need to create a variable id in “metadata” that can be a dictionary or node, or just an int that represents your id)

This method was just what I was looking for. when adding new Items I’ll just make sure to pass their id with set_item_metadata(index, metadata) as well, thanks a lot

djangocoyote | 2020-02-11 21:28