Autoloot Script

This is a QoL script I am working on. If auto loot is not enabled, then I want to enable it. If it is enabled, then do nothing. Here is what I have and it is not working. I’m running this as a Lua script.

local cvar = “autoLootDefault”

al = C_CVar.GetCVar(cvar)

if al == 0 then

C_CVar.SetCVar(cvar,”1”)

end

/run local cvar, toggle = "autoLootDefault" toggle = not C_CVar.GetCVarBool(cvar) C_CVar.SetCVar(cvar, toggle and "1" or "0") print("Auto Loot:", toggle and "\124cff00ff00ON!" or "\124cffff0000OFF")

Remove the /run if it’s for an addon.

Thanks !!!

Misread the “do nothing”. What you want would just be:

C_CVar.SetCVar("autoLootDefault", "1")

as turning the CVar on again if it’s already on won’t make a difference.

1 Like

I took your code and made this, but it does not change the autoloot value.

al = C_CVar.GetCVarBool(“autoLootDefault”)

if al == 0 then

C_CVar.SetCVar(“autoLootDefault”,”1”)

end

What I want it to do is set auto loot to 1 (on) if it is not set. If it is set to on, then do nothing.

I’m trying to do this in and addon.

You can just do what Fizzle said in their most recent post or do the following:

if not C_CVar.GetCVarBool("autoLootDefault") then
    C_CVar.SetCVar("autoLootDefault","1")
end
1 Like

If that’s all the code then it won’t work because CVars aren’t loaded when addon code is first run. You will need to wait until after the "VARIABLES_LOADED" event

local owner = {}
EventRegistry:RegisterFrameEvent("VARIABLES_LOADED")
EventRegistry:RegisterCallback("VARIABLES_LOADED", function() 
	C_CVar.SetCVar("autoLootDefault", "1")
	local on = C_CVar.GetCVarBool("autoLootDefault")
	print("Auto Loot:", on and "\124cff00ff00ON!" or "\124cffff0000OFF") 
end, owner)

You can edit it to get rid of the test and print once you know it’s working.

2 Likes

It works, not the way I thought it would, but I’m happy.

You can use the EventRegistry or you can create a frame, register the event and set an OnEvent script to do the same thing.

1 Like