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
04/04/14, 07:17 AM   #7
Cr4x
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 11
Hey,

line 36 your if compares to variables

addon: which is the 2. paramater < OK
addon_name: i cant find any variable with that name < I Expect you want to use addonName.

Cheers
  Reply With Quote
04/04/14, 08:34 AM   #8
Xrystal
caritas omnia vincit
 
Xrystal's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2014
Posts: 369
I have slash commands in my gatherer addon so feel free to look at it. I have put comments in there as well. When I get home tonight I will extract that part of it for you.
  Reply With Quote
04/04/14, 11:27 PM   #9
tiomun
 
tiomun's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 25
Cr4x good eye thanks

So I looked over the gatherer addonon but that didn't help much with the slash command issue. The problem was still with my functions, and the variables being passes between them. After digging around for a few hours online and looking at different tutorials I realised I was assuming there was something different about lua that I didn't know, but its not actually different in any way, all the source code I was looking at was confusing me due to the different styles of coding people have. Xrystal yours was what pointed me towards that fact since it was similar to how I read/write code. more so than others. After I get a grasp of a language I'm usually fine reading it no matter who created it, but starting out it can be difficult.

So here is what I have working for me right now. It handles the slash command and the triggering of functions in the functions.lua script I started just fine. functions just output text to the chat as they are executed for now but the text will be replaced with some actual api content soon to learn/test more on

main.lua
Lua Code:
  1. name = "mmoui"
  2. nameupper = "MMOUI"
  3. command = "/mmo"
  4. version = 0.01
  5. saveddata = "mmoui_SavedData"
  6. default = {}
  7. data = {}
  8.  
  9. function slashcommand( )
  10.     d("You are using " .. nameupper .. " version " .. version)
  11.     d("Configuration settings are in the game settings interface!")
  12. end
  13.  
  14. function initilize(eventid,addon)
  15.     SLASH_COMMANDS[command] = slashcommand
  16.     if addon ~= name then return end
  17.     EVENT_MANAGER:UnregisterForEvent(name,eventid)
  18.     EVENT_MANAGER:RegisterForEvent( name , EVENT_PLAYER_ACTIVATED , activated )
  19. end
  20.  
  21. function activated()
  22.     d("Loaded " .. nameupper .. " version " .. version)
  23.     mmouibag()
  24.     -- do stuff
  25.  
  26. end
  27.  
  28. -- trigger AddOnLoaded function, passes this eventID to function
  29. EVENT_MANAGER:RegisterForEvent( name , EVENT_ADD_ON_LOADED , initilize )

functions.lua
Lua Code:
  1. function mmouibag()
  2.  
  3.     d("Loaded BAG")
  4.  
  5. end

Thank you both for all the assistance
  Reply With Quote
04/05/14, 05:25 AM   #10
Xrystal
caritas omnia vincit
 
Xrystal's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2014
Posts: 369
Glad it helped and yes, I get somewhat confused when looking at other code myself.

I'll extract a small segment of my slash code and see if that helps you a bit better than my huge block of code I have in the addon.
  Reply With Quote
04/05/14, 05:54 AM   #11
Xrystal
caritas omnia vincit
 
Xrystal's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2014
Posts: 369
This is the first step for slash commands. Either have this line in your initialising function or call the function there. In essence it is saying, watch for /xrga input and process it with the function ProcessSlashCommands.

Lua Code:
  1. -- Initialise slash Commands
  2. local function InitSlashCommands(...)
  3.     SLASH_COMMANDS["/xrga"] = ProcessSlashCommands
  4. end

option is anything that appears after /xrga. So first we need to extract what is typed afterwards. This extraction code extracts up to 2 phrases from the command line so to speak. For example /xrga alert is one phrase 'alert' which becomes option[1]. /xrga zone kenarthi's roost is treated as two phrases 'zone' as option[1] and 'kenarthi's roost' as option[2].

It next tests to see if it finds no phrases so that #options equate to 0 or you type 'help' it lists the available options on screen.

It then processes the options it found and carries out the code to process them.

The two examples I have listed shows the 1 phrase option and 2 phrases option in action. /xrga update to generate the harvest data into a simple table for easy access bypassing the account information not needed for the addon to work. /xrga zone [zonename] uses that simplified harvest data to extract the information related to the zone requested, the current zone if none is requested and if somehow it thinks you are not in a zone it will list everything

