Thread Tools Display Modes
04/03/14, 06:12 PM   #1
tiomun
 
tiomun's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 25
SavedVars and functions

Still very new to lua so its probably all me lol

I am having some issues dealing with ZO_SavedVars. for example, I constantly receive a stack traceback error on the line for event manager. I believe I have a issue with how I am recalling the values from ZO_SavedVars.

So far google has not been friendly to me with this project. I have a tough time telling the lua code apart from apy function events and w/e else there is in code samples I'm referencing from multiple addons. Any help/tips is apreciated

Lua Code:
  1. -- Default Saved Variables
  2. MMO.defaults = {
  3.     -- Bag
  4.     OverideBag = true,
  5.     -- Map
  6.     OverideMap = true,
  7.     -- ActionBar
  8.     OverideActionBar = true,
  9.     -- Mouse
  10.     OverideMouse = true
  11.    
  12. }
  13.  
  14. -- Hook initialization onto the EVENT_ADD_ON_LOADED listener
  15. EVENT_MANAGER:RegisterForEvent( "MMO" , EVENT_ADD_ON_LOADED , MMO.Initialize )
  16.  
  17. function MMO.Initialize( )
  18.  
  19.     -- Register the slash command handler
  20.     SLASH_COMMANDS[MMO.command] = MMO.Slash
  21.        
  22.     -- Load saved variables
  23.     MMO.Vars = ZO_SavedVars:New( 'MMO_VARS' , math.floor( MMO.version * 100 ) , nil , MMO.defaults )
  24.    
  25.     if (MMO.Vars.OverideBag == true) then  
  26.         MMO.bag()
  27.     end
  28.    
  29.     if (MMO.Vars.OverideActionBar == true) then
  30.         MMO.actionbar()
  31.     end
  32.    
  33. end

Last edited by tiomun : 04/03/14 at 08:33 PM. Reason: Found the highlight lua option :)
  Reply With Quote
04/03/14, 06:19 PM   #2
Xrystal
caritas omnia vincit
 
Xrystal's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2014
Posts: 369
What is the stack traceback error you are getting ?

That may help highlight where things are going crazy on you.
  Reply With Quote
04/03/14, 08:32 PM   #3
tiomun
 
tiomun's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 25
Originally Posted by Xrystal View Post
What is the stack traceback error you are getting ?

That may help highlight where things are going crazy on you.
From my point of view its fairly vague. Unless theirs a better way of loging errors besides what comes up in the UI?

Code:
Checking type on argument callback failed in ScriptEventManagerRegisterForEventLua
stack traceback:
	[C]: in function 'RegisterForEvent'
	user:/AddOns/mmoui/mmoui.lua:27: in function '(main chunk)'
Theirs about 10+ lines of commenting nonsense above what I posted in the actual file so error line 27 is line 15 in the sample code above

Last edited by tiomun : 04/03/14 at 08:35 PM. Reason: more info
  Reply With Quote
04/03/14, 09:33 PM   #4
tiomun
 
tiomun's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 25
I was able to make some progress on this just with trial and error.

Basically I Changed the function names to MMO_xxx() Insread of MMO.xxx(),
added local infront of each function,
and Removed the RegisterForEvent and added a simple function call to initialize at the end.

Lua Code:
  1. -- Default Saved Variables
  2. MMO.defaults = {
  3.     -- Bag
  4.     OverideBag = true,
  5.     -- Map
  6.     OverideMap = true,
  7.     -- ActionBar
  8.     OverideActionBar = true,
  9.     -- Mouse
  10.     OverideMouse = true
  11.    
  12. }
  13.  
  14. local function MMO_Initialize()
  15.  
  16.     -- Register the slash command handler
  17.     SLASH_COMMANDS[MMO.command] = MMO_Slash
  18.        
  19.     -- Load saved variables
  20.     MMO.Vars = ZO_SavedVars:New( 'MMO_VARS' , math.floor( MMO.version * 100 ) , nil , MMO.defaults )
  21.    
  22.     if (MMO.Vars.OverideBag == true) then    
  23.         MMO_bag()
  24.     end
  25.    
  26.     --if (MMO_Vars.OverideActionBar == true) then    
  27.         --MMO_actionbar()
  28.     --end
  29.    
  30. end
  31.  
  32. local function MMO_Update()
  33.  
  34. end
  35.  
  36. local function MMO_Slash( text )
  37.     MMO_Message()
  38. end
  39.  
  40. local function MMO_Message()
  41.     d( "You are using ".. MMO.name .. " version " .. MMO.version )
  42.     d( MMO.name .. " configuration settings are in the game settings interface!" )
  43. end
  44.  
  45. MMO_Initialize()

This worked well while I had the call to the functions MMO_bag() commented out much like MMO_actionbar() is commented out.

