Thread Tools Display Modes
05-25-11, 09:06 PM   #1
cokedrivers
A Rage Talon Dragon Guard
 
cokedrivers's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2009
Posts: 325
Professions

I'm trying to modify a Datatext what i have so far is...

The tooltip will show me all my professions Primary, Seconday and Tradeskills.

What I would like to be able to do is

Left Click = Open Profession 1
Right Click = Open profession 2
Mouse Wheel Click = Open Profession Frame.

Code:
Stat:SetScript("OnMouseDown", function(self, btn)
	if btn == "LeftButton" then
		(Code to Open First Profession)
	elseif btn == "MiddleButton" then
		(Code to Open Professions Window)
	elseif btn == "Rightbutton" then
		(Code to Open Second Profession)
	end
end)
Any Help on this would be great. I'm still new to .lua codeing so please if you try to help be very expansive on your explinations.

Thanks

Coke
  Reply With Quote
05-27-11, 10:23 PM   #2
cokedrivers
A Rage Talon Dragon Guard
 
cokedrivers's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2009
Posts: 325
Shameless Bump!

Maybe one of you coders can help.

I am able to find the profession im looking for but i can not get it to toggle it or cast the spell.

This is what I have so far.
Code:
Stat:SetScript('OnMouseDown', function(self, btn)

	prof1, prof2 = GetProfessions()
	skillName1 = GetProfessionInfo(prof1)
	skillName2 = GetProfessionInfo(prof2)

	if btn == 'LeftButton' then
		if(skillName1 == 'Mining')then
			CastSpellByName('Smelting')
		elseif(skillName1 == 'Inscription')then
			CastSpellByName('Inscription')
		elseif(skillName1 == 'Leatherworking')then
			CastSpellByName('Leatherworking')
		elseif(skillName1 == 'Jewelcrafting')then
			CastSpellByName('Jewelcrafting')
		elseif(skillName1 == 'Alchemy')then
			CastSpellByName('Alchemy')
		elseif(skillName1 == 'Blacksmithing')then
			CastSpellByName('Blacksmithing')
		elseif(skillName1 == 'Engineering')then
			CastSpellByName('Engineering')
		elseif(skillName1 == 'Enchanting') then
			CastSpellByName('Enchanting')
		elseif(skillName1 == 'Herbalism')then
			print('Herbalism has no options!')
		elseif(skillName1 == 'Tailoring')then
			CastSpellByName('Tailoring')
		elseif(skillName1 == 'Skinning')then
			print('Skinning has no options!')
		else
			print('No Profession Found!')
		end
	elseif btn == 'MiddleButton' then
		ToggleSpellBook("professions")
		
	elseif btn == 'RightButton' then
		if(skillName2 == 'Mining')then
			ToggleFrame(MRTSkillFrame)
		elseif(skillName2 == 'Inscription')then
			CastSpellByName('Inscription')
		elseif(skillName2 == 'Leatherworking')then
			CastSpellByName('Leatherworking')
		elseif(skillName2 == 'Jewelcrafting')then
			CastSpellByName('Jewelcrafting')
		elseif(skillName2 == 'Alchemy')then
			CastSpellByName('Alchemy')
		elseif(skillName2 == 'Blacksmithing')then
			CastSpellByName('Blacksmithing')
		elseif(skillName2 == 'Engineering')then
			CastSpellByName('Engineering')
		elseif(skillName2 == 'Enchanting')then
			CastSpellByName('Enchanting')
		elseif(skillName2 == 'Herbalism')then
			print('Herbalism has no options!')
		elseif(skillName2 == 'Tailoring')then
			CastSpellByname('Tailoring')
		elseif(skillName2 == 'Skinning')then
			print('Skinning has no options!')
		else
			print('No Profession Found!')
		end	
	end

end)
As you can see with the "LeftButton" i set the Enchanting to print and it does so i believe everything is ok up to the point of opening the profession itself.

Thanks in advance for any help.

Coke

Last edited by cokedrivers : 05-27-11 at 11:01 PM.
  Reply With Quote
10-29-14, 12:59 AM   #3
gmarco
An Onyxian Warder
 
gmarco's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2009
Posts: 362
I was trying to achieve a similar result (a profession LDB), but was very confusing about a thing.

The code:

