WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Need help (https://www.wowinterface.com/forums/showthread.php?t=45642)

laukond 01-13-13 09:14 AM

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)

Dridzt 01-13-13 09:34 AM

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

laukond 01-13-13 09:54 AM

Quote:

Originally Posted by Dridzt (Post 271778)
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:

Quote:

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

humfras 01-13-13 01:05 PM

Quote:

Originally Posted by laukond (Post 271781)
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)

laukond 01-13-13 01:58 PM

I feel so stupid right now :o
Thank you, both of you! :D

Seerah 01-13-13 04:11 PM

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.

laukond 01-13-13 04:15 PM

Edited #1 with the conclusion. Thank you all :-)

Phanx 01-13-13 06:00 PM

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)


laukond 01-14-13 03:32 AM

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?

Dridzt 01-14-13 04:58 AM

_ 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

laukond 01-14-13 05:51 AM

Quote:

Originally Posted by Dridzt (Post 271821)
_ 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 :rolleyes:

Phanx 01-14-13 06:45 PM

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



All times are GMT -6. The time now is 10:24 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI