Thread Tools Display Modes
11-01-15, 11:46 PM   #1
mster50
A Murloc Raider
Join Date: Nov 2015
Posts: 5
Weird variable scoping... need a second pair of eyes

Hello all,

I'm writing the beginnings of a unit frame addon. Tonight, while writing the boilerplate setup, I encountered something really strange that I can't seem to find an answer to.

My setup is this:
I have a library file that I am loading in advance to define empty globals that other files will then manipulate:
"GeoUnitFrames.lib.lua"
This file looks like this:
Code:
--root GUF object - all method definitions should fall under this global
GUF = {};

--root images folder
GUF.images = "Interface\\Addons\\GeoUnitFrames\\images\\prod\\";

--root Core object
GUF.Core = {};

--root Frames object - all Frames files will create methods under this object
GUF.Frames = {};

GUF.Utils = {};
GUF.Core, GUF.Frames, and GUF.Utils are managed by their own files. My issue is happening when attempting to pass a frame that is created in GUF.Frames:
Code:
--set our identity
self = GUF.Core;

--register our addon message prefix
RegisterAddonMessagePrefix("GEO");
print("Geo Unit Frames load was successful.\n\n");

self.playerFrame = GUF.Frames.Circles:testCircle();
print(self.playerFrame);
GUF.Utils:MakeFrameMovable(self.playerFrame);

function self.ConsoleCommand(cmd)
	print("Your command was: " .. cmd);
end

SlashCmdList["GEO"] = self.ConsoleCommand;
SLASH_GEO1 = "/geo";
When self.playerFrame is being created in GUF.Core, from the GUF.Frames.Circles:testCircle(); call, I'm printing the table ID, and then handing the table to GUF.Utils.MakeFrameMovable (to make the frame well, movable). The issue is that the table, once passed, has a different ID inside of the GUF.Utils.MakeFrameMovable call. The function errors when calling SetClampedToScreen on the frame, claiming that the call is nil.

Really scratching my head on this. No idea what's causing the problem. For reference, here is the GUF.Utils file's contents:

Code:
self = GUF.Utils;

self.Classes = {
	["Unknown"] = {name = "Unknown", id = 0},
	["Warrior"] = {name = "Warrior", id = 1},
	["Paladin"] = {name = "Paladin", id = 2};
	["Hunter"] = {name = "Hunter", id = 3};
	["Rogue"] = {name = "Rogue", id = 4};
	["Priest"] = {name = "Priest", id = 5};
	["DeathKnight"] = {name = "Death Knight", id = 6};
	["Shaman"] = {name = "Shaman", id = 7};
	["Mage"] = {name = "Mage", id = 8};
	["Warlock"] = {name = "Warlock", id = 9};
	["Monk"] = {name = "Monk", id = 10};
	["Druid"] = {name = "Druid", id = 11};
};

--colors as defined here http://wowwiki.wikia.com/wiki/Class_colors
self.Classes.Colors = {
	-- unknown / no class
	[self.Classes["Unknown"].id] = { r = 1, g = 1, b = 1},
	--warrior
	[self.Classes["Warrior"].id] = { r = 0.78, g = 0.61, b = 0.43},
	--paladin
	[self.Classes["Paladin"].id] = { r = 0.96, g = 0.55, b = 0.73},
	--hunter
	[self.Classes["Hunter"].id] = { r = 0.67, g = 0.83, b = 0.45},
	--rogue
	[self.Classes["Rogue"].id] = { r = 1.00, g = 0.96, b = 0.41},
	--priest
	[self.Classes["Priest"].id] = { r = 1, g = 1, b = 1},
	--deathknight
	[self.Classes["DeathKnight"].id] = { r = 0.77, g = 0.12, b = 0.23},
	--shaman
	[self.Classes["Shaman"].id] = { r = 0.00, g = 0.44, b = 0.87},
	--mage
	[self.Classes["Mage"].id] = { r = 0.41, g = 0.80, b = 0.94},
	--warlock
	[self.Classes["Warlock"].id] = { r = 0.58, g = 0.51, b = 0.79},
	--monk
	[self.Classes["Monk"].id] = { r = 0.33, g = 0.54, b = 0.52},
	--druid
	[self.Classes["Druid"].id] = { r = 1.00, g = 0.49, b = 0.04}
}