Lua Code:
  1. local prof1, prof2, arch, fish, cook, faid = GetProfessions()

will find some index for professions that the pg actually has and I can decode all them with something like this:

Lua Code:
  1. local function mkmyprof(x,y)
  2.     name, icon, rank, top, _, _, _, _, _, _ = GetProfessionInfo(y)    
  3.     myprof[x]={name, icon, rank, top}
  4. end
  5.  
  6. if prof1    then mkmyprof(1,prof1)  end
  7. if prof2    then mkmyprof(2,prof2)  end
  8. if arch     then mkmyprof(3,arch)   end
  9. if fish     then mkmyprof(4,fish)   end
  10. if cook     then mkmyprof(5,cook)   end
  11. if faid     then mkmyprof(6,faid)   end


What I really don't understand is that the indexes are not unique per profession of the pg.

So prof1 for a pg can have an index that for another pg could be of another prof while I thought it was an index unique for the profession and so prof1=herbalism should always be identified by the number 6 for example.

I try to explain better with an example:

local prof1, prof2, arch, fish, cook, faid = GetProfessions()

in a PG with prof1=herbalism and prof2=inscription I get:
6,9,10,8,7,5

while in a pg with prof1=tailoring and prof2=engineering I get:
7,8,10,9,6,5

As I wrote before I can decode this happily (name, rank, etc etc) but it is very difficult for me now try to do some check like:

if prof1 == herbalism don't let it be pressed because it is a protected action.

I can't use the name (I got Erbalismo italian client, an english friends wll get Herbalism and so on ...) nor I can't use the index.

Probably I can check the icon but I think this is not the right way to do this thing :-)


Btw the full addon skeleton is:


Lua Code:
  1. local ADDON = ...
  2.  
  3. local myprof = {}
  4. local name, icon, rank, top
  5. local x, y
  6. local playerName = UnitName("player");
  7.  
  8. local ldb = LibStub:GetLibrary("LibDataBroker-1.1")
  9. local prof1, prof2, arch, fish, cook, faid = GetProfessions()
  10.  
  11. local function mkmyprof(x,y)
  12.     name, icon, rank, top, _, _, _, _, _, _ = GetProfessionInfo(y)    
  13.     myprof[x]={name, icon, rank, top}
  14. end
  15.  
  16. if prof1    then mkmyprof(1,prof1)  end
  17. if prof2    then mkmyprof(2,prof2)  end
  18. if arch     then mkmyprof(3,arch)   end
  19. if fish     then mkmyprof(4,fish)   end
  20. if cook     then mkmyprof(5,cook)   end
  21. if faid     then mkmyprof(6,faid)   end
  22.  
  23. local dataobj = ldb:NewDataObject("MyProf", {type = "data source", text = "myprof"})
  24. dataobj.OnClick = function(self, button)  
  25.  
  26.     if (button == "LeftButton") then   
  27.         if prof1 then
  28.             dataobj.icon = myprof[1][2]
  29.             dataobj.text = myprof[1][1]
  30.             CastSpellByName(myprof[1][1])
  31.         end
  32.     elseif ( button == "RightButton" ) then            
  33.         if prof2 then
  34.             dataobj.icon = myprof[2][2]
  35.             dataobj.text = myprof[2][1]
  36.             CastSpellByName(myprof[2][1])
  37.         end
  38.     end
  39.  
  40. end
  41.  
  42. function dataobj:OnEnter()
  43.     GameTooltip:SetOwner(self, "ANCHOR_NONE")
  44.     GameTooltip:SetPoint("TOPLEFT", self, "BOTTOMLEFT")
  45.     GameTooltip:ClearLines()
  46.     dataobj.OnTooltipShow(GameTooltip)
  47.     GameTooltip:Show()
  48. end
  49.  
  50. function dataobj:OnTooltipShow()
  51.     GameTooltip:AddLine(playerName .. " skills", 1,1,1);
  52.     if prof1    then GameTooltip:AddDoubleLine(myprof[1][1], myprof[1][3])  end;
  53.     if prof2    then GameTooltip:AddDoubleLine(myprof[2][1], myprof[2][3])  end;
  54.     if arch     then GameTooltip:AddDoubleLine(myprof[3][1], myprof[3][3])  end;
  55.     if fish     then GameTooltip:AddDoubleLine(myprof[4][1], myprof[4][3])  end;
  56.     if cook     then GameTooltip:AddDoubleLine(myprof[5][1], myprof[5][3])  end;
  57.     if faid     then GameTooltip:AddDoubleLine(myprof[6][1], myprof[6][3])  end;
  58. end
  59.  
  60. function dataobj:OnLeave()
  61.     GameTooltip:Hide()
  62. end


