Reply
 
Thread Tools Display Modes
Old 01-13-13, 08:14 AM   #1
laukond
A Cliff Giant
Join Date: Dec 2011
Posts: 79
Need help

I cannot make this work

Lua Code:
  1. local f = CreateFrame("Frame")
  2.     f:RegisterEvent("ZONE_CHANGED")
  3.     f:RegisterEvent("ZONE_CHANGED_NEW_AREA")
  4.     f:RegisterEvent("PLAYER_UPDATE_RESTING")
  5.     f:SetScript("OnEvent", function()  
  6.    
  7.         if IsResting() == true then
  8.             local a, b, c, d = "UnitNameFriendlyPlayerName", "UnitNameFriendlyPetName", "UnitNameFriendlyGuardianName", "UnitNameFriendlyTotemName"
  9.             SetCVar(a,0)
  10.             SetCVar(b,0)
  11.             SetCVar(c,0)
  12.             SetCVar(d,0)
  13.             else
  14.             SetCVar(a,1)
  15.             SetCVar(b,1)
  16.             SetCVar(c,1)
  17.             SetCVar(d,1)           
  18.         end
  19.     end)



Conclusion:
Little addon to hide friendly player names (not NPCs) when resting.

Lua Code:
  1. local cvars = {
  2.     "UnitNameFriendlyPlayerName",
  3.     "UnitNameFriendlyPetName",
  4.     "UnitNameFriendlyGuardianName",
  5.     "UnitNameFriendlyTotemName",
  6. }
  7.  
  8. local f = CreateFrame("Frame")
  9. f:RegisterEvent("PLAYER_UPDATE_RESTING")
  10. f:SetScript("OnEvent", function()
  11.     local value = IsResting() and 0 or 1
  12.     for _, cvar in pairs(cvars) do
  13.         SetCVar(cvar, value)
  14.     end
  15. end)

Last edited by laukond : 01-14-13 at 05:41 AM.
laukond is online now   Reply With Quote
Old 01-13-13, 08:34 AM   #2
Dridzt
A Pyroguard Emberseer
 
Dridzt's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2005
Posts: 1,190
I'm saying this from memory so take it with a grain of salt but I believe the cvars are string type not number.

So try setting them to "1" and "0" respectively.
You also do not need to test explicitly for true.
Code:
If IsResting() then
should be sufficient
Dridzt is offline   Reply With Quote
Old 01-13-13, 08:54 AM   #3
laukond
A Cliff Giant
Join Date: Dec 2011
Posts: 79
Originally Posted by Dridzt View Post
I'm saying this from memory so take it with a grain of salt but I believe the cvars are string type not number.

So try setting them to "1" and "0" respectively.
You also do not need to test explicitly for true.
Code:
If IsResting() then
should be sufficient
Didn't fix it.

Getting this error:

