Using several resources including lua code given to me, the wow api manual and perplexity I came up with the following program to get my coordinates.
At one point it gave me an error for one of the features I used. I believe it was the TargetUnit() function. It said it only works for the Blizzard UI. As far as I can tell I am using the Blizzard UI. It’s the one you get when you download the game and start it from Battlenet.
Anyway here is the lua program. When I push the button it tells me “Tiffany is not your current target” which comes from a print statement in my “else” clause.
When I put in the TargetUnit(“Tiffany”) I get the error message.
Here is what I’ve got:
local f = CreateFrame("Button", "TiffanyTestButton", UIParent, "UIPanelButtonTemplate")
f:SetSize(90, 40)
f:SetPoint("CENTER", UIParent, "CENTER", 100, 0)
f:SetText("Altitude")
local function GetCord()
TargetUnit("Tiffany")
if UnitName("target") == "Tiffany" then
local x, y, z = UnitPosition("target")
if x and y and z then
print("Your position is: " .. x .. y .. z)
else
print("Unable to get coordinates")
end
else
print("Tiffany is not your current target.")
end
end
f:SetScript("OnClick", GetCord)
If the TargetUnit is there I get the error message and no button.
If I take out the TargetUnit then I get the button but when I click the button it says Tiffany is not my target.
Targeting is a protected function and so can only be called from a secure execution state. If its just for yourself, why not just use UnitPosition("player")
UnitPosition is also restriced to not being used in instances but as per Battlecruisr, the code could be something like:
local function GetCord(self)
if UnitExists("target") then
if UnitName("target") == "Tiffany" then
local x, y, z = UnitPosition("target")
if x and y and z then
print("Your position is: " .. x .. y .. z)
else
print("Unable to get coordinates")
end
else
print("Tiffany is not your current target.")
end
else
print("No target selected!")
end
end
local f = CreateFrame("Button", "TiffanyPositionButton", UIParent, "UIPanelButtonTemplate")
f:SetSize(90, 40)
f:SetPoint("CENTER", UIParent, "CENTER", 100, 0)
f:SetText("Altitude")
f:SetScript("OnClick", GetCord)
The problem is primarily that, as mentioned in a previous post, Blizzard don’t want addons to do some things programatically so parts of the API a secured and, where even possible, require addons to go though hurdles that Blizz. code doesn’t require. Automatic/selective targeting/spell selection, programatic movement etc. etc. etc. are among those restricted things.
https://warcraft.wiki.gg/wiki/API_TargetUnit
The code I posted requires you to have selected a target (UnitExists(‘target’)) and if the target is Tiffany print the position.
In this case, self is a reference to the frame (button) performing the click but it’s not actually used in the GetCord function (the reference is the first argument passed to a script by a buttons OnClick handler).
if you added
self:SetText("Clicked!")
as the first line after local function GetCord(self)
The text on the button would change after the first click.
I know you’re trying things out so “as a note to consider”, if your primary objective in writting an addon turns out to be creating an “I Win” button or something similary advantageous, you are not likely to find an easy way (or a way that won’t result in a bannable action).
No I’m not trying to create an “I win” button. While regular flying is sort of like a helicopter, dragon flying is sort of like an airplane. What I want is to set up a “standard T instrument layout” with the the airspeed on the left, altimeter on the right and heading indicator below.
That being said, I’ve made progress. First, I got the other error messages to print out by selecting different things. I can’t seem to select myself. But I did select a flight master and print out his name with print("Your target is " … UnitName(“target”)).
Also the coordinates don’t work even if I do have something selected. I get the “Unable to get coordinates” message. Maybe it can’t get coordinates for NPCs.
Here’s the current code:
local function GetCord(self)
if UnitExists("target") then
local x, y, z = UnitPosition("target")
print("Your target is " .. UnitName("target"))
if x and y and z then
print("Your position is: " .. x .. y .. z)
else
print("Unable to get coordinates")
end
else
print("No target selected!")
end
end
local f = CreateFrame("Button", "TiffanyPositionButton", UIParent, "UIPanelButtonTemplate")
f:SetSize(90, 40)
f:SetPoint("CENTER", UIParent, "CENTER", 100, 0)
f:SetText("Altitude")
f:SetScript("OnClick", GetCord)
You can select yourself by clicking your unit frame (portrait).
UnitPosition has been restriced to stop addons doing things like triangluations to tell people where to stand in fights. It also doesn’t actually return a z position to addons.
Ok it’s working now. My coordinates seem to be larger than 100, 100 so it’s probably all of Revendreth. It’s goofy how it puts us in the 4th quadrant instead of the first.
But this has been fun. I’ve been hearing about addons for so long, it’s good to see how it all works and actually get one working. Thanks for all your help.