Thanks.
__________________
This is Unix-Land. In quiet nights, you can hear the Windows machines reboot.
  Reply With Quote
10-29-14, 06:45 AM   #4
Torhal
A Pyroguard Emberseer
 
Torhal's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 1,196
Here's what I'm doing in AckisRecipeList_QuickScan to make this work no matter which locale is being played. Of course, this is just the relevant code, and is tailored for Ackis Recipe List (the mining and smelting stuff) so you'll need to modify to suit what you're doing. Look at the code for the actual AddOn for more details.

Code:
-------------------------------------------------------------------------------
-- Constants
-------------------------------------------------------------------------------
local MINING_NAME = GetSpellInfo(32606)
local SMELTING_NAME = GetSpellInfo(61422)

local VALID_PROFESSIONS = {
        [GetSpellInfo(2259)] = true, -- Alchemy
        [GetSpellInfo(2018)] = true, -- Blacksmithing
        [GetSpellInfo(2550)] = true, -- Cooking
        [GetSpellInfo(7411)] = true, -- Enchanting
        [GetSpellInfo(4036)] = true, -- Engineering
        [GetSpellInfo(746)] = true, -- First Aid
        [GetSpellInfo(2108)] = true, -- Leatherworking
        [SMELTING_NAME] = true, -- Smelting
        [GetSpellInfo(3908)] = true, -- Tailoring
        [GetSpellInfo(25229)] = true, -- Jewelcrafting
        [GetSpellInfo(45357)] = true, -- Inscription
        [GetSpellInfo(53428)] = true, -- Runeforging
}

-------------------------------------------------------------------------------
-- Variables.
-------------------------------------------------------------------------------
local known_professions = {
        ["prof1"] = false,
        ["prof2"] = false,
        ["archaeology"] = false,
        ["fishing"] = false,
        ["cooking"] = false,
        ["firstaid"] = false,
}

-- When you want to update information:
                        local known = known_professions

                        known.prof1, known.prof2, known.archaeology, known.fishing, known.cooking, known.firstaid = _G.GetProfessions()

-- The main part
                for profession, index in pairs(known_professions) do
                        if index then
                                local name, icon, rank, maxrank, numspells, spelloffset, skillline = _G.GetProfessionInfo(index)

                                if name == MINING_NAME then
                                        name = SMELTING_NAME
                                end

                                if VALID_PROFESSIONS[name] then
                                    -- Do your stuff here.
                                end
                        end
                end
__________________
Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Author of NPCScan and many other AddOns.
  Reply With Quote
10-29-14, 07:16 AM   #5
Tageshi
A Murloc Raider
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 7
There is no ID for profession names, but there is spellid for profession spell on your spellbook.

