ESOUI

ESOUI (https://www.esoui.com/forums/index.php)
-   Lua/XML Help (https://www.esoui.com/forums/forumdisplay.php?f=175)
-   -   Updating optionData in LAM2 (https://www.esoui.com/forums/showthread.php?t=9893)

Leonardo1123 08/26/21 07:30 PM

Updating optionData in LAM2
 
Hey there!

My addon has a list of outfits in the LAM2 settings page. After renaming an outfit, I've noticed it doesn't update in LAM2 until you reload UI.

I tried solving it by listening to the event EVENT_OUTFIT_RENAME_RESPONSE but with no luck.

Here's my code:
Lua Code:
  1. function LeonardosWardrobeManager:Initialize()
  2.     LeonardosWardrobeManager.savedVariables = ZO_SavedVars:NewCharacterIdSettings("LeonardosWardrobeManagerVars", LeonardosWardrobeManager.variableVersion, nil, LeonardosWardrobeManager.Default, GetWorldName())
  3.  
  4.     self.inCombat = IsUnitInCombat("player")
  5.     self.inStealth = GetUnitStealthState("player")
  6.  
  7.     for i=1,GetNumUnlockedOutfits() do
  8.         self.allOutfits[i + OUTFIT_OFFSET] = GetOutfitName(0, i)
  9.     end
  10.  
  11.     LAM2:RegisterAddonPanel("LeonardosWardrobeManagerOptions", panelData)
  12.     LAM2:RegisterOptionControls("LeonardosWardrobeManagerOptions", optionsData)
  13.  
  14.     EVENT_MANAGER:RegisterForEvent(self.name, EVENT_OUTFIT_RENAME_RESPONSE, self.OnOutfitRenamed)
  15.     EVENT_MANAGER:RegisterForEvent(self.name, EVENT_ZONE_UPDATE, self.OnZoneChange)
  16.     EVENT_MANAGER:RegisterForEvent(self.name, EVENT_PLAYER_COMBAT_STATE, self.OnPlayerCombatState)
  17.     EVENT_MANAGER:RegisterForEvent(self.name, EVENT_STEALTH_STATE_CHANGED, self.OnPlayerStealthState)
  18. end

Lua Code:
  1. function LeonardosWardrobeManager.OnOutfitRenamed(_, _, index) -- TODO: Still not working
  2.     LeonardosWardrobeManager.allOutfits[index + OUTFIT_OFFSET] = GetOutfitName(index + OUTFIT_OFFSET)
  3.     optionsData[3].controls[1].choices = LeonardosWardrobeManager.allOutfits
  4.     optionsData[3].controls[2].choices = LeonardosWardrobeManager.allOutfits
  5. end

Lua Code:
  1. optionsData = {
  2.     [1] = {
  3.         type = "description",
  4.         title = nil,
  5.         text = "Warning: If you rename an outfit, you will need to reload the UI for those changes to take effect here",
  6.         width = "full",
  7.     },
  8.     [2] = {
  9.         type = "dropdown",
  10.         name = "Default Outfit",
  11.         tooltip = "The outfit to be worn by default",
  12.         choices = LeonardosWardrobeManager.allOutfits,
  13.         getFunc = function() return LeonardosWardrobeManager.savedVariables.defaultOutfit end,
  14.         setFunc = function(var) LeonardosWardrobeManager.SetStateOutfit("DEFAULT", var) end,
  15.     },
  16.     [3] = {
  17.         type = "submenu",
  18.         name = "Combat",
  19.         tooltip = "Options related to combat and stealth", --(optional)
  20.         controls = {
  21.             [1] = {
  22.                 type = "dropdown",
  23.                 name = "Combat Outfit",
  24.                 tooltip = "The outfit to be switched to upon entering combat",
  25.                 choices = LeonardosWardrobeManager.allOutfits,
  26.                 getFunc = function() return LeonardosWardrobeManager.savedVariables.combatOutfit end,
  27.                 setFunc = function(var) LeonardosWardrobeManager.SetStateOutfit("COMBAT", var) end,
  28.             },
  29.             [2] = {
  30.                 type = "dropdown",
  31.                 name = "Stealth Outfit",
  32.                 tooltip = "The outfit to be switched to upon entering stealth",
  33.                 choices = LeonardosWardrobeManager.allOutfits,
  34.                 getFunc = function() return LeonardosWardrobeManager.savedVariables.stealthOutfit end,
  35.                 setFunc = function(var) LeonardosWardrobeManager.SetStateOutfit("STEALTH", var) end,
  36.             },
  37.         }
  38.     }
  39. }

Baertram 08/27/21 02:08 AM

This forum here is about asking questions to addons, not lua example code etc.
"The place to ask general questions about developing AddOns."

Moved to the correct forum
"The place to ask specific questions relating to Lua/XML coding."

Baertram 08/27/21 02:10 AM

There are other threads/posts about this already. Please use the forum search before posting new threads. Searching for LibAddonMenu or LAM would have provided them easily.

But basically you need to use the "reference" tag of the lam options to give it a name you can reference to.
And then use <referenceVariableOfDropdownBox>:UpdateChoices(listOfChoices, choicesValues, choicesTooltips)
choicesValues and choicesTooltips are optional. But I'd at least always provide choices and choicesValues so that the getfunc and setFunc uses the choicesValues easily, instead of having to check all entries in choices to match the string of your getFunc/setFunc parameter!

Searching for LAM or UpdateChoices (you could not know the later until now I assume :-)) will give you example forum threads, e.g.
https://www.esoui.com/forums/showthr...=UpdateChoices

I've added it to the Wiki as well now:
https://wiki.esoui.com/LibAddonMenu:...wn_box_entries

Leonardo1123 08/27/21 12:46 PM

Hey there! Sorry for not searching, definitely will in the future! So I feel like I'm super close but there's something basic I'm not understanding. Here's my attempt to implement what you suggested:

LAM
Lua Code:
  1. optionsData = {
  2.     [1] = {
  3.         type = "description",
  4.         title = nil,
  5.         text = "Warning: If you rename an outfit, you will need to reload the UI for those changes to take effect here",
  6.         width = "full",
  7.     },
  8.     [2] = {
  9.         type = "dropdown",
  10.         name = "Default Outfit",
  11.         tooltip = "The outfit to be worn by default",
  12.         choices = LeonardosWardrobeManager.allOutfits,
  13.         getFunc = function() return LeonardosWardrobeManager.savedVariables.defaultOutfit end,
  14.         setFunc = function(var) LeonardosWardrobeManager.SetStateOutfit("DEFAULT", var) end,
  15.         reference = "LeonardosWardrobeManager_Default_Dropdown"
  16.     },
  17.     [3] = {
  18.         type = "submenu",
  19.         name = "Combat",
  20.         tooltip = "Options related to combat and stealth", --(optional)
  21.         controls = {
  22.             [1] = {
  23.                 type = "dropdown",
  24.                 name = "Combat Outfit",
  25.                 tooltip = "The outfit to be switched to upon entering combat",
  26.                 choices = LeonardosWardrobeManager.allOutfits,
  27.                 getFunc = function() return LeonardosWardrobeManager.savedVariables.combatOutfit end,
  28.                 setFunc = function(var) LeonardosWardrobeManager.SetStateOutfit("COMBAT", var) end,
  29.                 reference = "LeonardosWardrobeManager_Combat_Dropdown"
  30.             },
  31.             [2] = {
  32.                 type = "dropdown",
  33.                 name = "Stealth Outfit",
  34.                 tooltip = "The outfit to be switched to upon entering stealth",
  35.                 choices = LeonardosWardrobeManager.allOutfits,
  36.                 getFunc = function() return LeonardosWardrobeManager.savedVariables.stealthOutfit end,
  37.                 setFunc = function(var) LeonardosWardrobeManager.SetStateOutfit("STEALTH", var) end,
  38.                 reference = "LeonardosWardrobeManager_Stealth_Dropdown"
  39.             },
  40.         }
  41.     }
  42. }

Update
Lua Code:
  1. function LeonardosWardrobeManager.OnOutfitRenamed(event, response, index) -- TODO: Still not working
  2.     for i=1,GetNumUnlockedOutfits() do
  3.         LeonardosWardrobeManager.allOutfits[i + OUTFIT_OFFSET] = GetOutfitName(0, i)
  4.     end
  5.     LeonardosWardrobeManager_Default_Dropdown:UpdateChoices(LeonardosWardrobeManager.allOutfits)
  6.     LeonardosWardrobeManager_Combat_Dropdown:UpdateChoices(LeonardosWardrobeManager.allOutfits)
  7.     LeonardosWardrobeManager_Stealth_Dropdown:UpdateChoices(LeonardosWardrobeManager.allOutfits)
  8. end

Event Registers
Lua Code:
  1. ...
  2.  
  3.     LAM2:RegisterAddonPanel("LeonardosWardrobeManagerOptions", panelData)
  4.     LAM2:RegisterOptionControls("LeonardosWardrobeManagerOptions", optionsData)
  5.  
  6.     EVENT_MANAGER:RegisterForEvent(self.name, EVENT_OUTFIT_RENAME_RESPONSE, self.OnOutfitRenamed)
  7.     EVENT_MANAGER:RegisterForEvent(self.name, EVENT_PLAYER_ACTIVATED, self.OnPlayerActivated)
  8.     EVENT_MANAGER:RegisterForEvent(self.name, EVENT_PLAYER_COMBAT_STATE, self.OnPlayerCombatState)
  9.     EVENT_MANAGER:RegisterForEvent(self.name, EVENT_STEALTH_STATE_CHANGED, self.OnPlayerStealthState)
  10. end


Quote:

Originally Posted by Baertram (Post 44641)
There are other threads/posts about this already. Please use the forum search before posting new threads. Searching for LibAddonMenu or LAM would have provided them easily.

But basically you need to use the "reference" tag of the lam options to give it a name you can reference to.
And then use <referenceVariableOfDropdownBox>:UpdateChoices(listOfChoices, choicesValues, choicesTooltips)
choicesValues and choicesTooltips are optional. But I'd at least always provide choices and choicesValues so that the getfunc and setFunc uses the choicesValues easily, instead of having to check all entries in choices to match the string of your getFunc/setFunc parameter!

Searching for LAM or UpdateChoices (you could not know the later until now I assume :-)) will give you example forum threads, e.g.
https://www.esoui.com/forums/showthr...=UpdateChoices

I've added it to the Wiki as well now:
https://wiki.esoui.com/LibAddonMenu:...wn_box_entries


Sharlikran 08/27/21 01:24 PM

I'm not 100% sure if I understand, so if I'm wrong please forgive me. The LAM menu was not designed with the intent to update or change anything. Meaning if you have a dropdown and the three names are Today, Yesterday, Tomorrow you can not add Next Week after you initialize LAM and then update the dropdown. Nor can you remove Tomorrow and replace it with Next Week.

I am currious because I see an update choices and you can't update them after you call RegisterOptionControls.

Leonardo1123 08/27/21 01:26 PM

Hmm, yes, that's exactly what I'm trying to do. So is it just an unavoidable shortcoming of my mod that after you rename an outfit you have to ReloadUI for that to take effect in LAM?

Quote:

Originally Posted by Sharlikran (Post 44644)
I'm not 100% sure if I understand, so if I'm wrong please forgive me. The LAM menu was not designed with the intent to update or change anything. Meaning if you have a dropdown and the three names are Today, Yesterday, Tomorrow you can not add Next Week after you initialize LAM and then update the dropdown. Nor can you remove Tomorrow and replace it with Next Week.

I am currious because I see an update choices and you can't update them after you call RegisterOptionControls.


Leonardo1123 08/27/21 10:10 PM

Soooooo I'm not entirely sure how I got this working, but I got it working, haha.

sirinsidiator 08/28/21 02:59 AM

Quote:

Originally Posted by Sharlikran (Post 44644)
I'm not 100% sure if I understand, so if I'm wrong please forgive me. The LAM menu was not designed with the intent to update or change anything. Meaning if you have a dropdown and the three names are Today, Yesterday, Tomorrow you can not add Next Week after you initialize LAM and then update the dropdown. Nor can you remove Tomorrow and replace it with Next Week.

I am currious because I see an update choices and you can't update them after you call RegisterOptionControls.

Not sure how you arrived from "Is it possible to add additional controls to a LAM options panel, after it has already been shown/opened once?" "nope. LAM wasn't made with dynamic menus in mind" at "LAM menu was not designed with the intent to update or change anything". xD
You can always update existing controls, including the choices in a dropdown. You just cannot add or remove controls after the menu was created.

Sharlikran 08/28/21 09:11 AM

Quote:

Originally Posted by sirinsidiator (Post 44648)
* LAM wasn't made with dynamic menus in mind"
* You can always update existing controls, including the choices in a dropdown.
* You just cannot add or remove controls after the menu was created.

That is what has me going around in circles.

sirinsidiator 08/28/21 09:30 AM

Quote:

Originally Posted by Sharlikran (Post 44650)
That is what has me going around in circles.

Ok. Guess if you ignore the context of my answer it is a bit confusing.
Let me try to restate it in a way that's harder to misunderstand.
LAM was not made with adding or removing controls in mind, so it's not possible to do that. You can however change values of existing controls in various ways which are outlined in the documentation on github.

Baertram 08/28/21 11:26 AM

Looks good so far except that you definately should use the 2nd parameter "choicesValues" in the UpdateChoices function as well.

And as the Wiki tells you here: https://wiki.esoui.com/LibAddonMenu:...ntrols_created
The LAM controls are NOT created ( means they will be nil ) until you open the LAM settings menu the first time!
If you want to update them via events like EVENT_OUTFIT_RENAME_RESPONSE you need to assure the controls exist before!
You cannot assume they exist at event_add_on_loaded or event_player_activated! They do not if you have not pressed ESC -> addon settings to show the LAM settings menu, which creates the panels of the registered addons then.

So make sure to check if the controls exist via ~= nil before trying to update them and ALWAYS pass in choices = LeonardosWardrobeManager.allOutfits (and like I said before also choicesValues = a table with the key = same as in LeonardosWardrobeManager.allOutfits but the value = the key again (a unique number). So that your setFunc and getFunc can use the key value instead of the name of the outfit to update your savedvariables! Names change! So use a unique key number instead via choicesValues parameter.

Quote:

Originally Posted by Leonardo1123 (Post 44643)
Hey there! Sorry for not searching, definitely will in the future! So I feel like I'm super close but there's something basic I'm not understanding. Here's my attempt to implement what you suggested:

LAM
Lua Code:
  1. optionsData = {
  2.     [1] = {
  3.         type = "description",
  4.         title = nil,
  5.         text = "Warning: If you rename an outfit, you will need to reload the UI for those changes to take effect here",
  6.         width = "full",
  7.     },
  8.     [2] = {
  9.         type = "dropdown",
  10.         name = "Default Outfit",
  11.         tooltip = "The outfit to be worn by default",
  12.         choices = LeonardosWardrobeManager.allOutfits,
  13.         getFunc = function() return LeonardosWardrobeManager.savedVariables.defaultOutfit end,
  14.         setFunc = function(var) LeonardosWardrobeManager.SetStateOutfit("DEFAULT", var) end,
  15.         reference = "LeonardosWardrobeManager_Default_Dropdown"
  16.     },
  17.     [3] = {
  18.         type = "submenu",
  19.         name = "Combat",
  20.         tooltip = "Options related to combat and stealth", --(optional)
  21.         controls = {
  22.             [1] = {
  23.                 type = "dropdown",
  24.                 name = "Combat Outfit",
  25.                 tooltip = "The outfit to be switched to upon entering combat",
  26.                 choices = LeonardosWardrobeManager.allOutfits,
  27.                 getFunc = function() return LeonardosWardrobeManager.savedVariables.combatOutfit end,
  28.                 setFunc = function(var) LeonardosWardrobeManager.SetStateOutfit("COMBAT", var) end,
  29.                 reference = "LeonardosWardrobeManager_Combat_Dropdown"
  30.             },
  31.             [2] = {
  32.                 type = "dropdown",
  33.                 name = "Stealth Outfit",
  34.                 tooltip = "The outfit to be switched to upon entering stealth",
  35.                 choices = LeonardosWardrobeManager.allOutfits,
  36.                 getFunc = function() return LeonardosWardrobeManager.savedVariables.stealthOutfit end,
  37.                 setFunc = function(var) LeonardosWardrobeManager.SetStateOutfit("STEALTH", var) end,
  38.                 reference = "LeonardosWardrobeManager_Stealth_Dropdown"
  39.             },
  40.         }
  41.     }
  42. }

Update
Lua Code:
  1. function LeonardosWardrobeManager.OnOutfitRenamed(event, response, index) -- TODO: Still not working
  2.     for i=1,GetNumUnlockedOutfits() do
  3.         LeonardosWardrobeManager.allOutfits[i + OUTFIT_OFFSET] = GetOutfitName(0, i)
  4.     end
  5.     LeonardosWardrobeManager_Default_Dropdown:UpdateChoices(LeonardosWardrobeManager.allOutfits)
  6.     LeonardosWardrobeManager_Combat_Dropdown:UpdateChoices(LeonardosWardrobeManager.allOutfits)
  7.     LeonardosWardrobeManager_Stealth_Dropdown:UpdateChoices(LeonardosWardrobeManager.allOutfits)
  8. end

Event Registers
Lua Code:
  1. ...
  2.  
  3.     LAM2:RegisterAddonPanel("LeonardosWardrobeManagerOptions", panelData)
  4.     LAM2:RegisterOptionControls("LeonardosWardrobeManagerOptions", optionsData)
  5.  
  6.     EVENT_MANAGER:RegisterForEvent(self.name, EVENT_OUTFIT_RENAME_RESPONSE, self.OnOutfitRenamed)
  7.     EVENT_MANAGER:RegisterForEvent(self.name, EVENT_PLAYER_ACTIVATED, self.OnPlayerActivated)
  8.     EVENT_MANAGER:RegisterForEvent(self.name, EVENT_PLAYER_COMBAT_STATE, self.OnPlayerCombatState)
  9.     EVENT_MANAGER:RegisterForEvent(self.name, EVENT_STEALTH_STATE_CHANGED, self.OnPlayerStealthState)
  10. end


Sharlikran 08/28/21 07:57 PM

Quote:

Originally Posted by sirinsidiator (Post 44651)
Ok. Guess if you ignore the context of my answer it is a bit confusing.

Not trying to do that sir. ^_^
Quote:

Originally Posted by sirinsidiator (Post 44651)
Let me try to restate it in a way that's harder to misunderstand.

I will experiment with it so I understand better.


All times are GMT -6. The time now is 01:11 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2014 - 2022 MMOUI