WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   checking if CVar exists (https://www.wowinterface.com/forums/showthread.php?t=57170)

CrittenxD 05-26-19 03:54 PM

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.

Xrystal 05-26-19 07:57 PM

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.

CrittenxD 05-26-19 08:05 PM

I'm getting an error as soon as im calling SetCvar or GetCvarDefault with a nonexisting CVar. "Couldn't find a CVar named 'xdtest'".

SDPhantom 05-26-19 08:56 PM

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.

CrittenxD 05-27-19 07:43 AM

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 :)


All times are GMT -6. The time now is 05:31 PM.

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