You can get profession spell names like below:
Code:
#Alchemy
/dump (GetSpellInfo(2259))
#Blacksmithing
/dump (GetSpellInfo(2018))
#Enchanting
/dump (GetSpellInfo(7411))
#Engineering
/dump (GetSpellInfo(4036))
#Herb Gathering
/dump (GetSpellInfo(2366))
#Inscription
/dump (GetSpellInfo(45357))
#Jewelcrafting
/dump (GetSpellInfo(25229))
#Leatherworking
/dump (GetSpellInfo(2108))
#Smelting
/dump (GetSpellInfo(2656))
#Skinning
/dump (GetSpellInfo(8613))
#Tailoring
/dump (GetSpellInfo(3908))
And then you can get profession spell names of your characters like below:
Code:
/dump GetSpellBookItemName(select(6,GetProfessionInfo((GetProfessions())))+1, BOOKTYPE_PROFESSION)
/dump GetSpellBookItemName(select(6,GetProfessionInfo(select(2, GetProfessions())))+1, BOOKTYPE_PROFESSION)
Compare these two names.
(Don't try to compare spellid directly. Spellid will change depending on profession ranks.)

Last edited by Tageshi : 10-29-14 at 07:42 AM.
  Reply With Quote
10-29-14, 03:20 PM   #6
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,323
This is what I can come up with for a somewhat simple solution. This follows the pattern of which all tradeskill windows are listed at index 1 of their associated offset. I included a lookup table that prevents the attempt to open a window for a profession that doesn't have one. Currently, this is Herbalism and Skinning.
Lua Code:
  1. --  To support other locales, restricted tradeskills are listed by their icon path, which never changes between locales
  2. local Restricted={
  3.     ["Interface\\Icons\\Trade_Herbalism"]=true;--   Herbalism
  4.     ["Interface\\Icons\\INV_Misc_Pelt_Wolf_01"]=true;-- Skinning
  5. };
  6.  
  7. Stat:SetScript("OnMouseDown", function(self, btn)
  8. --  Handle middle button and exit immediately
  9.     if btn == "MiddleButton" then
  10.         ToggleSpellBook(BOOKTYPE_PROFESSION);
  11.         return;
  12.     end
  13.  
  14. --  Get profession IDs
  15.     local prof1,prof2=GetProfessions();
  16.     local tex,offset;
  17.  
  18. --  Select based on button pressed
  19.     if btn=="LeftButton" and prof1 then
  20.         local _; _,tex,_,_,_,offset=GetProfessionInfo(prof1);
  21.     elseif btn=="RightButton" and prof2 then
  22.         local _; _,tex,_,_,_,offset=GetProfessionInfo(prof2);
  23.     else return; end
  24.  
  25. --  Open tradeskill
  26.     if not Restricted[tex] then--   Don't run if no tradeskill window
  27.         CastSpell(offset+1,BOOKTYPE_PROFESSION);
  28.     end
  29. end);
__________________
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
10-29-14, 07:35 PM   #7
cokedrivers
A Rage Talon Dragon Guard
 
cokedrivers's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2009
Posts: 325
Im not sure if this will work for a LDB and maybe you can add or adjust some of the code, But this is what I use on nData for professions:
Code:
local nData = LibStub("AceAddon-3.0"):GetAddon("nData")

------------------------------------------------------------------------
--	 Professions Plugin Functions
------------------------------------------------------------------------
nData.pluginConstructors["pro"] = function()

	db = nData.db.profile

	local plugin = CreateFrame('Button', nil, Datapanel)
	plugin:RegisterEvent('PLAYER_ENTERING_WORLD')
	plugin:SetFrameStrata('BACKGROUND')
	plugin:SetFrameLevel(3)
	plugin:EnableMouse(true)
	plugin.tooltip = false

	local Text = plugin:CreateFontString(nil, 'OVERLAY')
	Text:SetFont(db.font, db.fontSize,'THINOUTLINE')
	nData:PlacePlugin(db.pro, Text)

	local function Update(self)
		local prof1, prof2 = GetProfessions()
			if prof1 ~= nil then
				Text:SetFormattedText(hexa.."Professions"..hexb)
			else
				Text:SetFormattedText(hexa.."No Professions"..hexb)
			end
		self:SetAllPoints(Text)
	end

	plugin:SetScript('OnEnter', function()
		if InCombatLockdown() then return end
		local anchor, panel, xoff, yoff = nData:DataTextTooltipAnchor(Text)
		GameTooltip:SetOwner(panel, anchor, xoff, yoff)
		GameTooltip:ClearLines()
		GameTooltip:AddLine(hexa..PLAYER_NAME.."'s"..hexb.." Professions")
		GameTooltip:AddLine' '		
		for i = 1, select("#", GetProfessions()) do
			local v = select(i, GetProfessions());
			if v ~= nil then
				local name, texture, rank, maxRank = GetProfessionInfo(v)
				GameTooltip:AddDoubleLine(name, rank..' / '..maxRank,.75,.9,1,.3,1,.3)
			end
		end
		GameTooltip:AddLine' '
		GameTooltip:AddLine("|cffeda55fLeft Click|r to Open Profession #1")
		GameTooltip:AddLine("|cffeda55fMiddle Click|r to Open Spell Book")
		GameTooltip:AddLine("|cffeda55fRight Click|r to Open Profession #2")
		
		GameTooltip:Show()
	end)


	plugin:SetScript("OnClick",function(self,btn)
		local prof1, prof2 = GetProfessions()
		if btn == "LeftButton" then
			if prof1 then
				if (GetProfessionInfo(prof1) == ('Herbalism')) then
					print('|cff33ff99nData:|r |cffFF0000Herbalism has no options!|r')
				elseif(GetProfessionInfo(prof1) == ('Skinning')) then
					print('|cff33ff99nData:|r |cffFF0000Skinning has no options!|r')
				elseif(GetProfessionInfo(prof1) == ('Mining')) then
					CastSpellByName("Smelting")
				else	
					CastSpellByName((GetProfessionInfo(prof1)))
				end
			else
				print('|cff33ff99nData:|r |cffFF0000No Profession Found!|r')
			end
		elseif btn == 'MiddleButton' then
			ToggleSpellBook(BOOKTYPE_PROFESSION)	
		elseif btn == "RightButton" then
			if prof2 then
				if (GetProfessionInfo(prof2) == ('Herbalism')) then
					print('|cff33ff99nData:|r |cffFF0000Herbalism has no options!|r')
				elseif(GetProfessionInfo(prof2) == ('Skinning')) then
					print('|cff33ff99nData:|r |cffFF0000Skinning has no options!|r')
				elseif(GetProfessionInfo(prof2) == ('Mining')) then
					CastSpellByName("Smelting")
				else
					CastSpellByName((GetProfessionInfo(prof2)))
				end
			else
				print('|cff33ff99nData:|r |cffFF0000No Profession Found!|r')
			end
		end
	end)


	plugin:RegisterForClicks("AnyUp")
	plugin:SetScript('OnUpdate', Update)
	plugin:SetScript('OnLeave', function() GameTooltip:Hide() end)

	return plugin -- important!
end
Now this only opens the professions 1 and 2 not any secondary professions like First-Aid, Cooking or Archeology. But it is set that a left click will open your profession one (unless it is skinning or herbalism at this point it will print in general chat "Skinning has no options.") then middle click will open your spell book and right click opens your second profession.

Not sure if this is the direction your heading but this is what I have been using for a bout a year and seems to work for me.

Coke

Here is a screenshot of the tooltip for my professions:

Last edited by cokedrivers : 10-29-14 at 07:38 PM.
  Reply With Quote
10-29-14, 08:33 PM   #8
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Here is some code I wrote a while back for LibDataBroker that does what you describe, and may be helpful:

http://forums.wowace.com/attachment....9&d=1392003088
__________________
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
10-30-14, 12:53 AM   #9
gmarco
An Onyxian Warder
 
gmarco's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2009
Posts: 362
Thanks very much to all for the kind the replies.

Now I have better ideas on "how it works" this thing of the profession :-)
... and I have seen also how much elegant lua code could be ... :-)

