0 votes

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?

asked Feb 9 in Engine by djangocoyote (55 points)
edited Feb 10 by djangocoyote

1 Answer

0 votes
Best answer

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)

answered Feb 10 by Sween123 (758 points)
selected Feb 11 by djangocoyote

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.