WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   AddOn Help/Support (https://www.wowinterface.com/forums/forumdisplay.php?f=3)
-   -   Need help loading SavedVariable setting. (https://www.wowinterface.com/forums/showthread.php?t=57341)

Sojiro84 08-10-19 09:21 AM

Need help loading SavedVariable setting.
 
I searched around and I have tried a dozen things but somehow, I can't get it to work.
I can get the value 1 or 3 saved, but somewhere with reading it back, it goes wrong but I am unsure why.

Here is the AddOns code:

Code:


WorldMapFrame:SetScale(0.8)

Code:

WorldMapFrame.BlackoutFrame.Blackout:SetAlpha(0)

if SimpleMap == 1 then
    PlayerMovementFrameFader.AddDeferredFrame(WorldMapFrame, .3, 3.0, .5)
elseif SimpleMap == 3 then
    PlayerMovementFrameFader.AddDeferredFrame(WorldMapFrame, 1, 3.0, .5)
end


WorldMapFrame.ScrollContainer.GetCursorPosition = function(f)
    local x,y = MapCanvasScrollControllerMixin.GetCursorPosition(f);
    local s = WorldMapFrame:GetScale();
    return x/s, y/s;
end

local function SimpleMapCommands(msg, editbox)
  if msg == 'off' then
    print('SimpleMap fade has been turned off.')
    PlayerMovementFrameFader.AddDeferredFrame(WorldMapFrame, 1, 3.0, .5)
    SimpleMap = 3
  elseif msg == 'on' then
    print("SimpleMap fade has been turned on.")
    PlayerMovementFrameFader.AddDeferredFrame(WorldMapFrame, .3, 3.0, .5)
    SimpleMap = 1
  end
end


SLASH_SMFADE1 = '/smfade'

SlashCmdList["SMFADE"] = SimpleMapCommands



And here is the .toc.

Code:


## Interface: 11302
## Version: 1.1
## Title: SimpleMap
## Notes: Smaller worldmap and it fades the map when moving
## Author: Sojiro84
## Dependencies: Blizzard_WorldMap
## SavedVariables: SimpleMap

SimpleMap.lua

Code:




The savedvariable contrains either "SimpleMap = 1" or "SimpleMap = 3".

Somehow the following doesn't trigger:

Code:

if SimpleMap == 1 then
    PlayerMovementFrameFader.AddDeferredFrame(WorldMapFrame, .3, 3.0, .5)
elseif SimpleMap == 3 then
    PlayerMovementFrameFader.AddDeferredFrame(WorldMapFrame, 1, 3.0, .5)
end



I tried "AddOn Loaded" events and saying that variable "SimpleMap = SimpleMap" so that the value is loaded but nothing.

Clearly SimpleMap isn't being assigned with the SavedVariable value.

Can anyone help me?

It feels like the solution should be simple but I never made a WoW AddOn before so a bit difficult for me!

Rilgamon 08-10-19 09:43 AM

https://wow.gamepedia.com/Saving_var..._game_sessions

Quote:

Saved variables are loaded after the addon code is executed
They cannot be accessed immediately, and will overwrite any "defaults" the addon may place in the global environment during its loading process.

Sojiro84 08-10-19 09:49 AM

Quote:

Originally Posted by Rilgamon (Post 333120)

Yeah, I found that page and many others. I played around with those statements but it ended up breaking the whole addon or still not loading my value.


What am I missing?

Rilgamon 08-10-19 10:02 AM

The addon loading process loads the file(s), executes the stuff that is placed in the main code-flow and after that only events can trigger execution of your code.

Any code that is executed before ADDON_LOADED with the argument of your own addon cant access your SV.
You have to listen for this event. And only after this event occurs make sure it is fired for your addon and setup your stuff after that.

Fizzlemizz 08-10-19 10:06 AM

Lua Code:
  1. WorldMapFrame.BlackoutFrame.Blackout:SetAlpha(0)
  2.  
  3. WorldMapFrame.ScrollContainer.GetCursorPosition = function(f)
  4.     local x,y = MapCanvasScrollControllerMixin.GetCursorPosition(f);
  5.     local s = WorldMapFrame:GetScale();
  6.     return x/s, y/s;
  7. end
  8.  
  9. local function SimpleMapCommands(msg, editbox)
  10.   if msg == 'off' then
  11.     print('SimpleMap fade has been turned off.')
  12.     PlayerMovementFrameFader.AddDeferredFrame(WorldMapFrame, 1, 3.0, .5)
  13.     SimpleMap = 3
  14.   elseif msg == 'on' then
  15.     print("SimpleMap fade has been turned on.")
  16.     PlayerMovementFrameFader.AddDeferredFrame(WorldMapFrame, .3, 3.0, .5)
  17.     SimpleMap = 1
  18.   end
  19. end
  20.  
  21.  
  22. SLASH_SMFADE1 = '/smfade'
  23.  
  24. SlashCmdList["SMFADE"] = SimpleMapCommands
  25.  
  26. local f = Createframe("Frame")
  27. f:SetScript("OnEvent", function(self, event, ...)
  28.     if SimpleMap == 1 then
  29.         PlayerMovementFrameFader.AddDeferredFrame(WorldMapFrame, .3, 3.0, .5)
  30.     elseif SimpleMap == 3 then
  31.         PlayerMovementFrameFader.AddDeferredFrame(WorldMapFrame, 1, 3.0, .5)
  32.     end
  33. end)
  34. f:RegisterEvent("PLAYER_LOGIN")

Sojiro84 08-10-19 10:19 AM

Thank you both for the suggestions, but even with my whole script inside the addon_loaded event (with checking for my addon name), it still doesn't work.

Hell, my whole AddOn just doesn't work at all when I do it inside/after the addon_loaded event.


It was worth a shot.

Fizzlemizz 08-10-19 11:16 AM

SavedVariables are initially nil so if they require meaning other than that then you need to initialise them first time in.

PLAYER_LOGIN event
Lua Code:
  1. if not SimpleMap then
  2.     SimpleMap = 1
  3.     -- or SimpleMap = {}
  4.     -- or SimpleMap = Whatever
  5. end

Seerah 08-10-19 03:05 PM

Or, an edit to Fizzlemizz's code (that is more relevant to what you are learning):
Lua Code:
  1. local f = CreateFrame("Frame")  --oh, and there was a typo here
  2. f:SetScript("OnEvent", function(self, event, addon)
  3.     if addon == "YourAddonNameHere" then
  4.         if SimpleMap == 1 then
  5.             PlayerMovementFrameFader.AddDeferredFrame(WorldMapFrame, .3, 3.0, .5)
  6.         elseif SimpleMap == 3 then
  7.             PlayerMovementFrameFader.AddDeferredFrame(WorldMapFrame, 1, 3.0, .5)
  8.         end
  9.     end
  10. end)
  11. f:RegisterEvent("ADDON_LOADED")

https://wow.gamepedia.com/AddOn_loading_process

Fizzlemizz 08-10-19 04:12 PM

I like PLAYER_LOGIN because you don't have to check for an addon name, it only fires once and that one time after all addons have loaded and before PLAYER_ENTERING_WORLD. Then again, I'm lazy :)

Sojiro84 08-10-19 04:20 PM

Thanks all for the help. I managed to get it working with the "PLAYER_LOGIN" event!


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

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