self.MyClass = self.Classes["Unknown"];

unitClassTable = ({UnitClass("player")});

--assign colors to our classes table, assign MyClass
for key, value in pairs(self.Classes) do
	value.colors = self.Classes.Colors[value.id];
	if(value.id == unitClassTable[3]) then
		self.MyClass = value;
	end
end

--frame setup
function self.SetAsTargetFrame(unitFrame)
	unitFrame:SetFrameRef("TargetFrame", TargetFrame);
end

function self.MakeFrameMovable(frame, button)
	print(frame);
	local btnString = "LeftButton"; --default, left mouse button
	if button then
		btnString = button;
	end

	--don't allow images to go off screen
	frame:SetClampedToScreen(true);
	frame:SetMovable(true);
	frame:EnableMouse(true);
	frame:RegisterForDrag(btnString);
	frame:SetScript("OnDragStart", function(self)
		if IsShiftKeyDown() then
			self:StartMoving();
		end
	end)
	frame:SetScript("OnDragStop", function(self)
		self:StopMovingOrSizing();
	end)
end
Also, here is my TOC file:
Code:
## Title: Geo Unit Frames
## Version: 0.01-a
## Author: John Rubin & DJ Foreman
## Interface: 60200
## SavedVariablesPerCharacter: gufPlayerData
## SavedVariables:
## Notes: Much Unit Frames, Such Geometry, Wow
GeoUnitFrames.lib.lua
Frames\GeoUnitFrames.frames.circles.lua
Utils\GeoUnitFrames.utils.lua
GeoUnitFrames.core.lua
And finally, the actual error:
http://imgur.com/VAGMOyy


Any help would be awesome.

Note, that when I take the MakeFrameMovable call, and move it to GUF.Core, the frame becomes movable, and everything's all peachy-keen. But, I'd like to keep this function call in my Utils file... first world problems.

Thanks for your help dudes, and lady dudes.

Kind Regards,
Mster50

Last edited by mster50 : 11-02-15 at 12:20 AM.
  Reply With Quote
11-02-15, 12:05 AM   #2
semlar
A Pyroguard Emberseer
 
semlar's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2007
Posts: 1,060
Have you tried GUF.Utils.MakeFrameMovable(self.playerFrame) instead of GUF.Utils:MakeFrameMovable(self.playerFrame)?

The colon is passing "GUF.Utils" as the first argument to the MakeFrameMovable function.

Last edited by semlar : 11-02-15 at 12:14 AM.
  Reply With Quote
11-02-15, 12:18 AM   #3
mster50
A Murloc Raider
Join Date: Nov 2015
Posts: 5
Well, that was it! Heh. Thank you very much. Guess I'm a little LUA rusty.

I just re-read LUA's self-referencing jargon. Makes total sense. Thanks again.

Last edited by mster50 : 11-02-15 at 12:24 AM.
  Reply With Quote
11-02-15, 10:09 PM   #4
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Also, I'd recommend not naming something "self" at the file level, or you're going to have this problem throughout your addon every time you use : notation, and you're going to have to use a non-standard name for the first argument in functions you pass to :SetScript on frames, etc. (traditionally it's named "self" but you won't be able to use that name there).
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
11-03-15, 03:25 PM   #5
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,323
One thing I'd suggest is if you don't actually need a global for other addons to interact with your code, you should put everything into your addon's private table.



In this example placed at the top of your Lua files:
Code:
local name,addon=...;
In the main chunk of every Lua file in your addon, WoW passes a string containing the name of your addon's folder and a private table shared among the rest of your Lua files.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote
11-16-15, 01:04 PM   #6
mster50
A Murloc Raider
Join Date: Nov 2015
Posts: 5
Great suggestions, thank you!
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Weird variable scoping... need a second pair of eyes


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