The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

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?

in Engine by (97 points)
edited by

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)

by (783 points)
selected by

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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.