Go Back   WowAce Forums > General > Lua Code Discussion
Lua Code Discussion You scared? Terrified. Mortified. Petrified. Stupefied... by [coding].

Reply
 
Thread Tools
Old 05-01-2012   #11
Phanx
Seal Cub Clubbing Club
 
Phanx's Avatar
 
Join Date: May 2005
Location: United States
Posts: 8,153
Default Re: my first addon

Quote:
Originally Posted by dunkindonut_lol View Post
i only get "i name 0 0 0" . whats wrong ?
You are using the UnitPower and UnitPowerMax functions incorrectly. Both require one argument, and accept an optional second argument:

The first argument is the unit token, such as "raid5". You are passing them strings like "raid%s", which are not valid unit tokens unless you run them through string.format with appropriate substitution values, eg. string.format("raid%s", i). If this argument is not a valid unit token, then the function will return 0.

The second argument is optional, and specifies which type of power to query. This is useful for classes with multiple power types (eg. paladins have mana and holy power, druids have mana, rage, energy, and eclipse power, etc.). You are passing values like 26 (for the 26th raid member) which do not correspond to any power type. If this argument specifies a power type the unit does not have, then the function will return 0.

Code:
for i = 1, GetRealNumRaidMembers() do -- [1]
	local name, rank, subgroup, level, class, classFilename,
		zone, online, isDead, role, isML = GetRaidRosterInfo(i)
	
	local unit = ("raid%d"):format(i) -- [2]
	
	local powerTypeID, powerType = UnitPowerType(unit)
	print(("%s has %d/%d %s (power type %d)"):format(unit, UnitPower(unit), UnitPowerMax(unit), powerType, powerTypeID))
end
[1] You don't need to check that GetRealNumRaidMembers() > 0 first, since running a loop from 1 to 0 will simply result in 0 iterations of the loop (eg. it won't run at all).

[2] You should build the unit token once, and then reuse it, instead of doing a format or concatenation operation each time you want to use it.
__________________
Author/maintainer of Grid, PhanxChat, and many more. | PM me on CurseForge
Troubleshoot an addon | Turn any code into an addon | More addon resources
Need help with your code? Attach or paste your whole, real files.
Please don't PM me about addon bugs, requests, or code help. Post a comment, ticket, or thread instead!

Last edited by Phanx; 05-02-2012 at 05:59 AM.
Phanx is offline   Reply With Quote
Old 05-01-2012   #12
dunkindonut_lol
Member
 
Join Date: Jan 2012
Posts: 13
Default Re: my first addon

thanks a lot for the help and comments
lua is very new to me, you made it a lot clearer why the one thing works and the other didnt even tho they show the same on print.

i got it to kinda do what i want, but now its spamming me.
in a looking for raid test it exploded to 8MB usage while showing just one little frame.
question : can i restrict this event UNIT_POWER to myself ("player") ?

i am thinking something like

Code:
function MyMana_OnLoad(frame)
	frame:RegisterEvent("UNIT_POWER")
end

function MyMana_OnEvent(frame, event, ...)

	if ( ~~UNIT WAS PLAYER~~ ) then
	MyMana_UpdateText()
	end
end
or do i need to register a different event?
i read that this event has a signature, including the unitid (yes, i want!).

my tries to access that with event[1] , event.arg1 ect. failed hard .

Last edited by dunkindonut_lol; 05-01-2012 at 11:16 PM.
dunkindonut_lol is offline   Reply With Quote
Old 05-01-2012   #13
Lombra
Senior Member
 
Lombra's Avatar
 
Join Date: Jan 2006
Location: Sweden
Posts: 410
Default Re: my first addon

Event arguments are passed to the event handler, and starts from the third argument. E g:
Code:
function MyMana_OnEvent(frame, event, unit, ...)
	if unit == "player" then
		MyMana_UpdateText()
	end
end
__________________
Grab your sword and fight the Horde!

Shattered Hand-EU
Lombra is online now   Reply With Quote
Old 05-01-2012   #14
dunkindonut_lol
Member
 
Join Date: Jan 2012
Posts: 13
Default Re: my first addon

aha! so, just to make sure i understood the syntax:

i can passover the frame (and its first) because the event is registered to it, then the event itself, unit is first signature and i could also pass over the type with because its next?

Code:
function MyMana_OnEvent(frame, event, unit, powerType )
...
anyhow, you just made me (and memory usage) very happy. thanks.
dunkindonut_lol is offline   Reply With Quote
Old 05-03-2012   #15
Adirelle
Legendary Member
 
Adirelle's Avatar
 
Join Date: Dec 2006
Posts: 2,386
Default Re: my first addon

Quote:
Originally Posted by dunkindonut_lol View Post
Code:
function MyMana_OnLoad(frame)
	frame:RegisterEvent("UNIT_POWER")
