Thread Tools Display Modes
01-13-13, 09:14 AM   #1
laukond
A Black Drake
Join Date: Dec 2011
Posts: 87
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 06:41 AM.
  Reply With Quote
01-13-13, 09:34 AM   #2
Dridzt
A Pyroguard Emberseer
 
Dridzt's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2005
Posts: 1,360
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
  Reply With Quote
01-13-13, 09:54 AM   #3
laukond
A Black Drake
Join Date: Dec 2011
Posts: 87
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 09:58 AM.
  Reply With Quote
01-13-13, 01:05 PM   #4
humfras
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Oct 2009
Posts: 131
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 VuhDo CursorCastBar OptiTaunt Poisoner RaidMobMarker
  Reply With Quote
01-13-13, 01:58 PM   #5
laukond
A Black Drake
Join Date: Dec 2011
Posts: 87
I feel so stupid right now
Thank you, both of you!
  Reply With Quote
01-13-13, 04:11 PM   #6
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
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

  Reply With Quote
01-13-13, 04:15 PM   #7
laukond
A Black Drake
Join Date: Dec 2011
Posts: 87
Edited #1 with the conclusion. Thank you all :-)
  Reply With Quote
01-13-13, 06:00 PM   #8
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
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)
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
01-14-13, 03:32 AM   #9
laukond
A Black Drake
Join Date: Dec 2011
Posts: 87
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?
  Reply With Quote
01-14-13, 04:58 AM   #10
Dridzt
A Pyroguard Emberseer
 
Dridzt's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2005
Posts: 1,360
_ 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
  Reply With Quote
01-14-13, 05:51 AM   #11
laukond
A Black Drake
Join Date: Dec 2011
Posts: 87
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
  Reply With Quote
01-14-13, 06:45 PM   #12
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
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
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Need help


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