Thread Tools Display Modes
12-02-12, 06:36 PM   #1
sodra
A Murloc Raider
Join Date: Nov 2012
Posts: 5
On unit click question

I'm trying to make it so when I target a unit.

Lua Code:
  1. --The main frame.
  2. local frame = CreateFrame("Frame", "FinalFrame", UIParent, "BasicFrameTemplate")
  3. frame:SetPoint("LEFT",0,0)
  4. frame:SetWidth(200)
  5. frame:SetHeight(200)
  6. frame:EnableMouse(true)
  7. frame:SetMovable(true)
  8. frame:RegisterForDrag("LeftButton")
  9. frame:SetScript("OnDragStart", frame.StartMoving)
  10. frame:SetScript("OnDragStop", frame.StopMovingOrSizing)
  11.  
  12. local frametext = frame:CreateFontString(nil, "OVERLAY", "GameTooltipText")
  13. frametext:SetPoint("CENTER",frame,"CENTER",0,60)
  14. frametext:SetText("Name")
  15. frametext:SetTextHeight(24)
  16.  
  17. local classtext = frame:CreateFontString(nil, "OVERLAY", "GameToolTipText")
  18. classtext:SetPoint("CENTER",frame,"CENTER",0,36)
  19. classtext:SetText("Class")
  20. classtext:SetTextHeight(18)
  21.  
  22. local healthtext = frame:CreateFontString(nil, "OVERLAY", "GameToolTipText")
  23. healthtext:SetPoint("CENTER",frame,"CENTER",0,18)
  24. healthtext:SetText("Max Health")
  25. healthtext:SetTextHeight(18)
  26.  
  27.  
  28. ----------------------------------------------------------------------
  29. --Functions
  30. function hidebuttons()
  31.     accbutton:Hide();
  32.     trdbutton:Hide();
  33.     drcbutton:Hide();
  34. end
  35.  
  36. function partybutton()
  37.     print("Would you like to add them to your party?");
  38.     accbutton = CreateFrame("Button","accbutton",frame,"UIPanelButtonTemplate")
  39.     accbutton:SetPoint("BOTTOMLEFT",0,2)
  40.     accbutton:SetWidth(80)
  41.     accbutton:SetHeight(22)
  42.     accbutton:SetText("Party")
  43.     accbutton:SetScript("OnClick", function(self, mouseButton)
  44.         InviteUnit(UnitName("target"));
  45.         hidebuttons();
  46.     end)
  47.    
  48.     trdbutton = CreateFrame("Button","trdbutton",frame,"UIPanelButtonTemplate")
  49.     trdbutton:SetPoint("BOTTOMLEFT",0,24)
  50.     trdbutton:SetWidth(80)
  51.     trdbutton:SetHeight(22)
  52.     trdbutton:SetText("Trade")
  53.     trdbutton:SetScript("OnClick", function(self, mouseButton)
  54.         InitiateTrade("target");
  55.         hidebuttons();
  56.     end)
  57.    
  58.     decbutton = CreateFrame("Button","decbutton",frame,"UIPanelButtonTemplate")
  59.     decbutton:SetPoint("BOTTOMLEFT",0,46)
  60.     decbutton:SetWidth(80)
  61.     decbutton:SetHeight(22)
  62.     decbutton:SetText("Close")
  63.     decbutton:SetScript("OnClick", function(self, mouseButton)
  64.         hidebuttons();
  65.     end)
  66. end
  67.  
  68. function dpshealth()
  69.     if UnitHealthMax("target") / UnitHealth("target") <.50 then
  70.         if(GetLFGRoles() == isDPS) then
  71.             print("DONT FORGET THE DPS!")
  72.         end
  73.     end
  74. end
  75.  
  76. function otherenddps()
  77.     print("Please target the Healer. If you do not target the healer, this will not work")
  78.     if UnitGroupRolesAssigned("target") == DAMAGER then
  79.         print("That is not the healer, please target the healer")
  80.     end
  81.    
  82.     if UnitGroupRolesAssigned("target") == TANK then
  83.         print("That is not the healer, please target the healer")
  84.     end
  85.    
  86.     if UnitGroupRolesAssigned("target") == HEALER then
  87.         messagedman = "target"
  88.         print("That is the healer. When you are at 50% or lower health, this addon will automatically send a message to the healer")
  89.     end
  90.    
  91.     if UnitHealth("player") / UnitHealthMax("player") <=.50 then
  92.             SendChatMessage("HEAL ME!","WHISPER","nil",messagedman);
  93.     end
  94. end
  95. ---------------------------------------------------------------------------------------------------------------------------------
  96. --Working
  97. print("Addon is currently working");
  98.  
  99. if UnitIsPlayer("target") then
  100.     frametext:SetText(UnitName("target"))
  101.     classtext:SetText(UnitClass("target") .. " " .. UnitRace("target"))
  102.     healthtext:SetText(UnitHealthMax("target") .. " Max HP")
  103.     partybutton();
  104. end
  105.    
  106. local mybutton = CreateFrame("Button","mybutton",frame,"UIPanelButtonTemplate")
  107.     mybutton:SetPoint("BOTTOMRIGHT",-2,2)
  108.     mybutton:SetWidth(80)
  109.     mybutton:SetHeight(22)
  110.     mybutton:SetText("Twelve")
  111.     mybutton:SetScript("OnClick", function(self, mouseButton)
  112.         print("AW DUDE THIS BUTTON ROCKS!");
  113.         if UnitIsPlayer("target") then
  114.             print("This players name is " .. UnitName("target"));
  115.             print("They are a " .. UnitClass("target") .. " " .. UnitRace("target"));
  116.             print("They currently have " .. UnitHealth("target") .. " health, out of " .. UnitHealthMax("target") .. " health.");
  117.             partybutton();
  118.             frametext:SetText(UnitName("target"))
  119.             classtext:SetText(UnitClass("target") .. " " .. UnitRace("target"))
  120.             healthtext:SetText(UnitHealthMax("target") .. " Max HP")
  121.         end
  122.     end)
  123.    
  124. local chanbutton = CreateFrame("Button","chanbutton",UIParent,"UIPanelButtonTemplate")
  125.     chanbutton:SetPoint("BOTTOMRIGHT",0,0)
  126.     chanbutton:SetWidth(160)
  127.     chanbutton:SetHeight(22)
  128.     chanbutton:SetText("Join Channel Twelve")
  129.     chanbutton:SetScript("OnClick", function(self, mouseButton)
  130.         ChatFrame_AddChannel(DEFAULT_CHAT_FRAME, "Twelve");
  131.     end)
  132.    
  133. ---------------------------------------------------------------------------'
  134. --Currently in testing
  135.  
  136. --[[function questions()
  137.     question = CreateFrame("Frame","question",UIParent,"UIFrameTemplate")
  138.     question:SetPoint("BOTTONLEFT",0,0)
  139.     question:SetWidth(800)
  140.     question:SetHeight(100)
  141.     question:SetScript("OnLoad", questionone)
  142. end--]]
  143.  
  144. SLASH_TWELVE1 = '/12'
  145. function SlashCmdList.TWELVE(msg, editbox)
  146.     print("TWELVE. That Unit is a " .. UnitIsPlayer("target"));
  147. end