end

function MyMana_OnEvent(frame, event, ...)
	print(status)
	MyMana_UpdateText()
end

function MyMana_UpdateText()
	status = string.format("%d of %d Mana", UnitPower("player"), UnitPowerMax("player") )
	MyManaFrameText:SetText(status)
end
getting rid of the locals worked!! thx for the help so far !!!
Getting rid of the locals is pretty much a bad idea because it means you are using some globals. There you are using a pretty common variable name ("status"). If the author of another addon does as you do, and use the global "status", both variables will collide and probably cause both addons to bug.

So 1) use locals as much as possible, 2) when using globals, choose names that have little chances to be used to other addons (you could prefix them with your addon name, e.g. "MyMana_Status" instead of "status").

(By the way, using and creating a lot of globals has minor perfomance implications.)
__________________
Author of AdiButtonAuras, AdiBags, Squire2 and several other addons.

Each time you hit your "copy" command with a block of code, think about a way to refactor it so it did what you want without using the "paste" command.
Adirelle is offline   Reply With Quote
Old 05-03-2012   #16
Phanx
Seal Cub Clubbing Club
 
Phanx's Avatar
 
Join Date: May 2005
Location: United States
Posts: 8,153
Default Re: my first addon

In this case, the "status" variable is never accessed from outside your Lua file, so it should be local to the file. However, you don't really need it. If you want to print the current text, you can just retrieve it from the frame. Finally, you can use the :SetFormattedText method on your font string, instead of calling string.format first and then calling the plain :SetText method.

Code:
function MyMana_OnLoad(frame)
	frame:RegisterEvent("UNIT_POWER")
end

function MyMana_OnEvent(frame, event, ...)
	local unit, powerType = ...
	if unit == "player" and powerType == "MANA" then
		MyMana_UpdateText()
		print(MyManaFrameText:GetText())
	end
end

function MyMana_UpdateText()
	MyManaFrameText:SetFormattedText("%d of %d Mana", UnitPower("player"), UnitPowerMax("player"))
end
__________________
Author/maintainer of Grid, PhanxChat, and many more. | PM me on CurseForge
Troubleshoot an addon | Turn any code into an addon | More addon resources
Need help with your code? Attach or paste your whole, real files.
Please don't PM me about addon bugs, requests, or code help. Post a comment, ticket, or thread instead!
Phanx is offline   Reply With Quote
Old 05-04-2012   #17
dunkindonut_lol
Member
 
Join Date: Jan 2012
Posts: 13
Default Re: my first addon

thx again all for your comments, i made everything a local variable now. i dont know whats using cpu power or memory; if you say locals dont, perfect. i put the first version of what i had in mind when i was starting out here now:

http://www.wowace.com/addons/hoh_targets/

i was a bit busy to set this all up, even with all the excellent tutorials available here it takes some time. the next thing i will get to is what phanx wrote earlier already about different fontstrings, so numbers names ect. are presented right below each other, this one string feels a bit like cheating i hope i can improve this so it meets the quality standard of other addons here on wowace.
dunkindonut_lol is offline   Reply With Quote
Old 05-05-2012   #18
Phanx
Seal Cub Clubbing Club
 
Phanx's Avatar
 
Join Date: May 2005
Location: United States
Posts: 8,153
Default Re: my first addon

You are currently including every previous version inside the ZIP file for the current version... and inside each previous version subfolder, additional copies of all the versions before that, so there are multiple copies of every old version.

You do not need to keep "hardcopies" of each old version. The act of tagging your repository (to make a release ZIP) creates a permanent bookmark to that revision in the repository, so if you want to go back and look at old released versions, you just look in the "tags" section of your repository (instead of "trunk" where your working code is stored).

Edit: I went ahead and fixed this in your repository. The next time you tag a release, it will not include multiple copies of every previous release. If you need to see previous releases for some reason, you can (a) download them from the Files tab on your project page or (b) find them in the "tags" section of your repository.
__________________
Author/maintainer of Grid, PhanxChat, and many more. | PM me on CurseForge
Troubleshoot an addon | Turn any code into an addon | More addon resources
Need help with your code? Attach or paste your whole, real files.
Please don't PM me about addon bugs, requests, or code help. Post a comment, ticket, or thread instead!

Last edited by Phanx; 05-07-2012 at 10:34 PM.
Phanx is offline   Reply With Quote
Old 06-27-2012   #19
dunkindonut_lol
Member
 
Join Date: Jan 2012
Posts: 13
Default Re: my first addon

ah its true, this repository was a mess
once i got it into a browseable repository and not only command lines i saw for myself. thx, i cleaned up the mess and commited a new version without old hardcopies in backpack!
dunkindonut_lol is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 02:20 PM.