-
Posted by roadmaster on Tue, 17 May 2011 10:08:23
I have a button that when the cursor enters the button area it shows a Tooltip, this works good, the tooltip shows a message.
Now what i want to do is I want to add to that message the name of the targeted player or unit, this also works well. The problem i am having is if there is no targeted player then i want the message to say something completely different and this is where i am having problems.
this is what i have that works:
if(UnitIsUnit("target", "player")) then -- player has self targeted EmoteForTargetTooltip(owner, playerString); else -- someone else is targeted. EmoteForTargetTooltip(owner, targetString); end
this code works as intended, but what it doesnt do is send the tooltip to the player without a target.
I tried:
if UnitName("target") then -- Do something if the name exists print("target does exist"); else -- Do something if it doesn't exist print("target does not exist"); end
with the above code if I have any unit targeted the if statement does process its print statement but the else never processes even if i dont have a target.
ie.. I reload the UI, and i immediately move my mouse over the button while i have no target, nothing happens, so i click on my own profile and it then shows that i have myself targeted, so i move the mouse over the button again and this time the print statement does work it shows me the "target does exist" statement.
I also tried:
if(UnitExists("target")) then print("target does exist"); else print("target does not exist"); end
Here I have the exact same problem with the exact same results, as long as I have any unit targeted then the print statement print("target does exist"); processes fine, but without a target i get nothing.
The UnitIsUnit If statement does what i need it to do if something is targeted but I also need to check if the player does not have any unit targeted and if not then do something.
with UnitName("target") and UnitExists("target") the else statements never seem to process, can anyone suggest a course of action for me?
Is there another way to do what i want to do?
Thanks for any help you can give.
roadmaster
-
Posted by jnwhiteh on Tue, 17 May 2011 10:13:18
I have a button that when the cursor enters the button area it shows a Tooltip, this works good, the tooltip shows a message.
Now what i want to do is I want to add to that message the name of the targeted player or unit, this also works well. The problem i am having is if there is no targeted player then i want the message to say something completely different and this is where i am having problems.
Your statement is confusing me a bit, but here's what I'm reading. If you want to add a message if the player has someone other than themselves targeted, and a different message if the player has no one targeted, you can do this:
if UnitExists("target") then if UnitIsUnit("target", "player") then -- the target is the player, so do nothing else -- display the target information in the tooltip end else -- the player does not have anyone targeted end
-
Posted by roadmaster on Tue, 17 May 2011 10:42:19
Thank you for your quick reply.
I tried your code with no success, I still have the same problem.
if UnitExists("target") then if UnitIsUnit("target", "player") then EmoteForTargetTooltip(owner, playerString); -- EXAMPLE 1 else EmoteForTargetTooltip(owner, targetString); -- EXAMPLE 2 end else EmoteForTargetTooltip(owner, playerString); -- EXAMPLE 3 end
If i have myself targeted then Example 1 runs, if I have someone else targeted then Example 2 runs, but if i dont have any target nothing happens, Example 3 never runs.
something i dont understand though is I took the following code:
if(UnitName("target")) then print("1"); else print("2"); end
put it in the xml file Buttons OnEnter script and it ran fine both the if and the else worked fine but when i took the same code and put it in my lua file the else would not process, is there something im doing that could make this happen?
-
Posted by jnwhiteh on Tue, 17 May 2011 11:11:36
The issue is then somewhere else in your logic and that's very difficult for me to troubleshoot. The conditional I gave you works properly and you can test this with the following macro:
/run if UnitExists("target") then if UnitIsUnit("target", "player") then print("player targeted") else print(UnitName("target") .. " is targeted") end else print("no target") end
Just put that on your action bar, and you'll be able to see that it properly handles all three cases. I'd suggest ensuring that Lua Errors are enabled (Interface Options -> Help -> Display Lua Errors) to ensure you're not missing an error message somewhere. There is no need to use UnitName, the UnitExists and UnitIsUnit functions do exactly what you want them to do.
-
Posted by roadmaster on Tue, 17 May 2011 11:15:42
Ive done some more troubleshooting of sorts,
the button i have has OnClick and OnEnter defined in the Scripts Tag of the xml file.
I added the following code to both OnClick and OnEnter In the Xml file:
if(UnitName("target")) then print(GetUnitName("target")); else print("unit is nil or doesnt exist"); end
when i saved and reloaded the UI then moved my cursor over the button to invoke the OnEnter or when I clicked the button to invoke the OnClick, both times the If and Else worked fine, but when i put the code into thier respective functions in the lua file the OnClick still worked fine but the OnEnter would not process the else statement.
here is the function that my OnClick goes to in the lua file:
function Emote_OnClick(string) DoEmote(string); if(UnitName("target")) then print(GetUnitName("target")); else print("OnClick, unit is nil or doesnt exist"); end end
Both the If and the Else works above.
Here is the function that my OnEnter goes to in the lua file:
function Emote_OnEnter(owner) local green = "|cFF00FF00"; local playerString = green .. "You applaud. Bravo!"; local targetString = green .. "You applaud at < " .. UnitName("target") .." >. Bravo!"; if(UnitName("target")) then print(GetUnitName("target")); else print("OnEnter, unit is nil or doesnt exist"); end if UnitExists("target") then if UnitIsUnit("target", "player") then EmoteForTargetTooltip(owner, playerString); -- EXAMPLE 1 else EmoteForTargetTooltip(owner, targetString); -- EXAMPLE 2 end else EmoteForTargetTooltip(owner, playerString); -- EXAMPLE 3 end end
In the Above only the "if(UnitName("target")) then" part works, the Else wont process.
Why does it work for one method but not for another? have i done something wrong?
-
Posted by roadmaster on Tue, 17 May 2011 11:42:48
Thank you for your help, I didnt know how to enable viewing of lua errors, after i enabled it I got some errors right off the main problem led me right to what was giving me so much stress tonight.
the line:
local targetString = green .. "You applaud at < " .. UnitName("target") .." >. Bravo!";
was defined at a point where if there was no target it would try to concatenate a nil value in the middle of the string. ie.. UnitName("target"), btw i changed that to GetUnitName.after moving that line down inside the [ if UnitExists("target") then ] check it finally worked, i have sure found out that logic can be a killer with lua. Thanks for your help, Ive spent way too much time up tonight, time for bed. lol at 7:47 in the am. lol