Thread Tools Display Modes
01-25-16, 02:09 PM   #1
NeXxUSA
A Murloc Raider
Join Date: Jan 2016
Posts: 8
SayHealth Addon -- Code Help

Hello, I need some help. This code doesn't work. As far as what it's supposed to do that is documented function by function below, as well as a general overview in the final comment. Please help me fix this. This is my first addon I'm going to see through to the end, as I scrapped my last one due to it being too advanced.
Code:
Lua Code:
  1. --Check if right alt key is down. If true returns 1.
  2. local function altDown()
  3.     local isDown = IsRightAltKeyDown()
  4.     return isDown
  5. end
  6. --Get current health
  7. local function getHealth()
  8.     local cHealth = UnitHealth("player")
  9.     return cHealth
  10. end
  11. --Get max health
  12. local function getMHealth()
  13.     local mHealth = UnitMaxHealth("player")
  14.     return mHealth
  15. end
  16. --All functions have bee declared
  17.  
  18. --If the function altDown returns 1, say current health over max health. Else, end.
  19. if altDown() == 1
  20.             then SendChatMessage(getHealth() .. "/" .. getMHealth())
  21.             else
  22. end
  23. --Purpose of the addon is to say "health/max" in /say chat when you press right alt.
Thanks!

Last edited by NeXxUSA : 01-25-16 at 02:18 PM. Reason: Name Change
  Reply With Quote
01-25-16, 02:20 PM   #2
Yukyuk
A Chromatic Dragonspawn
 
Yukyuk's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2015
Posts: 179
Not tested but I think IsRightAltKeyDown() returns a true/false?
__________________
Better to fail then never have tried at all.
  Reply With Quote
01-25-16, 02:24 PM   #3
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,877
Why are you creating a bunch of functions to wrap single functions in?

Code:
    if IsRightAltKeyDown() then
    	SendChatMessage(UnitHealth("player") .. "/" .. UnitMaxHealth("player"))
    end
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.
  Reply With Quote
01-25-16, 02:31 PM   #4
NeXxUSA
A Murloc Raider
Join Date: Jan 2016
Posts: 8
Originally Posted by Yukyuk View Post
Not tested but I think IsRightAltKeyDown() returns a true/false?
http://wowprogramming.com/docs/api/IsRightAltKeyDown says it returns 1 if true. Does the if statement need to be attached to anything? I think it may be that it is only check on initialization. If that's the case how do i fix it? Thanks!
  Reply With Quote
01-25-16, 02:36 PM   #5
NeXxUSA
A Murloc Raider
Join Date: Jan 2016
Posts: 8
Originally Posted by Fizzlemizz View Post
Why are you creating a bunch of functions to wrap single functions in?

Code:
    if IsRightAltKeyDown() then
    	SendChatMessage(UnitHealth("player") .. "/" .. UnitMaxHealth("player"))
    end
Like an exact copy and paste of my original cod . Unfortunately it doesn't work.
  Reply With Quote
01-25-16, 02:38 PM   #6
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,877
Originally Posted by NeXxUSA View Post
http://wowprogramming.com/docs/api/IsRightAltKeyDown says it returns 1 if true. Does the if statement need to be attached to anything? I think it may be that it is only check on initialization. If that's the case how do i fix it? Thanks!
All the documentation is user created so what might have been true at one point might not be today but noone has gone back to correct it.

test with:
Code:
/run print(IsRightAltKeyDown())
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 01-25-16 at 02:47 PM.
  Reply With Quote
01-25-16, 02:41 PM   #7
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,877
UnitMaxHealth should be UnitHealthMax

Code:
 
if IsRightAltKeyDown() then
    	SendChatMessage(UnitHealth("player") .. "/" .. UnitHealthMax("player"))
end
Edit, I'm guessing you don't lua errors turned on. If you are creating addons I would suggest gettting BugSack or Bugger to make you life ever so much easier.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 01-25-16 at 02:48 PM.
  Reply With Quote
01-25-16, 02:45 PM   #8
NeXxUSA
A Murloc Raider
Join Date: Jan 2016
Posts: 8
Originally Posted by Fizzlemizz View Post
All the doumentation is user created so what might have been true at one point might not be today but noone has gone back to cotrrect it.

test with:
Code:
/run print(IsRightAltKeyDown())
Returns

stdin:1: attempt to call global 'IsRightAltKeyDown' (a nil value)
stack traceback
stdin:1: in main chunk
[C]: ?
  Reply With Quote