Lua Code:
  1. -- Process the slash commands
  2. local function ProcessSlashCommands(option)
  3.  
  4.     -- Extract Options
  5.     local options = {}
  6.     local searchResult = { string.match(option,"^(%S*)%s*(.-)$") }
  7.     for i,v in pairs(searchResult) do
  8.         if (v ~= nil and v ~= "") then
  9.             options[i] = string.lower(v)
  10.         end
  11.     end
  12.  
  13.     -- Show what slash commands are available
  14.     if #options == 0 or options[1] == "help" then
  15.         CHAT_SYSTEM:AddMessage("XrysGatherer Slash Commands")
  16.         CHAT_SYSTEM:AddMessage("/xrga alert - toggle the harvest alert window")
  17.         return
  18.     end
  19.  
  20.     -- Execute functionality based on user requests
  21.  
  22.     -- Refresh the Harvest History table
  23.     if options[1] == "update" then
  24.         UpdateHarvestHistory()
  25.  
  26.     -- Display items gatherered in the selected zone or the current zone
  27.     elseif options[1] == "zone" then
  28.         local testItem = options[2] or GetUnitZone("player")
  29.         if testItem then
  30.             ChatMsg:AddMessage(string.format("Listing history for zone: %s",testItem))
  31.         else
  32.             ChatMsg:AddMessage("Listing history for all zones")
  33.         end
  34.         for historyIndex,historyData in pairs(harvestHistory) do
  35.             for zoneIndex,zoneData in pairs(historyData) do
  36.                 if testItem and string.lower(zoneIndex) == string.lower(testItem) or not testItem then
  37.                     for itemIndex,itemData in pairs(zoneData) do
  38.                         for indexIndex,indexData in pairs(itemData) do
  39.                             ChatMsg:AddMessage('Display the appropriate information here')
  40.                         end
  41.                     end
  42.                 end
  43.             end
  44.         end
  45. etc...

I hope that helps you figure that part out.
  Reply With Quote
04/05/14, 09:49 AM   #12
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Feb 2014
Posts: 648
@tiomun: Please do not use generically named global variables/names for your functions. Make them local if possible, or at least name them better.
  Reply With Quote
04/05/14, 03:37 PM   #13
tiomun
 
tiomun's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 25
@Seerah Don't worry. I wouldn't publish anything like that. its just experimentation and learning for the moment.


@Xrystal Thank you I am getting into the more elaborate chat commands as I work with the settings now.
  Reply With Quote
04/10/14, 11:18 PM   #14
tiomun
 
tiomun's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 25
Post

Thought I'd share my slash function now that I have it working.

When the slash command is entered this checks for a trailing command. The value of the command is then converted to the arrays key name that defines the message to be displayed, It also generates a function name which will be executed if a function by that name exists.

To avoid picking up a normal function in the addon, these dynamically triggered functions use a unique nomenclature. RVD_ instead of RV_ in this example.

This is just a snipet of the slash command code. not the complete addon script. there are earlier failsafes to prevent the execution of this affecting other addons

Lua Code:
  1. RVSLASH = {
  2.     default = RVNAME .." version " .. RVVERSION .. " \nType /rv help for a list of commands \nControls are in the settings interface!",
  3.     test = "Test sub command",
  4.     help = "List of commands",
  5. }
  6.  
  7. function RVD_TEST()
  8.     d("Function ok")
  9. end
  10.  
  11. function RV_SLASH(command)
  12.     local RVSUBCOM = {}
  13.     local rvSlashSearch = { string.match(command,"^(%S*)%s*(.-)$") }
  14.     for i,v in pairs(rvSlashSearch) do
  15.         if (v ~= nil and v ~= "") then
  16.             RVSUBCOM[i] = string.lower(v)
  17.         end
  18.     end
  19.     if RVSUBCOM[1] then
  20.         if RVSLASH[ RVSUBCOM[1] ] then
  21.             local RVFUNC = "RVD_" .. string.upper(RVSUBCOM[1])
  22.             if type ( _G[RVFUNC] ) == 'function' then
  23.                 _G[RVFUNC]()
  24.             end
  25.             d( RVSUBCOM[1] .. ": " .. RVSLASH[ RVSUBCOM[1] ] )
  26.         else
  27.             d(RVSLASH.default)
  28.         end
  29.     else
  30.         d(RVSLASH.default)
  31.     end
  32. end

It took a bit of playing with, but I am happy with the results. Thank you again for all the help

Last edited by tiomun : 04/10/14 at 11:26 PM.
  Reply With Quote

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

Thread Tools
Display Modes

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