0 votes

I have code that adds a content into an array, through a series of check boxes, but want the user to be able to delete the specific item when they un-check the box. since the player can chose the options in a random order, I don't think using the index of the array is a good solution. What would be?

in Engine by (51 points)

Is there a reason you couldn't use erase() to remove the item you needed to remove?

Maybe I don't understand how erase() works, but what I did was arrayName.erase("variable I need to erase here") and it erases the item with index 0. I also cannot use indexes to reference items, since when I remove an item the index will change, and since the options can be picked in a random order, I can't assign items to a specific index.

I think as mentioned in the other answer, a dictionary would work, and would also be more consistent if you had settings being enabled or disabled, rather than added or deleted. I haven't seen your code, but I feel like that's typically easier to manage.

1 Answer

0 votes

I would use dictionary for keeping record:

var settings: {
"option1": true,
"option2": false,
"option3": 1,
"option4": 0 }

To add something just write settings.options5 = 1 and it will be created or overwritten if already present.
To delete smth use method erase("option2") though I think it's better to just switch it off, e.g. settings.options2 = 0

by (122 points)

Wow, thank you, seems a dictionary would work better in my case!

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.