The bit of code I need to work when a unit is targeted is this

Lua Code:
  1. if UnitIsPlayer("target") then
  2.     frametext:SetText(UnitName("target"))
  3.     classtext:SetText(UnitClass("target") .. " " .. UnitRace("target"))
  4.     healthtext:SetText(UnitHealthMax("target") .. " Max HP")
  5.     partybutton();
  6. end
  Reply With Quote
12-02-12, 07:15 PM   #2
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
Note 1: please don't nag the healers. This makes them ignore you and annoys the crap out of them.

Note 2: you're calling that bit of code ONLY when the addon is loaded. In order to have it called each time you target something, you need to register for the event PLAYER_TARGET_CHANGED and then have your check within the OnEvent script.
__________________
"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-12, 07:21 PM   #3
sodra
A Murloc Raider
Join Date: Nov 2012
Posts: 5
Originally Posted by Seerah View Post
Note 1: please don't nag the healers. This makes them ignore you and annoys the crap out of them.

Note 2: you're calling that bit of code ONLY when the addon is loaded. In order to have it called each time you target something, you need to register for the event PLAYER_TARGET_CHANGED and then have your check within the OnEvent script.
Thank you so much.
I'll take note of the healer thing, thanks.
  Reply With Quote
12-02-12, 07:21 PM   #4
semlar
A Pyroguard Emberseer
 
semlar's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2007
Posts: 1,060
This will call your function whenever the player target changes..
Lua Code:
  1. frame:SetScript('OnEvent', function()
  2.     if UnitIsPlayer("target") then
  3.         frametext:SetText(UnitName("target"))
  4.         classtext:SetText(UnitClass("target") .. " " .. UnitRace("target"))
  5.         healthtext:SetText(UnitHealthMax("target") .. " Max HP")
  6.         partybutton()
  7.     end
  8. end)
  9. frame:RegisterEvent('PLAYER_TARGET_CHANGED')
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » On unit click question


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