I'll try to check all your suggestions and rewrite everything today or tomorrow.
I'll post here the feedback ...

Thanks again very much to all.
__________________
This is Unix-Land. In quiet nights, you can hear the Windows machines reboot.
  Reply With Quote
10-30-14, 02:12 AM   #10
gmarco
An Onyxian Warder
 
gmarco's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2009
Posts: 362
Here it is a first "stable" :-) release:

Lua Code:
  1. -- I want to say thanks to the people in wowinterface forum for the help, code and over all ... patience :-)
  2. -- Phanx, Torhal, Tageshi, SDPhantom, cokedrivers and all the others.
  3. -- [url]http://www.wowinterface.com/forums/showthread.php?p=299262#post299184[/url]
  4.  
  5. local ADDON = ...
  6. local prgname = "|cffffd200myprof|r"
  7. local playerName = UnitName("player");
  8.  
  9. local Restricted={
  10.     ["Interface\\Icons\\Trade_Herbalism"]=true;--   Herbalism
  11.     ["Interface\\Icons\\INV_Misc_Pelt_Wolf_01"]=true;-- Skinning
  12. };
  13.  
  14. local ldb = LibStub:GetLibrary("LibDataBroker-1.1")
  15. local dataobj = ldb:NewDataObject("MyProf", {
  16.     type = "data source",
  17.     icon = "Interface\\Minimap\\Tracking\\Repair",
  18.     text = playerName .. " skills",
  19. })
  20.  
  21. -- idea and code by SDPhantom ----------
  22. dataobj.OnClick = function(self, button)  
  23.    
  24.     if button == "MiddleButton" then
  25.         ToggleSpellBook(BOOKTYPE_PROFESSION);
  26.         return;
  27.     end
  28.  
  29. --  Get profession IDs
  30.     local prof1,prof2=GetProfessions();
  31.     local tex,offset;
  32.  
  33. --  Select based on button pressed
  34.     if button=="LeftButton" and prof1 then
  35.         local _; name,tex,_,_,_,offset=GetProfessionInfo(prof1);
  36.     elseif button=="RightButton" and prof2 then
  37.         local _; name,tex,_,_,_,offset=GetProfessionInfo(prof2);
  38.     else return; end
  39.  
  40. --  Open tradeskill
  41.     if not Restricted[tex] then--   Don't run if no tradeskill window
  42.         CastSpell(offset+1,BOOKTYPE_PROFESSION);
  43.     else
  44.         print(prgname .. " Error: " .. name .. " has no tradeskill window")
  45.     end
  46. end
  47.  
  48. function dataobj:OnEnter()
  49.     GameTooltip:SetOwner(self, "ANCHOR_NONE")
  50.     GameTooltip:SetPoint("TOPLEFT", self, "BOTTOMLEFT")
  51.     GameTooltip:ClearLines()
  52.     dataobj.OnTooltipShow(GameTooltip)
  53.     GameTooltip:Show()
  54. end
  55.  
  56. function dataobj:OnTooltipShow()
  57.     local i
  58.  
  59.     GameTooltip:AddLine(" ")
  60.  
  61.     -- thanks to: cokedrivers
  62.     -- [url]http://www.wowinterface.com/downloads/info19814-nData.html[/url]
  63.     for i = 1, select("#", GetProfessions()) do
  64.             local v = select(i, GetProfessions());
  65.             if v ~= nil then
  66.                 local name, _, rank, maxRank = GetProfessionInfo(v)
  67.                 GameTooltip:AddDoubleLine(name, rank..' / '..maxRank)
  68.             end
  69.         end
  70.         GameTooltip:AddLine(" ")
  71.         GameTooltip:AddDoubleLine("Left Click", "Open Profession #1")
  72.         GameTooltip:AddDoubleLine("Right Click", "Open Profession #2")
  73.         GameTooltip:AddDoubleLine("Middle Click", "Open Spellbook")
  74. end
  75.  
  76. function dataobj:OnLeave()
  77.     GameTooltip:Hide()
  78. end

