Thread Tools Display Modes
08-08-15, 05:57 PM   #1
Nimhfree
A Frostmaul Preserver
AddOn Author - Click to view addons
Join Date: Aug 2006
Posts: 267
Issue with variable scope

Hi, I have a problem that someone can probably explain to me pretty easily, but my head is a little fuzzy at the moment from lack of sleep.

In the following code I have a variable followerName declared outside of the for loop. I insert a new table into t with a function f defined to pass in followerName to the call to SetMapAreaQuests. What happens is whenever that function f is called, the value that is passed in for followerName is always the last one that was in the Grail.followerMapping as it passed through the for loop.

Code:
local followerInfo, followerName, qualityLevel
for questId, followerId in pairs(Grail.followerMapping) do
	if Grail:MeetsRequirementFaction(questId) then
		followerInfo = C_Garrison.GetFollowerInfo(followerId)
		followerName = followerInfo.name
		qualityLevel = followerInfo.quality
		tinsert(t, { sortName = followerName, displayName = ITEM_QUALITY_COLORS[qualityLevel].hex..followerName.."|r", mapID = 0, f = function() Grail:SetMapAreaQuests(0, followerName, { questId }) Wholly.zoneInfo.panel.mapId = 0 Wholly._ForcePanelMapArea(Wholly, true) CloseDropDownMenus() end })
	end
end
A fix that seems to work properly is to change the scope of the followerName variable such that is is defined within the loop itself as the following code shows:

Code:
local followerInfo, qualityLevel
for questId, followerId in pairs(Grail.followerMapping) do
	if Grail:MeetsRequirementFaction(questId) then
		followerInfo = C_Garrison.GetFollowerInfo(followerId)
		local followerName = followerInfo.name
		qualityLevel = followerInfo.quality
		tinsert(t, { sortName = followerName, displayName = ITEM_QUALITY_COLORS[qualityLevel].hex..followerName.."|r", mapID = 0, f = function() Grail:SetMapAreaQuests(0, followerName, { questId }) Wholly.zoneInfo.panel.mapId = 0 Wholly._ForcePanelMapArea(Wholly, true) CloseDropDownMenus() end })
	end
end
Can someone enlighten me as to why this is the case?
  Reply With Quote
08-08-15, 06:30 PM   #2
MunkDev
A Scalebane Royal Guard
 
MunkDev's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2015
Posts: 431
That's because you're referencing the original followerName, not the value it held at the time you created the instance of the function. Since the original followerName was last overwritten by the last entry in Grail.followerMapping, all the functions will refer to that data. Think memory address, not value.

In your second code snippet you create a separate instance of followerName for each iteration, meaning each followerName has its own memory assigned and its own address. If you don't use the variables outside the for loop then there really is no reason to reference outside that scope.
__________________

Last edited by MunkDev : 08-08-15 at 06:34 PM.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Issue with variable scope


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