Thread Tools Display Modes
11/09/14, 06:36 AM   #1
brekal
Guest
Posts: n/a
UI Error - function expected instead of nil value

Hey,

I'm trying to make some Alchemy Addon to help me get an overview of the ingredients i have in my bag.
When logging into the game or reloading the UI it checks my backpack for some predefined Ingredients and saves the initial amount into a list.
As soon as a bagupdate happens, it checks if it is an ingredient and counts one to the existing value in the list.

The Function with the bagupdate works well but the function for getting the initial amount in my backpack throws an error.


The error is: function expected instead of nil value. The line is
Lua Code:
  1. LootManager:UpdateUI(name, count)

When I dump the two variables "name" and "count" they are defenitly not nil.

Here is the code of my lua-file:

Lua Code:
  1. LootManager = {}
  2. LootManager.name = "LootManager"
  3. LootManager.allCounts = {}
  4.  
  5. --
  6. --
  7.  
  8. function LootManager:Initialize()
  9.  
  10.     LootManager.allCounts["Kornblume"] = 0
  11.     LootManager.allCounts["Wiesenschaumkraut"] = 0
  12.     LootManager.allCounts["Wasserhyazinthe"] = 0
  13. end
  14.  
  15. --
  16. --
  17.  
  18. function LootManager:OnAddOnLoad(event, addonName)
  19.     if addonName == LootManager.name then
  20.         LootManager:Initialize()
  21.     end
  22. end
  23.  
  24. --
  25. --
  26.  
  27. function LootManager:GetBagOnInit()
  28.     local name
  29.     local itemName
  30.     local slotId
  31.     local count
  32.     local bagId = BAG_BACKPACK
  33.     local slots = GetBagSize(bagId)
  34.  
  35.     for i=0, slots - 1 do
  36.         slotId = i
  37.         itemName = GetItemName(bagId, slotId)
  38.         name = zo_strformat(SI_UNIT_NAME, itemName)
  39.         count = GetItemTotalCount(bagId, slotId)
  40.                
  41.         if name == "Wasserhyazinthe" then
  42.             LootManager.allCounts[name] = count
  43.             LootManager:UpdateUI(name, count)
  44.         end
  45.         if name == "Kornblume" then
  46.             d(name)
  47.             d(count)
  48.             LootManager.allCounts[name] = count
  49.             LootManager:UpdateUI(name, count)
  50.         end
  51.         if name == "Wiesenschaumkraut" then
  52.             LootManager.allCounts[name] = count
  53.             LootManager:UpdateUI(name, count)
  54.         end
  55.  
  56.     end
  57.    
  58.     LootManager:CheckMin()
  59.  
  60.     EVENT_MANAGER:UnregisterForEvent(LootManager.name, EVENT_PLAYER_ACTIVATED)
  61. end
  62.  
  63. --
  64. -- When you put something in your bag it looks for ingredients and adds it to a list and updates the ui
  65.  
  66. function LootManager:BagUpdate( bagId,  slotId,  isNewItem,  itemSoundCategory,  updateReason)
  67.  
  68.     local itemName = GetItemName(bagId, slotId)
  69.     local name = zo_strformat(SI_UNIT_NAME, itemName)
  70.     local totalCount = GetItemTotalCount(bagId, slotId)
  71.  
  72.     if name == "Wasserhyazinthe" then
  73.         LootManager:UpdateUI(name, totalCount)
  74.         LootManager.allCounts[name] = totalCount
  75.     end
  76.    
  77.     if name == "Kornblume" then
  78.         LootManager:UpdateUI(name, totalCount)
  79.         LootManager.allCounts[name] = totalCount
  80.     end
  81.    
  82.     if name == "Wiesenschaumkraut" then
  83.         LootManager:UpdateUI(name, totalCount)
  84.         LootManager.allCounts[name] = totalCount
  85.     end
  86.    
  87.     LootManager:CheckMin()
  88. end
  89.  
  90. --
  91. -- Check if the values are not nil - otherwise math.min() throws an error
  92.  
  93. function LootManager:CheckMin()
  94.     local countMin
  95.    
  96.     if LootManager.allCounts["Wasserhyazinthe"] == nil or LootManager.allCounts["Kornblume"] == nil or LootManager.allCounts["Wiesenschaumkraut"] == nil then
  97.         countMin = 0
  98.     else
  99.         countMin = math.min(LootManager.allCounts["Wasserhyazinthe"], LootManager.allCounts["Kornblume"], LootManager.allCounts["Wiesenschaumkraut"])
  100.     end
  101.         LootManager:UpdateUI("Trank", countMin)
  102. end
  103.  
  104. --
  105. --
  106.  
  107. function LootManager:UpdateUI(labelName, value)
  108.     if labelName == "Wiesenschaumkraut" then
  109.         LootManagerWindowWiesenschaumkrautCount:SetText(value)
  110.     end
  111.     if labelName == "Kornblume" then
  112.         LootManagerWindowKornblumeCount:SetText(value)
  113.     end
  114.     if labelName == "Wasserhyazinthe" then
  115.         LootManagerWindowWasserhyazintheCount:SetText(value)
  116.     end
  117.     if labelName == "Trank" then
  118.         LootManagerWindowTrankCount:SetText(value)
  119.     end
  120. end
  121.  
  122.  
  123. --
  124. --
  125.  
  126. EVENT_MANAGER:RegisterForEvent(LootManager.name, EVENT_ADD_ON_LOADED, LootManager.OnAddOnLoad)
  127. EVENT_MANAGER:RegisterForEvent(LootManager.name, EVENT_INVENTORY_SINGLE_SLOT_UPDATE, LootManager.BagUpdate)
  128. EVENT_MANAGER:RegisterForEvent(LootManager.name, EVENT_PLAYER_ACTIVATED, LootManager.GetBagOnInit)


