Page 1 of 2

Help with script

PostPosted: Fri Oct 23, 2015 11:25 am
by jakeadams
Hello guys!

I am having issues with hiding my unitframes when I am out of combat. I want it to hide when I am at full health AND mana. So i tried this:

Code: Select all
local djiFrame = CreateFrame("frame");
djiFrame:RegisterEvent("PLAYER_REGEN_ENABLED"); --leaving combat
djiFrame:RegisterEvent("PLAYER_REGEN_DISABLED"); --entering combat
djiFrame:SetScript("OnEvent", function()
   if (event == "PLAYER_REGEN_ENABLED") then
      djiFrame:RegisterEvent("UNIT_HEALTH");   
   elseif (event == "PLAYER_REGEN_DISABLED") then
      DUF_PlayerFrame:Show(); 
      DUF_TargetFrame:Show();
   elseif (event == "UNIT_HEALTH") and UnitHealth ("player") == UnitHealthMax ("player") and UnitMana ("player") == UnitManaMax ("player")then
      DUF_PlayerFrame:Hide();
      DUF_PetFrame:Hide();
      DUF_TargetFrame:Hide();
      DUF_TargetOfTargetFrame:Hide();
      djiFrame:UnregisterEvent("UNIT_HEALTH");   

   end
end)


It worked with just checking for max health, but now it doesn't really work. I just added the "and UnitMana..." but apparently you can't do that.
Any way to make it only go away when I have full health and mana?

Re: Help with script

PostPosted: Fri Oct 23, 2015 11:40 am
by modernist
try this, its a little less verbose
https://gist.github.com/obble/658576c69341f5c6abb9
i think unregistering UNIT_HEALTH will stop the function from updating past your first iteration (?) and you need to track UNIT_MANA

Re: Help with script

PostPosted: Fri Oct 23, 2015 11:43 am
by LYQ
describe "doesn't really work"

is the compiler sending an syntax error? or is it simply not working the way you want it to? I'll guess latter for now - first think about your addition: the if clause is asking about the state of your health and mana but the event triggering this if clause is only a change in health, which means you should also register the UNIT_MANA event.

to make it cleaner I'd try:

Code: Select all
local f = CreateFrame("frame");
f:RegisterEvent("PLAYER_REGEN_ENABLED"); --leaving combat
f:RegisterEvent("PLAYER_REGEN_DISABLED"); --entering combat
f:SetScript("OnEvent", function()
   if (event == "PLAYER_REGEN_ENABLED") then
      f:RegisterEvent("UNIT_HEALTH");   
      f:RegisterEvent("UNIT_MANA");   
   elseif (event == "PLAYER_REGEN_DISABLED") then
      DUF_PlayerFrame:Show(); 
      DUF_PetFrame:Show();
      DUF_TargetFrame:Show();
      DUF_TargetOfTargetFrame:Show();
      f:UnregisterEvent("UNIT_HEALTH");
      f:UnregisterEvent("UNIT_MANA");
   elseif (event == "UNIT_HEALTH") or (event == "UNIT_MANA") then
      if UnitHealth("player") == UnitHealthMax("player") and UnitMana("player") == UnitManaMax("player") then
         DUF_PlayerFrame:Hide();
         DUF_PetFrame:Hide();
         DUF_TargetFrame:Hide();
         DUF_TargetOfTargetFrame:Hide();
         f:UnregisterEvent("UNIT_HEALTH");
         f:UnregisterEvent("UNIT_MANA");
      end
   end
end)


you've also never made the pet and target of target frame be shown again, also you should unregister health and mana events in combat because it could also make your UF hidden while still in combat

Re: Help with script

PostPosted: Fri Oct 23, 2015 11:52 am
by modernist
^ i personally don't see much point in registering REGEN_ENABLED and then registering your status events after it passes, since that's just going to happen as soon as the game loads anyway — might as well cut out that extra step, call UnitAffectingCombat and lose the need for all the event logic in the handler.

