❔ ✅ Update Dropdown options in Unity without loosing selected option

I have written a StatSelector class. The purpose is to let the user assign generated values to his character stats. It contains several dropdowns. A value can only be assigned once. Because of that I want to update all the dropdowns' options to remove any options I have selected in another dropdown already. Currently how I do it is iterate all the dropdowns except the one whose value was changed and clear it's options, then readd the options minus the selected option. This makes it so that any dropdowns that were already chosen forget which option was selected. How can I fix this?
32 Replies
Florian Voß
Florian VoßOP2y ago
public void ValueChanged(Dropdown dropdown, int index)
{
string selectedValue = possibleValues[index];
if(selectedValue == "-") return;
Dropdown selectedDropdown = dropdown;
Debug.Log(index);
dropdowns.Where(dropdown => dropdown != selectedDropdown).ToList().ForEach(dropdown => {
dropdown.ClearOptions();
dropdown.AddOptions(possibleValues.Where((value, idx ) => idx != index).ToList());
});
}
public void ValueChanged(Dropdown dropdown, int index)
{
string selectedValue = possibleValues[index];
if(selectedValue == "-") return;
Dropdown selectedDropdown = dropdown;
Debug.Log(index);
dropdowns.Where(dropdown => dropdown != selectedDropdown).ToList().ForEach(dropdown => {
dropdown.ClearOptions();
dropdown.AddOptions(possibleValues.Where((value, idx ) => idx != index).ToList());
});
}
Florian Voß
Florian VoßOP2y ago
I wish there would be a RemoveOption() method on the dropdown that would let me remove an option without clearing and readding all the options. Then my dropdowns would not forget what options was selected tho
Florian Voß
Florian VoßOP2y ago
The moment that I choose an option in a dropdown, all the others will get reset and loose their selected option.
Pobiega
Pobiega2y ago
well yeah, read the logic you are doing
dropdown.ClearOptions();
dropdown.AddOptions(possibleValues.Where((value, idx ) => idx != index).ToList());
dropdown.ClearOptions();
dropdown.AddOptions(possibleValues.Where((value, idx ) => idx != index).ToList());
that first one removes all options.
Florian Voß
Florian VoßOP2y ago
I know but I can't find an alternative approach to change the other dropdowns' options other than clearing them and readding
Pobiega
Pobiega2y ago
Well, that approach does come with the side effect of clearing all other dropboxes currently selected value 🙂 so either figure out a new way, or a workaround
Florian Voß
Florian VoßOP2y ago
that's pretty much what I asked for - A different way to update dropdown options other than clearing and readding RemoveOption() would be great, but there is no method like that
Pobiega
Pobiega2y ago
Just off the top of my head, maintain two lists (available, selected). Whenever a value is selected, remove the selected value from (available), add it to selected, and for all currently empty dropdowns, re-calculate their values you'll need a "reset" button or similar, if you want to start from scratch
Cattywampus
Cattywampus2y ago
is this legacy dropdown? if so, you can do this
for(int i = 0; i < myDrop.options.Count;i++)
{
if(myDrop.options[i].text == "myStingOption")
{
myDrop.RemoveAt(i);
break;
}
}
for(int i = 0; i < myDrop.options.Count;i++)
{
if(myDrop.options[i].text == "myStingOption")
{
myDrop.RemoveAt(i);
break;
}
}
Dropdown class uses List so you must iterate it either way you should track your selected option somewhere else like in a List or just a property
Florian Voß
Florian VoßOP2y ago
it should be the legacy Dropdown yes, but apperently there is no RemoveAt() method
Florian Voß
Florian VoßOP2y ago
Pobiega
Pobiega2y ago
does it have any Remove... methods at all?
Florian Voß
Florian VoßOP2y ago
nop
Pobiega
Pobiega2y ago
but yeah, please manage your state elsewhere and just let the UI components reflect your state. dont have them be source of truth
Cattywampus
Cattywampus2y ago
sorry, forgot misedd little details there... do this myDrop.options.RemoveAt(i)
Pobiega
Pobiega2y ago
some kinda OptionCollection?
Florian Voß
Florian VoßOP2y ago
that works, thank you
Cattywampus
Cattywampus2y ago
yes oh you're refering c# one, so no...
Florian Voß
Florian VoßOP2y ago
its a List<Dropdown.OptionData>
Pobiega
Pobiega2y ago
oh, a normal list? yea then it has both remove by index and by value
Florian Voß
Florian VoßOP2y ago
works as expected now, thank you both very much ❤️
Cattywampus
Cattywampus2y ago
aight, goodluck
Pobiega
Pobiega2y ago
don't forget to $close
MODiX
MODiX2y ago
Use the /close command to mark a forum thread as answered
Florian Voß
Florian VoßOP2y ago
I think I do already but I'm open for improvements. The state that the UI is supposed to display is represented by stats.values and managed by the Stats type. It generates random values for these different statistics as long as the sum of those values is lower than 60. The StatSelector is only supposed to display which stats were generated and let the user assign them. Then if he clicks Select my StatSelector calls a factory method passing what the user has selected
Cattywampus
Cattywampus2y ago
you can store the selected index myDropDow.value and later on you can just access the indexer myDropDown.options[myIndex]
Florian Voß
Florian VoßOP2y ago
now I'm not sure if I shall rather create a new thread for this but I'll just ask it here. I have an issue with my logic to remove selected options from the other dropdowns. If I compare the options by index there is the issue that after options got removed from certain dropdowns, same index may resolve to different options in different dropdowns. If I compare the options' strings by value, it also doesn't work properly because a value may appear in multiple options, for example there might be three options with the value "11", if i compare by value i would remove all of these options instead of the selected one from the other dropdowns. So what to do instead? Maybe compare the strings by reference? I've never done that 🤔 @Pobiega @SlimStv any ideas what I can do in that case?
Pobiega
Pobiega2y ago
Have a list be all available values, remove from that as things are selected? For dupe values, you only remove one of the value So it still has a valid state
Florian Voß
Florian VoßOP2y ago
but then that brings me back to the clearing and readding every dropdown on any change doesn't it? Rather than Removing an option
Pobiega
Pobiega2y ago
yeah, but you would only clear the ones that doesnt have a selected value eh would give so-so user experience for swapping things around idk UI logic is not my thing
Florian Voß
Florian VoßOP2y ago
I had it like that but changed to what I have now for some reason. I'll try that again then 🤔
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server