When I succeded in finding some more time I'll try to cleanup a little bit :-)
Thanks again.
Attached Thumbnails
Click image for larger version

Name:	2014-10-30_09-11-47.png
Views:	192
Size:	77.4 KB
ID:	8277  
__________________
This is Unix-Land. In quiet nights, you can hear the Windows machines reboot.
  Reply With Quote
10-30-14, 02:59 AM   #11
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Code:
local _; name,tex,_,_,_,offset=GetProfessionInfo(prof1);
This is creating a local variable "_" and global variables "name", "tex", and "offset". They should all be local:

Code:
    --  Select based on button pressed
    local name, tex, offset, _
    if button == "LeftButton" and prof1 then
        name, tex, _, _, _, offset = GetProfessionInfo(prof1)
    elseif button == "RightButton" and prof2 then
        name, tex, _, _, _, offset = GetProfessionInfo(prof2)
    else
        return
    end
On a side note, addons are not macros. There is no character limit. You don't need to mash everything together. Add spaces between variables, around operators, etc. Put each statement on its own line. Your code will be much more readable, and thus easier to maintain, if you let it breathe.

Also, semicolons are useless ugly clutter in Lua. Get rid of them, unless you are already used to programming in some other language that requires them, in which case you should still get rid of them. Different programming languages have different syntaxes, just like different human languages -- you'll be better at any of them if you use them as they're meant to be used, rather than trying to shoehorn them into some other language's conventions.
__________________
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
10-30-14, 06:33 AM   #12
gmarco
An Onyxian Warder
 
gmarco's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2009
Posts: 362
Thanks very much Phanx.

After your input and corrections the code looks like this way:

Lua Code:
  1. -- I want to say thanks to the people in wowinterface forum for the help, code and over all ... patience :-)
  2. -- Phanx, Torhal, Tageshi, SDPhantom, cokedrivers and all the others.
  3. -- [url]http://www.wowinterface.com/forums/showthread.php?p=299262#post299184[/url]
  4.  
  5. local ADDON = ...
  6. local prgname = "|cffffd200myprof|r"
  7. local playerName = UnitName("player")
  8.  
  9. local Restricted={
  10.     ["Interface\\Icons\\Trade_Herbalism"]=true,         --  Herbalism
  11.     ["Interface\\Icons\\INV_Misc_Pelt_Wolf_01"]=true,   --  Skinning
  12. };
  13.  
  14. local ldb = LibStub:GetLibrary("LibDataBroker-1.1")
  15. local dataobj = ldb:NewDataObject("MyProf", {
  16.     type = "data source",
  17.     icon = "Interface\\Minimap\\Tracking\\Repair",
  18.     text = playerName .. " skills"
  19. })
  20.  
  21. -- idea and code by SDPhantom ----------
  22. dataobj.OnClick = function(self, button)  
  23.  
  24.     if button == "MiddleButton" then
  25.         ToggleSpellBook(BOOKTYPE_PROFESSION)
  26.         return
  27.     end
  28.  
  29. --  Get profession IDs
  30.     local prof1,prof2=GetProfessions()
  31.     local tex,offset
  32.  
  33. --  Select based on button pressed
  34.     local name, tex, offset, _
  35.     if button == "LeftButton" and prof1 then
  36.         name, tex, _, _, _, offset = GetProfessionInfo(prof1)
  37.     elseif button == "RightButton" and prof2 then
  38.         name, tex, _, _, _, offset = GetProfessionInfo(prof2)
  39.     else
  40.         return
  41.     end
  42.  
  43. --  Open tradeskill
  44.     if not Restricted[tex] then         --   Don't run if no tradeskill window
  45.         CastSpell(offset+1,BOOKTYPE_PROFESSION)
  46.     else
  47.         print(prgname .. " Error: " .. name .. " has no tradeskill window")
  48.     end
  49. end
  50.  
  51. function dataobj:OnEnter()
  52.     GameTooltip:SetOwner(self, "ANCHOR_NONE")
  53.     GameTooltip:SetPoint("TOPLEFT", self, "BOTTOMLEFT")
  54.     GameTooltip:ClearLines()
  55.     dataobj.OnTooltipShow(GameTooltip)
  56.     GameTooltip:Show()
  57. end
  58.  
  59. function dataobj:OnTooltipShow()
  60.     local i
  61.     GameTooltip:AddLine(" ")
  62.    
  63.     -- code by: cokedrivers
  64.     -- [url]http://www.wowinterface.com/downloads/info19814-nData.html[/url]
  65.     for i = 1, select("#", GetProfessions()) do
  66.         local v = select(i, GetProfessions())
  67.         if v ~= nil then
  68.             local name, texture, rank, maxRank = GetProfessionInfo(v)
  69.             GameTooltip:AddDoubleLine(name, rank..' / '..maxRank)
  70.         end
  71.     end
  72.     GameTooltip:AddLine(" ")
  73.     GameTooltip:AddDoubleLine("Left Click", "Open Profession #1")
  74.     GameTooltip:AddDoubleLine("Right Click", "Open Profession #2")
  75.     GameTooltip:AddDoubleLine("Middle Click", "Open Spellbook")
  76. end
  77.  
  78. function dataobj:OnLeave()
  79.     GameTooltip:Hide()
  80. end

Only a question:
Is it important to declare local also the "_" var ?

Thanks again.
Attached Thumbnails
Click image for larger version

Name:	2014-10-30_13-29-36.png
Views:	202
Size:	60.8 KB
ID:	8278  
__________________
This is Unix-Land. In quiet nights, you can hear the Windows machines reboot.

Last edited by gmarco : 10-30-14 at 06:37 AM.
  Reply With Quote
10-30-14, 07:41 AM   #13
Lombra
A Molten Giant
 