e: actually i guess it cuts down on the handler running when the event health/mana events fire for other units. I really miss RegisterUnitEvent

Re: Help with script

PostPosted: Fri Oct 23, 2015 12:00 pm
by LYQ
I just edited OPs code while leaving the same structure, didn't see your post until I finished mine.

ofc only UNIT_HEALTH/MANA would make more sense but then you should also check for arg1 and I think making local variables in your example is also too much plus you forgot about OPs Pet and TargetOfTarget frame

Re: Help with script

PostPosted: Fri Oct 23, 2015 12:02 pm
by jakeadams
Thanks for the replies!

I tried your method Modernist, but there seems to an issue. I get an error ingame. Somewhere it says "for _, v" That looks wrong.

LYG: Yeah, I did mean that it doesn't work like i want. I don't get an actual error, but my unitframes didn't allways disapear after i added the mana part.

I removed the pet part, because if i use the show pet frame, then it would show an empty pet frame if I was on a character without a pet.

But all in all yours seem to work like I intended. I have no exp in that kind of stuff ;)
Thanks alot for the help.

EDIT:
I do want the best method, so if the first is better I'll use that.

Re: Help with script

PostPosted: Fri Oct 23, 2015 12:04 pm
by LYQ
so something like

Code: Select all
local f = CreateFrame("frame");
f:RegisterEvent("UNIT_HEALTH")
f:RegisterEvent("UNIT_MANA")
f:SetScript('OnEvent', function()
  if arg1 ~= "player" then return end
  if UnitAffectingCombat("player") and not DUF_PlayerFrame:IsVisible() then
    DUF_PlayerFrame:Show()
    DUF_PetFrame:Show()
    DUF_TargetFrame:Show()
    DUF_TargetOfTargetFrame:Show()
  elseif UnitHealth("player") == UnitHealthMax("player") and UnitMana("player") == UnitManaMax("player") and DUF_PlayerFrame:IsVisible() then
    DUF_PlayerFrame:Hide()
    DUF_PetFrame:Hide()
    DUF_TargetFrame:Hide()
    DUF_TargetOfTargetFrame:Hide()
  end
end)


edit: god damn it, I again took too much time to answer :D but that was my final suggestion

edit2: if you wish you could insert another if clause around the PetFrame Hide&Show using http://wowwiki.wikia.com/wiki/API_HasPetUI or similar functions to check for a pet

edit3: lel now I didn't use arg1 myself :D

Re: Help with script

PostPosted: Fri Oct 23, 2015 12:11 pm
by jakeadams
Awesome thanks alot!
I'll try to add the if to check for a pet, so I don't have to manually delete the line everytime I change class....

Re: Help with script

PostPosted: Fri Oct 23, 2015 12:19 pm
by LYQ
I've updated the code, added
if arg1 ~= "player" then return end
at the top of the event handler since I forgot to use arg1 what my initial main argument was :D

arg1 should for both events, UNIT_HEALTH and UNIT_MANA be the unitID affecting which in your wanted case must be "player".
UNIT_HEALTH / MANA can fire for all units around you so you should use such a check so that, if one of that event fires it will only read one line of code which is checking arg1, if it's not regarding you

Re: Help with script

PostPosted: Fri Oct 23, 2015 12:47 pm
by modernist
just for comparison, this version works on a quick test. had to add a player_entering_world check for first login.

https://gist.github.com/obble/c4d5509f869251acf9f7

i hadn't properly realised these were unit frames other than the default and ditched the tot + pethide call in my first version since they're parented to player/target & would also be hidden.

the for loop simply reiterates the hide() on each frame rather than manually writing it out for each separate frame, saves line space ¯\_(ツ)_/¯

the error in my original code was because i forgot the last parenthesis on the handler!

E: updated slightly to
- move frames we're showing/hiding into a table
- reflect whether there's actually a target so we're not forcing the frame to show even when you don't have one.

a little more testing and i have some questions about the tenability of this function in regards to the target frame — hiding it whenever the hp/pp is 100% seems to be slightly overzealous? i dunno