Do you have any suggestions?

thanks in advance

Last edited by brekal : 11/09/14 at 12:47 PM.
  Reply With Quote
11/09/14, 09:27 AM   #2
merlight
AddOn Author - Click to view addons
Join Date: Jul 2014
Posts: 671
Originally Posted by brekal View Post
The error is: function expected instead of nil value. The line is
Lua Code:
  1. LootManager:UpdateUI(name, count)

When I dump the two variables "name" and "count" they are defenitly not nil.
function expected instead of nil. The error means LootManager.UpdateUI is nil. Here's a possible reason:

Lua Code:
  1. --  LootManager.savedVars = ZO_SavedVars:New("LootManager", 1, nil, LootManager.Default)
SavedVariables table needs to be global. So when the game loads your saved vars, it replaces your LootManager add-on table with saved vars table. For completeness, you'll also encounter this error if you have a control in XML named LootManager, they all share the global namespace (_G).
  Reply With Quote
11/09/14, 10:12 AM   #3
Garkin
 
Garkin's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 832
Your function which scans backpack wont work correctly. There are two issues: slotIndex starts with 0, and you should be scanning all bag slots not just numUsedSlots. It is because not all slots are full.

Lua Code:
  1. local name
  2. local itemName
  3. local count
  4. local bagId = BAG_BACKPACK
  5. local numSlots = GetBagSize(bagId)
  6.  
  7. for slotId = 0, numSlots - 1 do
  8.     itemName = GetItemName(bagId, slotId)

As for the UI error - how is your addon manifest look like? It seems that you use LootManager in your XML for top level window. If xml file is listed after lua file in the addon manifest, your LootManager table defined in lua will be overwritten by the top level window defined in xml.
  Reply With Quote
11/09/14, 12:26 PM   #4
brekal
Guest
Posts: n/a
thanks for your response,

@merlight: but I cant imagine that this is the reason because it is a comment.

@garkin:

I updated my first post with the whole lua-file - so everything shows up now.

my txt-file looks like this:

Lua Code:
  1. ## Title: LootManager
  2. ## Description: Alchemy Loot Manager
  3. ## Version: 1.0
  4. ## APIVersion: 100010
  5. ## SavedVariables: LootManager
  6.  
  7. LootManager.xml
  8. LootManager.lua

and the xml-file looks like this:

Lua Code:
  1. <GuiXml>
  2.     <Controls>
  3.         <TopLevelControl name="LootManagerWindow" clampedToScreen="true" mouseEnabled="true" movable="true">
  4.             <Dimensions x="200" y="100" />
  5.             <Anchor point="TOPLEFT" relativeTo="GuiRoot" relativePoint="CENTER" offsetX="-500" offsetY="-300" />
  6.                
  7.             <Controls>
  8.                 <Backdrop name="$(parent)Panel" edgeColor="ffffff" centerColor="00000000" alpha="0">
  9.                     <Dimensions x="200" y="100" />
  10.                     <Anchor point="TOPLEFT" relativeTo="$(parent)" relativePoint="TOPLEFT" />
  11.                     <Edge edgeSize="1" />
  12.                 </Backdrop>
  13.         <Label name="$(parent)Kornblume" font="ZoFontWinH5" color="ffffff" text="Kornblume" verticalAlignment="LEFT" horizontalAlignment="TOP"  alpha="0.85">
  14.             <Dimensions x="150" y="20" />
  15.             <Anchor point="TOPLEFT"  relativeTo="$(parent)Panel" relativePoint="TOPLEFT" offsetX="0" offsetY="0"/>
  16.         </Label>
  17.         <Label name="$(parent)Wiesenschaumkraut" font="ZoFontWinH5" color="ffffff" text="Wiesenschaumkraut" verticalAlignment="LEFT" horizontalAlignment="TOP"  alpha="0.85">
  18.             <Dimensions x="150" y="20" />
  19.             <Anchor point="TOPLEFT"  relativeTo="$(parent)Panel" relativePoint="TOPLEFT" offsetX="0" offsetY="20"/>
  20.                 </Label>
  21.                 <Label name="$(parent)Wasserhyazinthe" font="ZoFontWinH5" color="ffffff" text="Wasserhyazinthe" verticalAlignment="LEFT" horizontalAlignment="TOP"  alpha="0.85">
  22.             <Dimensions x="150" y="20" />
  23.             <Anchor point="TOPLEFT"  relativeTo="$(parent)Panel" relativePoint="TOPLEFT" offsetX="0" offsetY="40"/>
  24.                 </Label>
  25.                 <Label name="$(parent)Trank" font="ZoFontWinH5" color="ffffff" text="Trank" verticalAlignment="LEFT" horizontalAlignment="TOP"  alpha="0.85">
  26.             <Dimensions x="150" y="20" />
  27.             <Anchor point="TOPLEFT"  relativeTo="$(parent)Panel" relativePoint="TOPLEFT" offsetX="0" offsetY="80"/>
  28.                 </Label>
  29.                    
  30.                 <Label name="$(parent)KornblumeCount" font="ZoFontWinH5" color="ffffff" text="x ?" verticalAlignment="LEFT" horizontalAlignment="TOP"  alpha="0.85">
  31.             <Dimensions x="50" y="20" />
  32.             <Anchor point="TOPLEFT"  relativeTo="$(parent)Panel" relativePoint="TOPLEFT" offsetX="150" offsetY="0"/>
  33.         </Label>
  34.         <Label name="$(parent)WiesenschaumkrautCount" font="ZoFontWinH5" color="ffffff" text="x ?" verticalAlignment="LEFT" horizontalAlignment="TOP"  alpha="0.85">
  35.             <Dimensions x="50" y="20" />
  36.             <Anchor point="TOPLEFT"  relativeTo="$(parent)Panel" relativePoint="TOPLEFT" offsetX="150" offsetY="20"/>
  37.         </Label>
  38.         <Label name="$(parent)WasserhyazintheCount" font="ZoFontWinH5" color="ffffff" text="x ?" verticalAlignment="LEFT" horizontalAlignment="TOP"  alpha="0.85">
  39.             <Dimensions x="50" y="20" />
  40.             <Anchor point="TOPLEFT"  relativeTo="$(parent)Panel" relativePoint="TOPLEFT" offsetX="150" offsetY="40"/>
  41.         </Label>
  42.         <Label name="$(parent)TrankCount" font="ZoFontWinH5" color="ffffff" text="x ?" verticalAlignment="LEFT" horizontalAlignment="TOP"  alpha="0.85">
  43.             <Dimensions x="50" y="20" />
  44.             <Anchor point="TOPLEFT"  relativeTo="$(parent)Panel" relativePoint="TOPLEFT" offsetX="150" offsetY="80"/>
  45.         </Label>      
  46.             </Controls>
  47.         </TopLevelControl>
  48.     </Controls>
  49. </GuiXml>


btw - i deleted all the "saved-variables" stuff - because i dont need it now.

Last edited by brekal : 11/09/14 at 12:42 PM.
  Reply With Quote
11/09/14, 12:45 PM   #5
merlight
AddOn Author - Click to view addons
Join Date: Jul 2014
Posts: 671
Originally Posted by brekal View Post
@merlight: but I cant imagine that this is the reason because it is a comment.
I assumed you commented it after getting the error, to narrow it down. Also I was not entirely clear. The problem is not in the line, but in the fact you defined LootManager to be the name of SavedVariables table, here:

Originally Posted by brekal View Post
## SavedVariables: LootManager
Between loading your add-on Lua & XML files, and calling your ON_LOADED handler, the game reads your saved vars file and replaces the table.
  Reply With Quote
11/09/14, 02:08 PM   #6
brekal
Guest
Posts: n/a
hmmm ... ok - i didn't know that.
thx for this info.

nevertheless i still get the same error.

Last edited by brekal : 11/09/14 at 02:34 PM.
  Reply With Quote
11/09/14, 04:26 PM   #7
merlight
AddOn Author - Click to view addons
Join Date: Jul 2014
Posts: 671
I suggest you try d("LootManager:", LootManager) before the offending call. Perhaps you have another addon using the same global name; with the dump you should at least see what's in there.
  Reply With Quote
11/11/14, 01:39 PM   #8
brekal
Guest
Posts: n/a
finally, the error disappeared over night -_-

I didn't change anything and when I started the game today no error occured.

Thanks for your help. :-)
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » UI Error - function expected instead of nil value


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