Lombra's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 554
Originally Posted by gmarco View Post
Only a question:
Is it important to declare local also the "_" var ?
Yes! The underscore is not in any way special and will leak like any other variable. It's arguably even more important to make sure that it's local, since it's so common that a conflict is at even greater risk.
__________________
Grab your sword and fight the Horde!
  Reply With Quote
10-30-14, 09:52 AM   #14
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
When Blizzard leaks a global underscore, and you leak a global underscore, you break the entire UI. It has happened before. Don't do it.
__________________
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
10-30-14, 12:39 PM   #15
gmarco
An Onyxian Warder
 
gmarco's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2009
Posts: 362
Uhm ... I begin to be scared :-)

I think I have to double check all my addons ...

Probably add to all of them :

local _

is the right thing to do ... (if not present :-)
__________________
This is Unix-Land. In quiet nights, you can hear the Windows machines reboot.

Last edited by gmarco : 10-30-14 at 01:04 PM.
  Reply With Quote
10-30-14, 01:19 PM   #16
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
That's the lazy solution, but really you should just make sure you're not leaking any globals. Anywhere you define a variable, stick a "local" in front of it. If you need it accessible in a higher scope, define it in that higher scope with no value (just "local myVariable") and then give it its value later, but as a general rule, keep variables in the narrowest scope necessary. If it only needs to exist inside a certain function, make sure it's defined as local inside that function. If it only needs to exist inside a certain for loop, keep it inside the loop, etc.

You can use WowGlobalFinder to help you find all the leaks in your code.
__________________
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
10-30-14, 04:31 PM   #17
gmarco
An Onyxian Warder
 
gmarco's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2009
Posts: 362
Thanks for your kind reply Phanx...

I have opened a new post about this here:
http://www.wowinterface.com/forums/s...ad.php?t=50333

Thanks again.
__________________
This is Unix-Land. In quiet nights, you can hear the Windows machines reboot.
  Reply With Quote
11-04-14, 12:27 PM   #18
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,323
Originally Posted by Phanx View Post
Code:
local _; name,tex,_,_,_,offset=GetProfessionInfo(prof1);
This is creating a local variable "_" and global variables "name", "tex", and "offset". They should all be local:
I had tex and offset defined as locals in the section before it, literally the previous running line. gmarco added name without adding it to the upvalue list making it leak into global namespace. Making "_" a local specifically in the if block was an attempt to isolate the value as it was not needed anywhere.





Originally Posted by Phanx View Post
On a side note, addons are not macros. There is no character limit. You don't need to mash everything together. Add spaces between variables, around operators, etc. Put each statement on its own line. Your code will be much more readable, and thus easier to maintain, if you let it breathe.

Also, semicolons are useless ugly clutter in Lua. Get rid of them, unless you are already used to programming in some other language that requires them, in which case you should still get rid of them. Different programming languages have different syntaxes, just like different human languages -- you'll be better at any of them if you use them as they're meant to be used, rather than trying to shoehorn them into some other language's conventions.
Lua was created to be flexible with its syntax. I agree semicolons are optional, as are most spaces. I'm not going to sit here and debate on which programming style is better, but yours is not the only way and you should stop pushing everyone into thinking that. Nobody except Blizzard is trying to "shoehorn" Lua into anything. It's created with options as intended to make people from all other languages comfortable using it. The only time "shoehorning" one language to act like another is bad is when you impede the standard performance of the language itself. Hint, Blizzard and their division by zero errors. -.-;
__________________
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-04-14, 09:29 PM   #19
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by SDPhantom View Post
Lua was created to be flexible with its syntax. ... yours is not the only way and you should stop pushing everyone into thinking that.
If you're the only person who's ever going to look at your code, sure, do whatever you want. But if you want other people to be able to read your code and help you with it, you should make some attempt to make it easy to read. Mashing everything together by omitting every possible space is the opposite of making your code easy to read, and I will continue to complain whenever people ask me to read code that looks like a copypasta from the Arenajunkies forum.
__________________
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-05-14, 02:18 AM   #20
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,323
Tbh, all this nitpicking is really irritating. You're complaining about semicolons at the end of each line and comas aren't enough separation between a list of names. Just stop and think about it. Is this really worth the fuss over?

It's a programming language, not grammar school.
__________________
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)

Last edited by SDPhantom : 11-05-14 at 02:21 AM.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Professions

Thread Tools
Display Modes

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