Thread Tools Display Modes
06-02-09, 03:46 PM   #1
Nirrudn
A Deviate Faerie Dragon
AddOn Author - Click to view addons
Join Date: Nov 2005
Posts: 17
GetCVar Returning nil When it Shouldn't?

Edit 2 - Nevermind, I'm stupid. I forgot to make the variable in question global before using 3rd party addons to check its value.

I have an addon that mutes the game sound at certain times by adjusting the CVar "Sound_EnableSFX". Trouble is right now the game sound stays off for good until manually re-enabled, and I want my addon to re-enable it when said time is over.

So what I'm trying to do is store the current value of Sound_EnableSFX in a variable at addon load time. This isn't working. In my ADDON_LOADED event I have the following:

gomlsfxdefault = GetCVar("Sound_EnableSFX");

Well gomlsfxdefault is still nil. Okay, so I figure maybe CVars are loaded after addons and I can't get it then.

My sound muting is triggered by a specific aura I'm watching for, so I figured I would just grab the CVar before registering for the UNIT_AURA event instead, which isn't registered at load time but well after the player has entered the world like so:
Code:
if not (GOMLCore:IsEventRegistered("UNIT_AURA")) then
		--See if we can capture the current sound setting
		gomlsfxdefault = GetCVar("Sound_EnableSFX");

		GOMLCore:RegisterEvent("UNIT_AURA");
end
Once again however, no dice. gomlsfxdefault is still nil when my addon has registered the UNIT_AURA event.

So does anyone know what's up? Am I just doing this wrong?

Edit - Upon further investigation, GetCvar("Sound_EnableSFX") does work at load time, which only serves to further baffle me. I even tried this:

Code:
if GetCVar("Sound_EnableSFX") then
	gomlsfxdefault = 1;
        DEFAULT_CHAT_FRAME:AddMessage("The variable should be set now")
end
And even though GetCVar("Sound_EnableSFX") returns 1 at load time, and I get the chat message, it STILL won't set the variable.

Last edited by Nirrudn : 06-02-09 at 04:20 PM.
  Reply With Quote
06-02-09, 04:52 PM   #2
Foxlit
A Warpwood Thunder Caller
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 91
My suggestion is that you use a /run print(GetCvar("Sound_EnableSFX")); macro to verify that GetCVar isn't an actual issue in this case.

"it STILL won't set the variable" suggests that your problem lies elsewhere, as there are very few reasons why a variable assignment might not actually assign to a variable; and none that you can stumble into unknowingly. Either your code sets the variable to nil at a later point, or your variable is simply out of scope where you're attempting to use it.
__________________
... and you do get used to it, after a while.
  Reply With Quote
06-02-09, 08:54 PM   #3
Nirrudn
A Deviate Faerie Dragon
AddOn Author - Click to view addons
Join Date: Nov 2005
Posts: 17
Originally Posted by Foxlit View Post
My suggestion is that you use a /run print(GetCvar("Sound_EnableSFX")); macro to verify that GetCVar isn't an actual issue in this case.

"it STILL won't set the variable" suggests that your problem lies elsewhere, as there are very few reasons why a variable assignment might not actually assign to a variable; and none that you can stumble into unknowingly. Either your code sets the variable to nil at a later point, or your variable is simply out of scope where you're attempting to use it.
The problem turned out to be neither, I just forgot to make the variable in question globally accessible so my development addons could properly read it. It seems to be working fine now.

One of many newbie mistakes I make way too often!
  Reply With Quote
06-02-09, 09:13 PM   #4
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
Originally Posted by Nirrudn View Post
The problem turned out to be neither,
Actually it was the second one. Your variable was out of scope when you tried to call it (was local only to that block).
__________________
"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
06-02-09, 11:41 PM   #5
Verissi
Premium Member
 
Verissi's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2006
Posts: 99
Originally Posted by Nirrudn View Post
One of many newbie mistakes I make way too often!
Variable scoping bites everyone in the rear when they're first learning. I still remember having to draw boxes around blocks of code just so I could visualise what was in vs. out of scope (more years ago than I'd like to remember) After a bit, though, you get used to it.

From the sound of it, you're learning pretty quickly. Keep up the good work
__________________
"I can calculate the motions of the heavenly bodies, but not the madness of people." - Sir Isaac Newton
"Half of twice as intimidating as Saurfang is still one whole Saurfang worth of intimidation." - Anticlaus, Gorefiend server
  Reply With Quote
06-03-09, 08:58 AM   #6
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
Just an fyi, if you wish your variable to be accessible in various parts of your code, but *not* across different files/addons, then you may declare it as a local at the top of your file.

lua Code:
  1. local myvar
  2.  
  3. function dostuff()
  4.      myvar = othervar + 3
  5. end
  6.  
  7. function morestuff()
  8.      print(myvar)
  9. end


edit: this prevents polluting the global namespace
__________________
"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

WoWInterface » Developer Discussions » Lua/XML Help » GetCVar Returning nil When it Shouldn't?


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