Thread Tools Display Modes
12-02-14, 12:33 PM   #1
Zireko
An Aku'mai Servant
Join Date: Nov 2014
Posts: 38
I Need Some Help Making My First WoW Addon

I'm trying to keep this very simple and starting with my addon. My addon is ZirBarInfo and it will eventually display money, bag space, and more. It will be a stand alone addon.

This is my first question. How do I save my frames position for when someone logs out and back in or reloads the ui. This is the entire code so far please keep it simple.

Lua Code:
  1. --ZirInfoBar will need to be a small bar that gives some information such as money, bag space, and more.
  2.  
  3. --Create the main Parent Frame that the info will be located in.
  4. ZirBarMainFrame = CreateFrame("Frame")
  5.     ZirBarMainFrame:SetPoint("CENTER", 0, 0)
  6.     ZirBarMainFrame:SetBackdrop(StaticPopup1:GetBackdrop())
  7.     ZirBarMainFrame:SetHight(300)
  8.     ZirBarMainFrame:SetWidth(300)
  9.     ZirBarMainFrame:SetMovable(true)
  10.     ZirBarMainFrame:EnableMouse(true)
  11.     ZirBarMainFrame:RegisterForDrag("LeftButton")
  12.     ZirBarMainFrame:SetScript("OnDragStart",ZirBarMainFrame.StartMoving)
  13.     ZirBarMainFrame:SetScript("OnDragStop",ZirBarMainFrame.StopMovingOrSizing)
  14.  
  15.  
  16. --Create the child frame that displays the money.
  17.  
  18.  
  19. --Create the child frame that displays the bagspace.
  20.  
  21.  
  22. --Create the extra child frames that hold more information.
  Reply With Quote
12-02-14, 12:45 PM   #2
Duugu
Premium Member
 
Duugu's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 851
Originally Posted by Zireko View Post
This is my first question. How do I save my frames position for when someone logs out and back in or reloads the ui. This is the entire code so far please keep it simple.
http://wowprogramming.com/docs/widge.../SetUserPlaced is what you're looking for.

[e]
Keep in mind that
If the frame does not have a name (set at creation time) specified, its position will not be saved.
So, add a name as a second parameter to CreateFrame:
Lua Code:
  1. ZirBarMainFrame = CreateFrame("Frame", "ZirBarMainFrameName")
(see http://wowprogramming.com/docs/api/CreateFrame)

Oh, and unless there is a valid reason you should define ZirBarMainFrame as local:
Lua Code:
  1. local ZirBarMainFrame = CreateFrame("Frame", "ZirBarMainFrameName")

Last edited by Duugu : 12-02-14 at 12:51 PM.
  Reply With Quote
12-02-14, 02:03 PM   #3
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,323
If the frame has a name, it should be enough to just have frame:SetMovable() set. frame:SetUserPlaced() is automatically flagged when frame:StartMoving() is called.
__________________
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
12-02-14, 03:09 PM   #4
Zireko
An Aku'mai Servant
Join Date: Nov 2014
Posts: 38
Ok, well got that in there so far and well now I gota wait for blizzard to get the servers up so I can finally test it. When you say as long as it has a name do you mean like this?

ZirBarMainFrame = CreateFrame("Frame", "ZirBarMainFrameName")
  Reply With Quote
12-02-14, 04:17 PM   #5
Zireko
An Aku'mai Servant
Join Date: Nov 2014
Posts: 38
This is my next question. I pulled up the api info and found the api to get money. So I tried to see if I could plug it into my code. Since the game is down currently I wanted to ask if this code would work and if not could you tell me why and how can I get it to work properly?

Lua Code:
  1. --Create the child frame that displays the money.
  2. local ZirBarMoneyFrame = CreateFrame("Frame", "ZirBarMoneyFrame", "ZirBarMainFrame")
  3.     ZirBarMoneyFrame:SetPoint("CENTER", 0, 0)
  4.     ZirBarMoneyFrame:SetBackdrop(StaticPopup1:GetBackdrop())
  5.     ZirBarMoneyFrame:SetHeight(30)
  6.     ZirBarMoneyFrame:SetWidth(60)
  7.    
  8. local copper = GetMoney()
  9.     ZirBarMoneyFrame:SetText(("%dg %ds %dc"):format(copper / 100 / 100, (copper / 100) % 100, copper % 100))
  Reply With Quote
12-02-14, 06:22 PM   #6
jeruku
A Cobalt Mageweaver
 
jeruku's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2010
Posts: 223
This time around you are not dealing with a frame, instead you want to create a FontString object to display text.

And if you are looking for a quick way to display gold/silver/copper then look into GetCoinTextureString and/or GetCoinText.

Last edited by jeruku : 12-02-14 at 06:25 PM. Reason: Post was too short making signature look odd... so does reason for editing.
  Reply With Quote
12-02-14, 07:21 PM   #7
Zireko
An Aku'mai Servant
Join Date: Nov 2014
Posts: 38
Originally Posted by jeruku View Post
This time around you are not dealing with a frame, instead you want to create a FontString object to display text.

And if you are looking for a quick way to display gold/silver/copper then look into GetCoinTextureString and/or GetCoinText.

First how do I even create a FontString object? I looked up info on http://wowprogramming.com/docs/widgets/FontString but I just don't seem to understand what I'm doing.

Second how do I plug the GetCoinTextureString in and GetCoinText? I also looked this up at http://wowpedia.org/API_GetCoinTextureString and didn't really understand how I would plug that in.

Please remember I'm new to coding and it more so helps when you explain things with code examples.
  Reply With Quote
12-02-14, 07:51 PM   #8
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,290
Originally Posted by Zireko View Post
First how do I even create a FontString object? I looked up info on http://wowprogramming.com/docs/widgets/FontString but I just don't seem to understand what I'm doing.

Second how do I plug the GetCoinTextureString in and GetCoinText? I also looked this up at http://wowpedia.org/API_GetCoinTextureString and didn't really understand how I would plug that in.

Please remember I'm new to coding and it more so helps when you explain things with code examples.
Lua Code:
  1. local FormattedMoney = ZirBarMoneyFrame:CreateFontString(nil, "ARTWORK")
  2. FormattedMoney:SetPoint("CENTER", ZirBarMoneyFrame)
  3. FormattedMoney:SetText(GetCoinTextureString(GetMoney()))
  Reply With Quote
12-02-14, 08:09 PM   #9
Zireko
An Aku'mai Servant
Join Date: Nov 2014
Posts: 38
Thank you Resike I'm understanding much better now how it's working.
  Reply With Quote
12-02-14, 08:19 PM   #10
Zireko
An Aku'mai Servant
Join Date: Nov 2014
Posts: 38
I'm getting an error saying <unnamed>SetText for this line.
FormattedMoney:SetText(GetCoinTextureString(GetMoney()))
  Reply With Quote
12-02-14, 08:25 PM   #11
Banknorris
A Chromatic Dragonspawn
 
Banknorris's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2014
Posts: 153
Originally Posted by Zireko View Post
I'm getting an error saying <unnamed>SetText for this line.
FormattedMoney:SetText(GetCoinTextureString(GetMoney()))
Dude you need to search about how to debug a program. If one line of code is not working test each component separately and use print to check how things works.

print(GetMoney()) --expected result?

print(GetCoinTextureString(GetMoney())) --expected result?

and so on
  Reply With Quote
12-02-14, 10:02 PM   #12
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by Zireko View Post
I'm getting an error saying <unnamed>SetText for this line.
FormattedMoney:SetText(GetCoinTextureString(GetMoney()))
1. What is the actual error message? The part of the error you posted only says where the problem is; the other part should say what the problem is.

2. GetMoney() probably does not return anything before you're fully logged in, and won't automatically update later, so you'll need to listen for an event to tell you when your money amount changes, and update the text each time:

Code:
YourFrame:RegisterEvent("PLAYER_MONEY")
YourFrame:SetScript("OnEvent", function(self, event)
     YourFontString:SetText(GetCoinTextureString(PlayerMoney()))
end)
Also:

Originally Posted by Zireko View Post
My addon is ZirBarInfo and it will eventually display money, bag space, and more. It will be a stand alone addon.
While it's awesome that you want to learn to write addons, why oh why does everyone pick this as their first project? There are already a million "standalone info bar" addons and they all suck; DataBroker displays + plugins are just so much more useful and easier to use. Surely there are a thousand addons you could write that haven't already been done to death.
__________________
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.

Last edited by Phanx : 12-02-14 at 10:05 PM.
  Reply With Quote
12-02-14, 10:17 PM   #13
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
Originally Posted by Phanx View Post
While it's awesome that you want to learn to write addons, why oh why does everyone pick this as their first project? There are already a million "standalone info bar" addons and they all suck; DataBroker displays + plugins are just so much more useful and easier to use. Surely there are a thousand addons you could write that haven't already been done to death.
Because they're a good place to start and are easy. Doesn't mean that they all need to be released to the public, but why not? As a teacher, I totally get why I see a million of these kinds of addons.
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
12-02-14, 10:36 PM   #14
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by Seerah View Post
Because they're a good place to start and are easy. Doesn't mean that they all need to be released to the public, but why not?
I would not characterize them as easy. They're full of multiple frames and objects, need multiple event handlers or end up with a giant 1000-line if/else chain in the event handler, and require using many unrelated API functions from many categories. An easy "learning to write an addon" project would be auto-selling grays, or announcing your interrupts to party chat, or something that only requires one frame and mostly sticks to one category of API functions.
__________________
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
12-03-14, 12:31 AM   #15
Zireko
An Aku'mai Servant
Join Date: Nov 2014
Posts: 38
Originally Posted by Phanx View Post
I would not characterize them as easy. They're full of multiple frames and objects, need multiple event handlers or end up with a giant 1000-line if/else chain in the event handler, and require using many unrelated API functions from many categories. An easy "learning to write an addon" project would be auto-selling grays, or announcing your interrupts to party chat, or something that only requires one frame and mostly sticks to one category of API functions.

Reason I'm starting out with it is the simple fact it implements more than one frame. Because most addons are hardly every just one simple frame. Then not to mention it has a good base to it. Speaking as a new coder I would rather take on something with more frames in it than something I would never use. Also I haven't found a good stand alone addon that just shows money and bag slots. I want something small and not all across my screen because many of the addons I've seen that show info just show way to much info. This is just why I want to work with this if it sheds any light on why some may start with this kind of code.

Any way, ty phanx for the info. I'll look more on it tomorrow have to get to bed for now so I can get the little one to school in the morning. Ty again
  Reply With Quote
12-03-14, 12:55 AM   #16
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by Zireko View Post
Also I haven't found a good stand alone addon that just shows money and bag slots. I want something small and not all across my screen because many of the addons I've seen that show info just show way to much info.
DataBroker displays only show the info you tell them to show. If you only want money and bag slot info, only install money and bag slot plugins. The great thing about the DataBroker system is that the display addon is just that -- it just provides the frames, however you want them. If you want a bar across the whole screen, you can have that. If you want little blocks you can put anywhere, you can have that. If you want a smaller bar across part of the screen, you can have that too. Then, depending on what information you want to show, you can pick from hundreds of available plugins, ranging from very simple "just the text" plugins to (IMO) hugely bloated "track everything under the sun" plugins with interactive tooltips.

Personally, I use Bazooka, which lets you create as many bars as you want, put them wherever you want, make them as big or small as you want, and control which info is shown on which part of which bar.

However, if your only experience with DataBroker is what you see in other people's premade UIs, I can see where you'd get the impression that they show too much -- most screenshots I see do indeed have way too much useless nonsense displayed. Then again, I'd characterize money and bag slot info as useless nonsense too, since you can just open your bag to check on that stuff, and it's not important to be able to see it all the time.
__________________
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
12-03-14, 07:32 AM   #17
Zireko
An Aku'mai Servant
Join Date: Nov 2014
Posts: 38
Well thanx Phanx, lol I looked into titan panel and actually like that one it seems smaller than the others. Not as much customization as Bazooka but it's out of the way and I don't really notice it unless I look directly at it. You're right there are a lot of addons out there like this. Hummm I may move on to just make a simple addon that makes a background for the quest tracker, make it movable and make it where it can be hidden. Maybe this could give me a chance to learn a simple saved variable if I even have to use one for that code since I figured out how to save a frame position without a saved variable in this little play around addon. Thank you again for all the info Phanx
  Reply With Quote
12-03-14, 08:39 AM   #18
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by Zireko View Post
Well thanx Phanx, lol I looked into titan panel and actually like that one it seems smaller than the others.
Oh, god.... Titan Panel is not a real DataBroker display. It's a bloated monstrosity that's been cobbled together and haphazardly patched by a long series of random developers over the years, with its own horrible, horrible plugin API, and an extra compatibility layer running on top of all of that to support some of the DataBroker spec.

I'm not sure what you mean by "it's out of the way and I don't notice it unless I look directly at it", but you can set Bazooka bars to fade out or disappear completely until you mouse over them. You can also change the background texture if you like the rocky Blizzard-like texture.

Also:

Originally Posted by Zireko View Post
I figured out how to save a frame position without a saved variable in this little play around addon
I assume you're talking about SetUserPlaced... that's fine for testing stuff, but not really a good way to do things in an addon you plan to publish, since if you ever log in with the addon disabled (eg. debugging another addon, or right after a patch when all the game "helpfully" un-checks the "load out of date addons" box) that position will be lost, and the frame will be created in its default position the next time you enable it. The position is also saved per-character, which is not desirable for most UI elements. TomTom works this way, and it's extremely irritating.

Figuring out how to save a frame position the right way is actually a good first project. Create a basic frame, add a basic texture so it's visible, make it movable, and work on figuring out how to save its position in saved variables.
__________________
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
12-03-14, 03:26 PM   #19
Zireko
An Aku'mai Servant
Join Date: Nov 2014
Posts: 38
As for Bazooka do you know a currency plugin, bag slot plugin, and a recommended zone plugin for it. I do like the fade in and out Bazooka has but spent some time looking and really got confused on what would and wouldn't work with it. I downloaded Titan Panel, Titan Panel (Currency), and Titan Panel (Recommended Zone). The titan panel already had a bag slot plugin in it.

As for my frame that I want to create I wanted to ask how to set a custom texture like I did before but last time I did it in xml and this time I want to do it in lua? Now if I'm going to do the saved variable for the position and the hiding of the frame can you please show me how to do this on a very simple level with the table because I know I will need tables in any later addon that I do? Thank you in advanced Phanx.
  Reply With Quote
12-03-14, 06:55 PM   #20
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by Zireko View Post
As for Bazooka do you know a currency plugin, bag slot plugin, and a recommended zone plugin for it.
There's a dedicated category for Broker plugins here:
http://www.wowinterface.com/downloads/cat108.html

I don't personally use any plugins that do what you described, so I can't offer any recommendations there. I do use plugins to quickly swap equipment sets, replace the minimap LFG icon, replace the minimap clock, replace the minimap zone name box, show my XP or rep, and quickly drop low-value items when my bags are full.

Additionally, many addons will provide a "launcher" icon that you can click to toggle the addon's display, quickly access the addon's options, etc. I'd say I have 5-10 such icons displayed on my bar, though there are an equal number that I've turned off because I don't care about them.

Originally Posted by Zireko View Post
As for my frame that I want to create I wanted to ask how to set a custom texture like I did before but last time I did it in xml and this time I want to do it in lua? Now if I'm going to do the saved variable for the position and the hiding of the frame can you please show me how to do this on a very simple level with the table because I know I will need tables in any later addon that I do? Thank you in advanced Phanx.
I'd suggest doing these one step at a time, and make sure you understand each step before proceeding to the next.

First, make the frame:
Code:
-- Create a frame with a unique global name,
-- and assign it to a local variable for faster local access:
local frame = CreateFrame("Frame", "MyAddonFrame", UIParent)
-- Give it a default position; the middle of the screen is good:
frame:SetPoint("CENTER")
-- And finally, give it a size, since frames need
-- both a location and a size to be displayed:
frame:SetSize(200, 200)
Second, add the texture so there's something to see:
Code:
-- Textures and font strings should generally NOT have global names:
local texture = frame:CreateTexture(nil, "BACKGROUND")
-- Make it fill the frame:
texture:SetAllPoints(true)
-- Make it semi-transparent black:
texture:SetTexture(0, 0, 0, 0.5)
Later you can play around with using actual texture files, but for simple solid-colored areas you can just pass your desired color directly to SetTexture.

Now make it movable:
Code:
frame:SetMovable(true)
frame:RegisterForDrag("LeftButton")
frame:SetScript("OnDragStart", function(self, button)
    -- self = frame
    -- button = string describing which mouse button is being used, eg. "LeftButton" or "RightButton"
    -- Let's make it only drag with the right button:
    if button == "RightButton" then
        self:StartMoving()
    end
end)
frame:SetScript("OnDragStop", function(self, button)
    self:StopMovingOrSizing()
end)
Now you can drag it around. Let's add a saved variable to your TOC:
Code:
## SavedVariables: MyAddonDB
And at the top of your Lua file, before you create the frame, give it a default value:
Code:
MyAddonDB = {}
Now restart WoW (most TOC changes require a full restart). From now on, this will happen:

1. You log in.
2. WoW reads your TOC file. The "SavedVariables" line has no effect at this time.
3. WoW reads your Lua file. "MyAddonDB" is created as a global variable with its value an empty table.
4. WoW reads your addon's saved variables file.
4a. If "MyAddonDB" was previously saved, its saved value overwrites the empty table.
4b. If "MyAddonDB" was not previously saved, its value remains the empty table.
5. You log out.
6. WoW remembers that you told it to save the "MyAddonDB" variable, and writes its current value out to your addon's saved variables file.

Now you can put stuff in it:
Code:
frame:SetScript("OnDragStop", function(self, button)
    self:StopMovingOrSizing()
    -- Get the new position:
    local x, y = self:GetLeft(), self:GetBottom()
    -- Save it:
    MyAddonDB.x = x
    MyAddonDB.y = y
    -- And re-anchor it:
    self:ClearAllPoints()
    self:SetPoint("BOTTOMLEFT", x, y)
end)
The last part isn't strictly necessary, but if you're saving points relative to the bottom left, then it's just a good idea to make sure the frame is always anchored by its bottom left. That way if you change its size later, it'll still be placed where you expect.

Now log out and look at your saved variables file. You should see those x/y coordinates in your table.

Now let's load them:
Code:
frame:RegisterEvent("ADDON_LOADED")
frame:SetScript("OnEvent", function(self, event, arg)
    if arg == "MyAddon" then
        -- This "addon loaded" event is for your addon.
        -- Your saved variables, if they existed, have been loaded. Check for that:
        if MyAddonDB.x and MyAddonDB.y then
            -- We have saved coordinates. Apply them:
            self:ClearAllPoints()
            self:SetPoint("BOTTOMLEFT", MyAddonDB.x, MyAddonDB.y)
        end
    end
end)
Now let's look at handling multiple events. For example, let's make the frame turn red while we're in combat. I'll point you to the event documentation on Wowpedia and make you find the right events. Once you've found them, register for them:
Code:
frame:RegisterEvent("ADDON_LOADED") -- Keep this around
frame:RegisterEvent("XXXXXX") -- Event for entering combat
frame:RegisterEvent("YYYYYY") -- Event for leaving combat
And adjust your event handler so it doesn't run the "addon loaded" code for every event:
Code:
-- Since different events come with different arguments,
-- use a vararg (looks like a horizontal ellipsis, or three dots)
-- as a generic "catch all" in the function definition:
frame:SetScript("OnEvent", function(self, event, ...)
    -- Now check the event first:
    if event == "ADDON_LOADED" then
        -- Assign the arguments to actual named variables for this event:
        local addon = ...
        if addon == "MyAddon" then
            -- Put the same code you used before here
        end
    -- Check for the combat starting event:
    elseif event == "XXXXXX" then
        -- This event does not provide any arguments, so there's
        -- no need to do anything with the vararg, since it's empty.

        -- Make the texture red:
        texture:SetTexture(1, 0, 0, 0.5)
    -- Check for the combat ending event:
    elseif event == "YYYYYY" then
        -- No arguments for this event either.

        -- Make the texture black again:
        texture:SetTexture(0, 0, 0, 0.5)
    end
end)
__________________
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

WoWInterface » Developer Discussions » Lua/XML Help » I Need Some Help Making My First WoW Addon


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