MMO_bag() is in a separate file functions.lua at the moment whether it has any code in the function or not it throws this new error

user:/AddOns/mmoui/mmoui.lua:23: function expected instead of nil
stack traceback:
user:/AddOns/mmoui/mmoui.lua:23: in function 'MMO_Initialize'
user:/AddOns/mmoui/mmoui.lua:45: in function '(main chunk)'
and the code for function.lua

Lua Code:
  1. local function MMO_bag()
  2. --[[--
  3.     if BAG == nil then
  4.         local BAG = ZO_PlayerInventoryBackpack
  5.         BAG:SetCenterColor(0,0,0,0.5)
  6.         BAG:SetTexture(CT_BACKDROP)
  7.         BAG:SetEdgeColor(.1,.1,.1,1)
  8.         BAG:SetEdgeTexture("",8,1,2)
  9.     end
  10. --]]--
  11. end

Theirs something different about lua that I cant wrap my head around. basic to php and most languages give me little trouble. I am definitely missing something with how the functions work, or variable get passed around. lua examples I see online look very little like what is used in these addons....

Last edited by tiomun : 04/03/14 at 09:39 PM.
  Reply With Quote
04/04/14, 01:46 AM   #5
Xrystal
caritas omnia vincit
 
Xrystal's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2014
Posts: 369
Ok, I think I can see where you are going wrong.

If you bear in mind that some information is only accessible after the addon is loaded or the player is activated things will become easier to implement.


If you take a look at my gatherer addon you will see that at the top of the file I have some addon variables and at the bottom I have some functions that are using the event system and are called by the event system. You could almost copy those and just adjust for your addon to get it to work.


Try something like the following.


local addon_name = "..."
local addondata = {}


local function init_addondata()
addondata=zo_savedvars:new(....)
end


local function player_activated(eventid)
--Player is active and most things are available now
end


local function addon_loaded(eventid, addon)
if addon == addon_name then init_addondata()
EVENT_MANAGER:RegisterForEvent(addon_name,EVENT_PLAYER_ACTIVATED,player_activated)
end
end


EVENT_MANAGER:RegisterForEvent(addon_name,EVENT_ADDON_LOADED,addon_loaded)




Hope that helps. I use this as the base of all my addons.

Last edited by Xrystal : 04/04/14 at 02:12 AM.
  Reply With Quote
04/04/14, 07:05 AM   #6
tiomun
 
tiomun's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 25
That was a big help I do not have any errors showing up any more, and I think I have a bit of a grasp on the functions now... maybe... let me know if any of my commenting or anything looks off.

However, I cant get the slash command to work. haven't gotten past that point yet.

having "SLASH_COMMANDS[addonCommand] = addon_slash" in the init function doesn't work anymore, and it wont work if moved to any of the other functions....

Sorry to be such a noob on all this, and thanks again for the help.

Lua Code:
  1. local addonName = "MMOUI"
  2. local addonCommand  = "/mmo"
  3. local addonVersion  = 0.01
  4. local addonData = {}
  5.  
  6. -- Default Saved settings
  7. addonData.defaults = {
  8.     -- Bag
  9.     overideBag = true  
  10. }
  11.  
  12. -- Initialize addon data
  13. local function init_addondata()
  14.    
  15.     -- Slash command handler
  16.     SLASH_COMMANDS[addonCommand] = addon_slash
  17.    
  18.     -- Load saved variables
  19.     mmoVars = ZO_SavedVars:New( 'MMOUI_VARS' , math.floor( addonVersion * 100 ) , nil , addonData.defaults )   
  20.  
  21. end
  22.  
  23. -- Addon is active and can call runtime functions
  24. local function addon_activated( eventid )
  25.     -- do stuff
  26.     --if (mmoVars.overideBag == true) then
  27.         --mmo_bag()
  28.     --end
  29.  
  30. end
  31.  
  32. -- Load addon
  33. local function addon_loaded(eventid, addon)
  34.    
  35.     -- if addon is for this script trigger init_addondata
  36.     if addon == addon_name then init_addondata()
  37.         -- trigger player activated passes this eventid to addon_activated function
  38.         EVENT_MANAGER:RegisterForEvent(addonName,EVENT_PLAYER_ACTIVATED,addon_activated)
  39.     end
  40.        
  41. end
  42.  
  43. local function addon_slash( text )
  44.     d( "You are using ".. addonName .. " version " .. addonVersion )
  45.     d( addonName .. " configuration settings are in the game settings interface!" )
  46. end
  47.  
  48. -- trigger addon_loaded function, passes this eventid to function
  49. EVENT_MANAGER:RegisterForEvent(addonName,EVENT_ADD_ON_LOADED,addon_loaded)

Last edited by tiomun : 04/04/14 at 07:08 AM.
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » SavedVars and functions


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off