sorry to bug again but i cant seem to get the addon i'm working on to register that my variables should be numbers. not strings. but for some reason when i load the addon it loads with an error saying it cant do the arithmetic for string value. when i looked over all the code i diddnt find any links to strings so i might just not be typing it up right i dunno :/
function LvlWizRev_EstXpTime()
local lvltime = LevelTime
local UXP=UnitXP("PLAYER")
ET = UXP / lvltime * 3600
print (ET)
--if ET==0 then ET="inf."end
--LvlWizRev_Tooltip:AddLine("|cFF6495EDEstimated time to level up: "..ET.."|r")
end
this is the function i'm having issues with and it tells me that lvltime is the problem "string"
elseif (event == "TIME_PLAYED_MSG") then
-- Remember play time
AllTime = SecondsToTime(a1);
LevelTime = SecondsToTime(a2);
is what's in my Onevent function that grabs the initial data
and these are my session variables(note some of them actually dont do anything i need to clean these up lol):
local LevelTime =0
local lvltime = 0
local LWR_currXP = 0
local LWR_exptogo = 0
local LWR_Init = nil
local WML = 0
local NUMMOBS = 0
local TPL = 0
local TX = 0; --(total amount of xp needed to level)--
local UXP = 0; --(current amount of xp aquired)--
local PT = 0 --(current amount of played time)--
local lxp = {} --(current player XP)--
local mkc=0 --(kills needed on specific mob to level up)--
local lastkillXP=0 --(last kill xp reward)--
local TL= {} --(level of the current target selected)--
local LWR_prevXP=0
PLT = 0
local prev_update = 0
local prev_update_delay = 4
local LvlWizRev_Update_Freq = 1
then finally it all is supposed to wrap in nice and neatly into my LvlWizRev_ShowTooltip() function which works for everything else. i've been over this bit of code trying to tweak and change it but i usually end up with a string error or cant do an upvalue on nil, so i'm lost lol any help is greatly appreciated! :)
SecondsToTime() return a formatted string, not a number. I don't know where you get a2 in your TIME_PLAYED_MSG event processing function but, assuming it's a number of seconds, you need to keep that value.
My advice would be to use SecondsToTime() only when you want to display the string and only keep the "raw" numerical value until you need to display it.
If you really need to keep the localized and formatted value, put it in separate variable, for example LevelTimeText.
thanks for the help guys, i switched the LevelTime call to just = a2 then made a secondary call in the function to convert to SecondsToTime() to get a time format. i also found a way to add more lines into it for a more precise look :)
i guess i shouldof figured SecondsToTime() was a string lol :P
just out of curiosity, is it possible to pull a number out of a string and use the number value in math? i tried like in the following but i never get an exact number, just a lower number
X = string.find("some number of 45", "%d+")
but when i call that it doesnt return the number 45 it returns something less which makes it no good for use in math if i need the number in question to be pulled
FYI: string.match() (and string.find() for that matter) always returns a string. So if you need to do arithmetic on the returned value, you need to pass it to tonumber() (e.g. x = tonumber(x)).
ahh i see... another thing that is puzzling me that i was looking up in the lua refrence manual and i cant seem to find... what is _, ? is that like ome unused variable?
"_" is a legit variable name, as would be "x" or "a". In Lua, it is often used as a placeholder to discard return values, e.g.:
local _, _, x = string.find("some number of 45", "(%d+)")
This assigns the first two return values from string.find to _. _ then contains the value of the second return value (since it is assigned last) but we will never use it.
One could also write it like this:
local x = select(3, string.find("some number of 45", "(%d+)"))
But this implies a function call, which may be slower than assigning to local variables. Moreover, using _ is sometimes more flexible, like this:
local start, _, x = string.find("some number of 45", "(%d+)")
the underscore _ is used as the common "i don't care about that return" variable. Just remember to use a local _ not the global _ ( IE [[ local _, a = someFunc() ]] vs [[ _, a = sumfunc() ]] )
the underscore _ is used as the common "i don't care about that return" variable. Just remember to use a local _ not the global _ ( IE [[ local _, a = someFunc() ]] vs [[ _, a = sumfunc() ]] )
i thought i'd post my finished line of code here for research purposes for anyone else that might be a bit confounded as i was.
in Levelwiz_revamped.lua:
elseif (event == "CHAT_MSG_COMBAT_XP_GAIN") then
local _,_,_,killEXP,_ = string.find(a1, LWR_STR_COMBATXPNOTE)
if killEXP then LWR_prevXPkill = tonumber(killEXP) end
if LWR_prevXPkill < 0 then LWR_prevXPkill = 0 end
end
in my localization.lua
LWR_STR_COMBATXPNOTE = "(.*) dies, you gain (%d+) experience."
it may very well be possible that i have more then one _ in the beginning of my call handler but after a lot of testing this is the string that worked for what i was needing called.
credit goes to the author of titanpanel XP of whom i got the idea that it was possible to seperate xp gains from kill gains and based the majority of the code off of.
thanks again for all your guys help! with this line completed i can officially call my addon ready!:D (even if it is just a rehash :P )
LWR_STR_COMBATXPNOTE = "(.*) dies, you gain (%d+) experience."
[...]
local _,_,_,killEXP,_ = string.find(a1, LWR_STR_COMBATXPNOTE)
1) If you're going to ignore the first capture, then just... don't capture it.
LWR_STR_COMBATXPNOTE = ".* dies, you gain (%d+) experience."
2) Not all the return values of a function need to be assigned to variables. In particular, values *after* the last one you're interested in can just be ignored instead of assigned to trailing dummy names.
local _,_,killEXP = string.find(a1, LWR_STR_COMBATXPNOTE)
sorry to bug again but i cant seem to get the addon i'm working on to register that my variables should be numbers. not strings. but for some reason when i load the addon it loads with an error saying it cant do the arithmetic for string value. when i looked over all the code i diddnt find any links to strings so i might just not be typing it up right i dunno :/
this is the function i'm having issues with and it tells me that lvltime is the problem "string"
is what's in my Onevent function that grabs the initial data
and these are my session variables(note some of them actually dont do anything i need to clean these up lol):
then finally it all is supposed to wrap in nice and neatly into my LvlWizRev_ShowTooltip() function which works for everything else. i've been over this bit of code trying to tweak and change it but i usually end up with a string error or cant do an upvalue on nil, so i'm lost lol any help is greatly appreciated! :)
SecondsToTime returns a string. SecondsToTime(3600) returns "1 Hr" (in enUS). So you should set lvltime to a2.
My advice would be to use SecondsToTime() only when you want to display the string and only keep the "raw" numerical value until you need to display it.
If you really need to keep the localized and formatted value, put it in separate variable, for example LevelTimeText.
i guess i shouldof figured SecondsToTime() was a string lol :P
but when i call that it doesnt return the number 45 it returns something less which makes it no good for use in math if i need the number in question to be pulled
x, y will be 16 and 17 respectively because "4" is the 16th character in the string and "5" is the 17th character.
What you want is
The () around the %d+ tells the find() function to also return the actual number as the 3rd return. This is called a string capture. Or you can
match() doesn't return the first 2 results, just the remainder of the string captures.
This assigns the first two return values from string.find to _. _ then contains the value of the second return value (since it is assigned last) but we will never use it.
One could also write it like this:
But this implies a function call, which may be slower than assigning to local variables. Moreover, using _ is sometimes more flexible, like this:
Here we only discard the second return value.
I've adopted using $_ in my PHP programming. lol
in Levelwiz_revamped.lua:
in my localization.lua
it may very well be possible that i have more then one _ in the beginning of my call handler but after a lot of testing this is the string that worked for what i was needing called.
credit goes to the author of titanpanel XP of whom i got the idea that it was possible to seperate xp gains from kill gains and based the majority of the code off of.
thanks again for all your guys help! with this line completed i can officially call my addon ready!:D (even if it is just a rehash :P )
1) If you're going to ignore the first capture, then just... don't capture it.
2) Not all the return values of a function need to be assigned to variables. In particular, values *after* the last one you're interested in can just be ignored instead of assigned to trailing dummy names.