Thread Tools Display Modes
05-26-19, 03:54 PM   #1
CrittenxD
A Deviate Faerie Dragon
Join Date: Apr 2018
Posts: 13
checking if CVar exists

Yo, I've been looking for a while and I haven't found something. I simply want to check if a cvar actually exists in case some get removed before Classic release. The following example produces an error if the cvar doesnt exist:


Code:
function setcvarifexists(cvar, value)
if GetCVarDefault(cvar) then SetCVar(cvar, value)
end
end
I'm looking for a replacement for GetCVarDefault(cvar) which just checks if the cvar exists.

EDIT: I'm also looking for a command to update all cvars similar to RestartGx(). For example the Timestamp setting will only load after reloading ui.

Last edited by CrittenxD : 05-26-19 at 06:56 PM.
  Reply With Quote
05-26-19, 07:57 PM   #2
Xrystal
nUI Maintainer
 
Xrystal's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 5,919
Have you tried just using SetCVar and use the return value and the event if necessary to continue the code process.

https://wow.gamepedia.com/API_C_CVar.SetCVar


The other option is to use the static variable NUM_LE_EXPANSION_LEVELS that is set to 1 for Classic and use that as a guide for Classic based functionality access and make any necessary changes using an if statement block, if you were hoping to have one source code base for both versions.
__________________


Characters:
Gwynedda - 70 - Demon Warlock
Galaviel - 65 - Resto Druid
Gamaliel - 61 - Disc Priest
Gwynytha - 60 - Survival Hunter
Lienae - 60 - Resto Shaman
Plus several others below level 60

Info Panel IDs : http://www.wowinterface.com/forums/s...818#post136818
  Reply With Quote
05-26-19, 08:05 PM   #3
CrittenxD
A Deviate Faerie Dragon
Join Date: Apr 2018
Posts: 13
I'm getting an error as soon as im calling SetCvar or GetCvarDefault with a nonexisting CVar. "Couldn't find a CVar named 'xdtest'".
  Reply With Quote
05-26-19, 08:56 PM   #4
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,322
You can use pcall() to capture the error and prevent it from being raised. If an error occurs, it instead returns false plus the error message as a string. Otherwise, if successful, it returns true with the returns from the called function appended.
__________________
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 : 05-26-19 at 09:02 PM.
  Reply With Quote
05-27-19, 07:43 AM   #5
CrittenxD
A Deviate Faerie Dragon
Join Date: Apr 2018
Posts: 13
Thank you very much for this great solution!

Lua Code:
  1. function setcvarifexists(cvar, value)
  2. if pcall(SetCVar, cvar, value) then
  3. else DEFAULT_CHAT_FRAME:AddMessage("|cffff0000Cvar not found: "..cvar)
  4. end
  5. end

Only one call, I'm super happy with that! Gonna look into the secure action button from my other thread now
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » checking if CVar exists

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