Feature Request: IF [script: ...]
eamonno opened this issue ยท 9 comments
Simple enough really, add an if option to run an arbitrary piece of lua script and use the truthiness of it's return value as a condition.
My own specific use case is I want to have LiteMount spawn a Traveller's Tundra Mammoth if my backpack is open on screen since if I am mounting with bags visible it's almost always because I need to vendor some stuff. It's easy to check in lua with IsBagOpen(0) but I can't find a way to tie it into a LiteMount option.
You could add a bagopen condition (IF [bagopen: 0] but the script itself feels like the more generic option.
I should add that obviously it's possible to taint the entire interface by doing things with pcall, and I provide no support at all for that mess.
As a programmer I absolutely want this and think it's cool. As someone who has been writing wow addons for 15 years I know if non-programmers can ever find it I'm going to be stuck in a support nightmare. :)
This is my dry-coded idea, please try it out (I'm not near WoW right now) by pasting it into Conditions.lua along with the other conditions.
CONDITIONS["pcall"] = {
handler =
function (cond, context, text)
if text then
local f, err = loadstring(text)
if f and not err then
local ok, rc = pcall(f)
return ok and rc
end
end
end
}
Usage would be something like
IF [pcall:IsBagOpen(0)]
Yeah, I managed to get it sort of working. There were a few issues. The working code I ended up with is shown below.
CONDITIONS["pcall"] = {
name = "pcall",
toDisplay =
function(v)
return v
end,
menu = {
{ val = "pcall:IsBagOpen(0)" },
{ val = "pcall:IsBagClosed(0)" }
},
handler =
function (cond, context, text)
if text then
local f, err = loadstring("return " .. text)
if f and err == nil then
local ok, rc = pcall(f)
return ok and rc
end
end
end
}
There were a few gotchas. First I had to prepend a return to the text passed to the loadstring
function or it was just returning nil all the time. Maybe there is a neater way to do it than just adding the text in the ugly manner here (though looking at the docs they do the exact same thing so maybe not). Next err returns as nil from loadstring if it succeeds, so the condition had to be if f and err == nil
as opposed to if f and err
.
The use of the menu was just a quick edit. I wasn't able to get it working editing the keybind script initially (because it didn't work presumably) so I just edited it in as a menu option. Obviously this kind of defeats the whole point. I intended to go back and edit it to work with the keybind script but I just haven't had a free minute since I initially got it working.
Also there is no IsBagClosed
function, I just added that to see what errors would pop up.
Thanks a million for the time you took on this. You certainly set me on the right path. Apologies for the slow reply, I meant to get the entire thing solved but haven't gotten around to the final part of making it work in the keybind script yet.
Turns out the last part was pretty simple. I just deleted all the menu stuff leaving the function as follows in Conditions.lua:
CONDITIONS["pcall"] = {
handler =
function (cond, context, text)
if text then
local f, err = loadstring("return " .. text)
if f and err == nil then
local ok, rc = pcall(f)
return ok and rc
end
end
end
}
Then I added the following to the Keybinding1 script in Advanced Options just before the SmartMount
line.
IF [pcall:IsBagOpen(0)]
Mount Traveler's Tundra Mammoth
END
Works a charm. If I need to vendor some stuff I just open my bags and it summons my Tundra Mammoth for me.
I should also note, you can use this in the rules UI by using "Advanced" as the conditon and putting the whole condition without the [] into the text box.