View Single Post
07-09-14, 07:09 AM   #2
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
In your first snippet (the one giving you an error):

Code:
local _, ns = ...
local _, playerClass = UnitClass("player")
local colors = oUF.colors
local config
-- ^ Your code doesn't use any of these variables, so you can remove them,
-- though if you plan to extend your code across multiple files in the future
-- then you might want to keep the first one. In particular, the "config" variable
-- is specific to the inner workings of oUF_Phanx, so it's unlikely your layout
-- will use it.

ns.frames, ns.headers, ns.objects, ns.fontstrings, ns.statusbars = {}, {}, {}, {}, {}
-- ^ Your code doesn't use any of these tables, which are specific to the internal
-- workings of oUF_Phanx. You can just remove this whole line.

-- Frames

local function Spawn(frame, unit, isSingle)
    frame:SetPoint("CENTER", UIParent)
    frame:SetSize(100, 25)
 
    local bg = frame:CreateTexture(nil, "BACKGROUND")
    bg:SetAllPoints(true)
    bg:SetTexture(0, 0, 0, 0.5)
    frame.bg = bg
    
    local name = frame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    name:SetPoint("BOTTOM", frame, "TOP")
    self:Tag(name, "[name]")
    -- ^ This line triggers the error because you are using a variable "self"
    -- which you didn't define anywhere. You probably meant "frame" to
    -- refer to the unit frame you are currently working with.
    frame.Name = name
end
I don't see anything wrong in your second block, but there are a few "bad habits" you should try to avoid when writing your own code (and should also correct when copying from other people's sloppy code, for consistency):

Code:
    local Health = CreateFrame("FRAME", nil, self)
Frame types are not written in all-caps. The API will auto-correct the capitalization for values that you pass in, but you should use correct capitalization anyway, both because it's correct, and because Lua itself is case sensitive, so if you're ever comparing values to the values returned by API functions like frame:GetFrameType(), your "FRAME" will not match the returned "Frame".

Code:
    Health:SetPoint("TOP", 0, -8)
    Health:SetPoint("LEFT", 8, 0)
    Health:SetPoint('RIGHT', -90, 0)
Pick one style of string quoting and stick with it. Don't randomly use "double quotes" on one line, and 'single quotes' the next. I'd suggest using "double quotes" since it's more likely you'll want to use an apostrophe inside a string than a quotation mark, and that way you won't have to escape things as often.

----------

Overall I'd say you're on the right track with your first snippet -- start with the most basic thing (the base frame) and add one thing at a time, making sure you understand each piece rather than copying and pasting a bunch of stuff at once and trying to figure it out afterward. You'll be a lot less frustrated along the way, and end up knowing a lot more.
__________________
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