Usage: SetCVar("cvar", value [, "scriptCvar")
<in C code>
Edit: I believe it's something with the "else" maybe it should be another word?

Last edited by laukond : 01-13-13 at 08:58 AM.
laukond is online now   Reply With Quote
Old 01-13-13, 12:05 PM   #4
humfras
A Warpwood Thunder Caller
AddOn Author - Click to view addons
Join Date: Oct 2009
Posts: 92
Originally Posted by laukond View Post
Didn't fix it.

Getting this error:


Edit: I believe it's something with the "else" maybe it should be another word?
Your local variables are only set/true in the if clause, in the else clause they are all global and nil.
You can remove the '== true' to save some space, too (as Dridzt suggested).


use:
Lua Code:
  1. local f = CreateFrame("Frame")
  2.  
  3.     f:RegisterEvent("ZONE_CHANGED")
  4.  
  5.     f:RegisterEvent("ZONE_CHANGED_NEW_AREA")
  6.  
  7.     f:RegisterEvent("PLAYER_UPDATE_RESTING")
  8.  
  9.     f:SetScript("OnEvent", function()  
  10.  
  11.    
  12.         local a, b, c, d = "UnitNameFriendlyPlayerName", "UnitNameFriendlyPetName", "UnitNameFriendlyGuardianName", "UnitNameFriendlyTotemName"
  13.         if IsResting() then
  14.  
  15.             SetCVar(a,0)
  16.             SetCVar(b,0)
  17.             SetCVar(c,0)
  18.             SetCVar(d,0)
  19.  
  20.         else
  21.  
  22.             SetCVar(a,1)
  23.             SetCVar(b,1)
  24.             SetCVar(c,1)
  25.             SetCVar(d,1)            
  26.  
  27.         end
  28.  
  29.     end)
__________________
Author of CursorCastBar OptiTaunt Poisoner RaidMobMarker
humfras is offline   Reply With Quote
Old 01-13-13, 12:58 PM   #5
laukond
A Cliff Giant
Join Date: Dec 2011
Posts: 79
I feel so stupid right now
Thank you, both of you!
laukond is online now   Reply With Quote
Old 01-13-13, 03:11 PM   #6
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 9,530
Actually, move the variables line out of the event script entirely. As it stands, you're setting those variables over and over, each time one of the events fire.
__________________
"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

Seerah is offline   Reply With Quote
Old 01-13-13, 03:15 PM   #7
laukond
A Cliff Giant
Join Date: Dec 2011
Posts: 79
Edited #1 with the conclusion. Thank you all :-)
laukond is online now   Reply With Quote
Old 01-13-13, 05:00 PM   #8
Phanx
A Pyroguard Emberseer
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 3,983
I'd suggest using a table instead. It'll be easier to read, and easier to extend in the future.

Also, there's no need to listen for zone change events if you only want to respond to changes in the rest state.

Code:
local cvars = {
	"UnitNameFriendlyPlayerName",
	"UnitNameFriendlyPetName",
	"UnitNameFriendlyGuardianName",
	"UnitNameFriendlyTotemName",
}

local f = CreateFrame("Frame")
f:RegisterEvent("PLAYER_UPDATE_RESTING")
f:SetScript("OnEvent", function()
	local value = IsResting() and 1 or 0
	for _, cvar in pairs(cvars) do
		SetCVar(cvar, value)
	end
end)
__________________
Author/maintainer of Grid, PhanxChat, ShieldsUp, and many more.
Troubleshoot an addonTurn any code into an addonMore addon resources
Need help with your code? Post all of your actual code! Attach or paste your files.
Please don’t PM me about addon bugs or code questions. Post a comment or forum thread instead!
Phanx is offline   Reply With Quote
Old 01-14-13, 02:32 AM   #9
laukond
A Cliff Giant
Join Date: Dec 2011
Posts: 79
I don't quite understand these two lines:

Lua Code:
  1. local value = IsResting() and 1 or 0
  2.     for _, cvar in pairs(cvars) do

Specifically I don't understand the IsResting() and 1 or 0
And the _ symbol.
Could you try and explain them for me?
laukond is online now   Reply With Quote
Old 01-14-13, 03:58 AM   #10
Dridzt
A Pyroguard Emberseer
 
Dridzt's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2005
Posts: 1,190
_ is a variable name used by programmer convention as a throwaway variable.
Variables holding values you don't care to reuse are named _ for convenience (other programmers can at a glance know that a value stored in a variable named _ will be discarded).

It could be any other valid variable name, there's nothing special about the name of the variable itself other than programmers know the value will not be used further on.

As to your first question value gets assigned the result of the logical operation on the right.
http://www.lua.org/pil/3.3.html
Dridzt is offline   Reply With Quote
Old 01-14-13, 04:51 AM   #11
laukond
A Cliff Giant
Join Date: Dec 2011
Posts: 79
Originally Posted by Dridzt View Post
_ is a variable name used by programmer convention as a throwaway variable.
Variables holding values you don't care to reuse are named _ for convenience (other programmers can at a glance know that a value stored in a variable named _ will be discarded).

It could be any other valid variable name, there's nothing special about the name of the variable itself other than programmers know the value will not be used further on.

As to your first question value gets assigned the result of the logical operation on the right.
http://www.lua.org/pil/3.3.html
Thanks
That linked helped a lot, still a bit confusing though
laukond is online now   Reply With Quote
Old 01-14-13, 05:45 PM   #12
Phanx
A Pyroguard Emberseer
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 3,983
These two lines are functionally identical:

Code:
local value = IsResting() and 1 or 0
Code:
local value
if IsResting() then
    value = 1
else
    value = 0
end
__________________
Author/maintainer of Grid, PhanxChat, ShieldsUp, and many more.
Troubleshoot an addonTurn any code into an addonMore addon resources
Need help with your code? Post all of your actual code! Attach or paste your files.
Please don’t PM me about addon bugs or code questions. Post a comment or forum thread instead!
Phanx is offline   Reply With Quote
Reply

Go BackWoWInterface » Developer Discussions » Lua/XML Help » Need help

Thread Tools
Display Modes

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