View Single Post
01/31/15, 04:15 PM   #1
Baertram
Super Moderator
 
Baertram's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 5,014
Problem with local variable (it is NIL all of sudden)?

Hey developers,

I got a problem with a source code.
Inside my function I use the :GetName() ESO LUA function to get the name of a control.
The name is stored inside the local (only known inside my function) variable "equipmentMarkerControlName"
I'll then determine the digits at the end of the string and remove them.
This works without any problems and the variable will store the new string afterwards, without trailing digits. I know there might be simpler ways to strip the trailing digits in this string but I'm not really well in using RegEx :-(

Afterwards I will use a loop to read an array (markedIcons) and if the iconid from the array differs from a function's parameter (markerId) I try to write the variable "equipmentMarkerControlName" to the chat again.
But it is NIL again here all of sudden? Why?

Lua Code:
  1. --Are we adding a tooltip to an equipment slot?
  2.         if pUpdateAllEquipmentTooltips then
  3.             --Get current controls name
  4.             local equipmentMarkerControlName = markerControl:GetName()
  5.             --Get the offset for the ending digit(s)
  6.             local replaceEnd = string.len(equipmentMarkerControlName) - locVars.gFCOMaxDigitsForIcons     -- locVars.gFCOMaxDigitsForIcons = 1
  7.             --Remove the ending number
  8.             equipmentMarkerControlName = string.sub(equipmentMarkerControlName, 1, replaceEnd)
  9.             d("name: " .. equipmentMarkerControlName)
  10.         end
  11.  
  12.         --Check if the item is marked with several icons
  13.         local markedIcons = {}
  14.         local bagId, slotIndex = MyGetItemDetails(markerControl:GetParent())
  15.         _, markedIcons = FCOIsMarked(GetItemInstanceId(bagId, slotIndex), -1)
  16.         for iconId, iconIsMarked in pairs(markedIcons) do
  17.             if iconIsMarked then
  18.                 markedCounter = markedCounter + 1
  19.                 if markedCounter > 1 then
  20.  
  21. --The variable will be NIL inside this FOR loop, but why?
  22.                 if iconId ~= markerId and pUpdateAllEquipmentTooltips then
  23.                     if equipmentMarkerControlName ~= "" then
  24.                     d("bla: " .. tostring(equipmentMarkerControlName))
  25.                     else
  26.                     d("blubb")
  27.                     end
  28.                 end
  29.             end
  30.         end

Chat output will be:
Lua Code:
  1. name: ZO_CharacterEquipmentSlotsRing2FCOIS
  2. bla: nil
  3. bla: nil

The function will be called once!
For the given example the loop at array "markedIcons" generated 2 chat output rows, because iconIsMarked was true 2 times.

I can't see why this variable is NIL. It should contain the string ZO_CharacterEquipmentSlotsRing2FCOIS :-(

If I declare the variable "equipmentMarkerControlName" as global it works all of sudden.
But why do I have to declare a variable global which I'm using locally inside the same function???
Is is because I'm using a global function (FCOIsMarked(...)) inside my function? I don't think so...

Thanks for your help.
  Reply With Quote