Thread Tools Display Modes
07-07-13, 04:33 PM   #1
Spawnova
A Warpwood Thunder Caller
 
Spawnova's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2012
Posts: 96
efficiency help

I have an addon and one thing it does is check to see if my public note has changed and if so change it back, the only way I can think to do this is register guild roster update event and check every time it fires

I'm curious if there isn't a better way to do it that is neater here's what I have, which works fine

Lua Code:
  1. if event=="GUILD_ROSTER_UPDATE" then
  2. local pt = {"Spawnova", "Shin"}
  3.     for i=1 , GetNumGuildMembers() do --loop through all
  4.     local name,_,_,_,_,_,Note=GetGuildRosterInfo(i)  --get name at index
  5.         for _, value in pairs(pt) do --test name for any in list
  6.             if name==value and Note~=SGDB[name].note then --if contains name and note is not saved note
  7.                 GuildRosterSetPublicNote(i,SGDB[name].note) --change back to old note
  8.             end
  9.         end
  10.     end
  11. end

I'm always trying to learn new/better ways to do thing if anyone sees anything that could be more effecient let me know!
  Reply With Quote
07-07-13, 04:48 PM   #2
Dridzt
A Pyroguard Emberseer
 
Dridzt's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2005
Posts: 1,360
API-wise nothing comes to mind but you can get rid of a for ... end loop by changing your table from array to hash.
You should also probably avoid creating a new table with your names every time the event fires.

Code:
local pt = {Spawnova=true, Shin=true}
if event=="GUILD_ROSTER_UPDATE" then
  for i=1, GetNumGuildMembers() do -- loop through all
    local name,_,_,_,_,_,Note=GetGuildRosterInfo(i)  -- get name at index
    if pt[name] and Note~=SGDB[name].note then -- if contains name and note is not saved note
       GuildRosterSetPublicNote(i,SGDB[name].note) -- change back to old note
    end
  end
end

Last edited by Dridzt : 07-07-13 at 04:54 PM. Reason: fix+formatting
  Reply With Quote
07-07-13, 05:58 PM   #3
Spawnova
A Warpwood Thunder Caller
 
Spawnova's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2012
Posts: 96
Thanks I totally forgot about that, and setting the variable at the start of the addon rather than on the event is a good idea thank you
  Reply With Quote
07-08-13, 04:15 AM   #4
Vlad
A Molten Giant
 
Vlad's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2005
Posts: 793
Just some comments on the code above:

- The table can be like {["Spawnôvá"]=1,Spanky=1,["Shìn"]=1} for those special characters, otherwise you'll get an error about syntax error. [] simply lets you use anything as key.

- "Note" is should be "note" for consistency but it doesn't really matter.

- In the event that SGDB[name] is nil you will get an error for looking up "nil.note" so it would be wise to first check that SGDB[name] exists before doing the .note on it.

- In lua's boolean logic the "if ... then" the dots can be anything, true/false is the old school way, the new way is also using nil and anything else (usually 1). For example "if DB[name] then" if the DB doesn't contain the entry with the key name, it will be nil, in other words the block of code assigned after "then" will not be executed. In the case like above, if pt[name] returns nil it's basically same as the boolean "false" and if it returns anything at all then it is "true", it can be a table, a number, string, doesn't matter. As long it isn't specifically a boolean true/false, those always supersede.

That's all I had to say.
__________________
Profile: Curse | Wowhead
  Reply With Quote
07-08-13, 10:34 AM   #5
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,327
Originally Posted by Vlad View Post
- In lua's boolean logic the "if ... then" the dots can be anything, true/false is the old school way, the new way is also using nil and anything else (usually 1). For example "if DB[name] then" if the DB doesn't contain the entry with the key name, it will be nil, in other words the block of code assigned after "then" will not be executed. In the case like above, if pt[name] returns nil it's basically same as the boolean "false" and if it returns anything at all then it is "true", it can be a table, a number, string, doesn't matter. As long it isn't specifically a boolean true/false, those always supersede.
In short, nil and boolean false are the only values that will return a false condition. All other values return a true condition. This also means unlike some other languages, Lua considers a numerical zero and empty strings/tables as true values.
__________________
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

WoWInterface » Developer Discussions » Lua/XML Help » efficiency help


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