01-25-16, 02:53 PM   #9
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,877
Originally Posted by NeXxUSA View Post
Returns

stdin:1: attempt to call global 'IsRightAltKeyDown' (a nil value)
stack traceback
stdin:1: in main chunk
[C]: ?
I meant for you to paste that into your chat edit box in-game for a quick test. Should return true or false depnding on wether the right alt key is down when you press enter.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.
  Reply With Quote
01-25-16, 05:06 PM   #10
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,326
One major problem is the main chunk of your code only runs once on load. To have functions run continuously or respond to an event, you need to create a frame to manage it.

Since we can respond to an event in order to accomplish our task, we'll do that.
Lua Code:
  1. local EventFrame=CreateFrame("Frame");--    Create our frame
  2. EventFrame:RegisterEvent("MODIFIER_STATE_CHANGED");--   Register event that fires when modifier key states change
  3. EventFrame:SetScript("OnEvent",function(self,event,key,down)--  Registers this function to run when the frame receives an event
  4. --  We only registered one event, so we don't need to check if it's MODIFIER_STATE_CHANGED
  5.     if key=="RALT" and down==1 then--   Only trigger if Right Alt was pressed
  6.         SendChatMessage(UnitHealth("player").."/"..UnitHealthMax("player"),"SAY");--    Send our message through the SAY channel
  7.     end
  8. end);
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote
01-25-16, 06:50 PM   #11
Tim
A Rage Talon Dragon Guard
 
Tim's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2008
Posts: 309
Another alternative is...

Code:
local RALT = CreateFrame("Frame")
RALT:SetScript("OnKeyDown", function(self)
   if IsRightAltKeyDown() then
      SendChatMessage(UnitHealth("player").."/"..UnitHealthMax("player"), "SAY")
   end
end)
__________________
AddOns: Tim @ WoWInterface
Battle Tag: Mysterio#11164
Current PC Setup: PCPartPicker List
  Reply With Quote
01-25-16, 07:58 PM   #12
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,326
In order for OnKeyDown to work, you need to set Frame:EnableKeyboard(). The problem with this is it blocks keybinds from working unless Frame:SetPropagateKeyboardInput() is set. This causes another problem, once you set the frame to propagate keyboard events, OnKeyDown stops responding to modifier keys. Also, if this were to work, it would respond every time you press any key plus right Alt.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)

Last edited by SDPhantom : 01-25-16 at 08:02 PM.
  Reply With Quote
01-25-16, 08:58 PM   #13
NeXxUSA
A Murloc Raider
Join Date: Jan 2016
Posts: 8
Originally Posted by Tim View Post
Another alternative is...

Code:
local RALT = CreateFrame("Frame")
RALT:SetScript("OnKeyDown", function(self)
   if IsRightAltKeyDown() then
      SendChatMessage(UnitHealth("player").."/"..UnitHealthMax("player"), "SAY")
   end
end)
Originally Posted by SDPhantom View Post
One major problem is the main chunk of your code only runs once on load. To have functions run continuously or respond to an event, you need to create a frame to manage it.

Since we can respond to an event in order to accomplish our task, we'll do that.
Lua Code:
  1. local EventFrame=CreateFrame("Frame");--    Create our frame
  2. EventFrame:RegisterEvent("MODIFIER_STATE_CHANGED");--   Register event that fires when modifier key states change
  3. EventFrame:SetScript("OnEvent",function(self,event,key,down)--  Registers this function to run when the frame receives an event
  4. --  We only registered one event, so we don't need to check if it's MODIFIER_STATE_CHANGED
  5.     if key=="RALT" and down==1 then--   Only trigger if Right Alt was pressed
  6.         SendChatMessage(UnitHealth("player").."/"..UnitHealthMax("player"),"SAY");--    Send our message through the SAY channel
  7.     end
  8. end);
Originally Posted by Fizzlemizz View Post
Why are you creating a bunch of functions to wrap single functions in?

Code:
    if IsRightAltKeyDown() then
    	SendChatMessage(UnitHealth("player") .. "/" .. UnitMaxHealth("player"))
    end
Originally Posted by Yukyuk View Post
Not tested but I think IsRightAltKeyDown() returns a true/false?
Thanks guys! I've decided to just stick to the languages I know cause I have no idea what I'm doing. Thanks for the help though! Maybe I'll change my mind or eventually find my way back. I leave you with a dancing banana to remember me by.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Say Health


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