WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   AddOn Help/Support (https://www.wowinterface.com/forums/forumdisplay.php?f=3)
-   -   Need help with Stuf Unit Frames custom LUA (https://www.wowinterface.com/forums/showthread.php?t=52885)

Darsyek 11-18-15 06:47 AM

Need help with Stuf Unit Frames custom LUA
 
Hey there,

As the title suggests, I'm looking for some help with some custom LUA coding for Stuf Unit Frames. I'll try to be as specific as I can be while trying this at 5:45am. :D

What I'm trying to do is have my power bar display a percentage for any class that uses mana and a flat number for everything else (ie. energy, rage, runic power, focus, etc.)

I've spent a couple hours researching the UnitPower LUA stuff, but I just couldn't get it to work. Here's the code I have so far:

Code:

function(unit, cache, textframe)
if UnitPowerType("player") == 0 then
    local z = math.max(0, UnitPower("player")) / math.max(1, UnitPowerMax("player")) * 100;
    return string.format("%.f", z) .. "%%";
else return UnitPower("player") end

Any help would be greatly appreciated. :p

Fizzlemizz 11-18-15 12:40 PM

Code:

if UnitPowerType("player") == 0 then
    return math.ceil(UnitPower("player") / UnitPowerMax("player") * 100) .."%"
else
    return UnitPower("player")
end

Your function didn't have an end statement but I'm asuming was just missed in the copy.

Darsyek 11-18-15 04:46 PM

Thank you for your reply. My previous code did actually have an end statement. It was tucked away at the end of the else statement. Unfortunately, your code doesn't seem to work for me either. The power bar is just blank, which I really don't understand. It's like the code isn't even getting past the if statement. :confused:

I did a little bit of testing with the code, and I can get it to display UnitPowerType as text or UnitPower as a flat unit, so I know it can handle those arguments, but for whatever reason it seems to stall on the if statements. I even tried to set up a local variable and use that in the if statement to display just plain text and it didn't work. I'm beginning to wonder if Stuf Unit Frames even allows if statements at all. D:

semlar 11-18-15 05:10 PM

Quote:

Originally Posted by Darsyek (Post 311835)
Thank you for your reply. My previous code did actually have an end statement. It was tucked away at the end of the else statement.

Your posted code does not have an "end" for the function, only the "if" statement.

If that is your actual code then it would be throwing a lua error.

Darsyek 11-18-15 06:21 PM

Code:

function(unit, cache, textframe)
if UnitPowerType("player") == 0 then
    local z = math.max(0, UnitPower("player")) / math.max(1, UnitPowerMax("player")) * 100;
    return string.format("%.f", z) .. "%%";
else return UnitPower("player") end

That is the exact code from my original post. You can even go read the code from the original post. I haven't even edited it, it's exactly as it was when I posted it in the first place. Note the last line again: else return UnitPower("player") end. Like I said, my original code does have an end statement - at the end of the else statement. Changing what line the end statement is on makes no impact on its function (or lack thereof). Stuf Unit Frames also doesn't display any LUA errors when you're using custom code. It seems to suppress it, otherwise I'd be looking through the error logs myself.

semlar 11-18-15 06:23 PM

You need another end at the end of that to close the function.

You are only closing the "if" statement. You aren't closing the function.

Lua errors need to be enabled through the "help" section of the in-game escape menu if they aren't enabled.

Fizzlemizz 11-18-15 06:35 PM

Your code:
Code:

function(unit, cache, textframe)
    if UnitPowerType("player") == 0 then
        local z = math.max(0, UnitPower("player")) / math.max(1, UnitPowerMax("player")) * 100;
        return string.format("%.f", z) .. "%%";
    else
        return UnitPower("player")
    end

Proper code:
Code:

function(unit, cache, textframe)
    if UnitPowerType("player") == 0 then
        local z = math.max(0, UnitPower("player")) / math.max(1, UnitPowerMax("player")) * 100;
        return string.format("%.f", z) .. "%%";
    else
        return UnitPower("player")
    end
end

Proper indentation helps.

Darsyek 11-18-15 07:10 PM

Well. I feel like an idiot now. :o I had been assuming that the end statement was for the function as a whole. I suppose I should have realized that, considering my programming background is with C and C++. I suppose I should have assumed that the end statement worked like } does in those languages or that you couldn't write an if else statement without closing the else like C++. I have a lot to learn about LUA still it seems. I really appreciate your help, both of you. :P Sorry if I seemed like an ass.

In any case, it seems that it works now. Even with my original code. I just needed that extra end. Damn curly brackets were always the bane of my existence with C++ too. >_>


All times are GMT -6. The time now is 01:06 PM.

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