Thread Tools Display Modes
01-07-12, 08:59 AM   #1
Angrysteel
A Murloc Raider
Join Date: Jan 2008
Posts: 6
Prospecting Lua Script Help

Trying to learn Lua, and starting really simple.

I have made a button that appears in the middle of my screen. I can make the button do many things, such as leave groups, dance, random rolls, summon creatures, etc etc.

When i try to cast a spell via this same button, i get the protected blah blah message. What i am trying to do is prospect some ore, when i push the button.

A simple macro of /cast Prospecting, /cast Obsidium Ore works just fine, but since i am trying to do this via an addon, i cant figure it out.

I know things like: CastSpellByName("Prospecting") no longer work (protected), but other addons are able to prospect ore, by clicking a button. Even reading through the code of those addons, i cant seem to piece it together

Beginner Code i am working with

local Button = CreateFrame("Button", "MyButton", UIParent, "UIPanelButtonTemplate")
Button:SetWidth(150)
Button:SetHeight(25)
Button:SetPoint("TOP")
Button:SetText("Prospect")
Button:RegisterForClicks("AnyUp")
Button:SetScript("OnClick", function()
CastSpellByName("Prospecting")
end )

Any simple solutions?
  Reply With Quote
01-07-12, 09:18 AM   #2
Rilgamon
Premium Member
 
Rilgamon's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 822
When it comes to buttons casting spells there is not exactly an easy solution
Your button should inherit a secure template. A good start to learn what you need is

http://www.wowpedia.org/SecureActionButtonTemplate
__________________
The cataclysm broke the world ... and the pandas could not fix it!
  Reply With Quote
01-07-12, 10:44 AM   #3
Angrysteel
A Murloc Raider
Join Date: Jan 2008
Posts: 6
local macroBtn = CreateFrame("Button", "Prospect", UIParent, "SecureActionButtonTemplate");
macroBtn:SetAttribute("type", "spell")
macroBtn:SetAttribute("spell", "Prospecting");
macroBtn:RegisterForClicks("AnyUp")
macroBtn:SetWidth(60); macroBtn:SetHeight(60)
macroBtn:SetPoint("Center")
macroBtn:Show()
macroBtn:EnableMouse("true")
macroBtn:EnableMouseWheel("false")

With the above code using the SecureActionButtonTemplate, i am able to cast Prospecting like i wanted. The problem now is, i can not get the button to show.

I can use /click Prospect and it fires just fine, but obviously im missing something to show the button, just not sure where i went wrong
  Reply With Quote
01-07-12, 12:40 PM   #4
Rilgamon
Premium Member
 
Rilgamon's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 822
The secure template only takes care of the protected stuff
For visuals you need 'ActionButtonTemplate'

You can inherit more than one template "ActionButtonTemplate SecureActionButtonTemplate"
__________________
The cataclysm broke the world ... and the pandas could not fix it!
  Reply With Quote
01-09-12, 05:31 PM   #5
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
In addition to inheriting from ActionButtonTemplate (which will give you the hover/pushed textures) you also need to set the icon texture for the button.
Code:
local macroBtn = CreateFrame("Button", "Prospect", UIParent, "SecureActionButtonTemplate,ActionButtonTemplate")
(On a side note, using a generic name like "Prospect" for an object in the global namespace is generally considered bad form. You should give it a name that makes it immediately obvious what addon owns the button, such as "AngrysteelProspect".)

Get the correct texture file using the GetSpellInfo API:
Code:
local name, _, icon = GetSpellInfo(31252)
Then assign the texture file to the icon texture object created from the ActionButtonTemplate:
Code:
macroBtn.icon:SetTexture(icon)
You should probably also use the name returned from GetSpellInfo for the spell attribute, so your addon will work in all locales:
Code:
macroBtn:SetAttribute("spell", name)
Finally, semicolons are not necessary or useful in Lua. Unless you are really stuck on them from experience in some other programming language, you should just get rid of them, as they are generally considered ugly clutter in Lua.
  Reply With Quote

WoWInterface » Developer Discussions » General Authoring Discussion » Prospecting Lua Script Help

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