|
||||||
| Lua Code Discussion You scared? Terrified. Mortified. Petrified. Stupefied... by [coding]. | ||||||
![]() |
|
|
Thread Tools |
|
|
#1 |
|
Member
Join Date: Nov 2008
Location: On a top secret hidden island resort in a big mansion
Posts: 33
|
hey all,
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 :/ Code:
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
Code:
elseif (event == "TIME_PLAYED_MSG") then
-- Remember play time
AllTime = SecondsToTime(a1);
LevelTime = SecondsToTime(a2);
and these are my session variables(note some of them actually dont do anything i need to clean these up lol): Code:
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!
|
|
|
|
|
|
#2 |
|
Junior Member
Join Date: Sep 2008
Posts: 68
|
Code:
LevelTime = SecondsToTime(a2);
__________________
I'm not crying. It's just been raining on my face. |
|
|
|
|
|
#3 |
|
Senior Member
Join Date: Jun 2006
Posts: 483
|
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. |
|
|
|
|
|
#4 |
|
Member
Join Date: Nov 2008
Location: On a top secret hidden island resort in a big mansion
Posts: 33
|
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 |
|
|
|
|
|
#5 |
|
Member
Join Date: Nov 2008
Location: On a top secret hidden island resort in a big mansion
Posts: 33
|
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
Code:
X = string.find("some number of 45", "%d+")
|
|
|
|
|
|
#6 |
|
Asian Sheep Lover
Join Date: Aug 2007
Location: Singapore
Posts: 4,033
|
The first 2 numbers that string.find() returns is the starting character and ending character number.
Code:
local x, y = string.find("some number of 45", "%d+")
What you want is Code:
local _, _, x = string.find("some number of 45", "(%d+)")
Code:
local x = string.match("some number of 45", "(%d+)")
__________________
Author/Maintainer of Postal, Omen3, GemHelper, BankItems, WoWEquip, GatherMate, Routes, HandyNotes and some others. |
|
|
|
|
|
#7 |
|
Amazing Member
|
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)).
__________________
WoWWiki has moved! Please, make a note of it. My Addons (a handy dandy list). My Characters (yes, I have a guild with only me in it). |
|
|
|
|
|
#8 |
|
Member
Join Date: Nov 2008
Location: On a top secret hidden island resort in a big mansion
Posts: 33
|
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?
|
|
|
|
|
|
#9 |
|
Legendary Member
Join Date: Dec 2006
Posts: 2,321
|
"_" 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.:
Code:
local _, _, x = string.find("some number of 45", "(%d+)")
One could also write it like this: Code:
local x = select(3, string.find("some number of 45", "(%d+)"))
Code:
local start, _, x = string.find("some number of 45", "(%d+)")
__________________
Author of Inline Aura, AdiBags, Squire2 and several other addons. Each time you hit your "copy" command with a block of code, think about a way to refactor it so it did what you want without using the "paste" command. |
|
|
|
|
|
#10 |
|
Legendary Member
Join Date: May 2006
Location: Arizona
Posts: 3,787
|
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() ]] )
__________________
Author of GuildCraft, SickOfClickingDailies, CursorCooldown, Broken_LFD WoWAce Addon List WoWInterface Addon List "I was there in the beginning... and things were very different back then" --An Echo from a time before. |
|
|
|
![]() |
«
Previous Thread
|
Next Thread
»
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|
All times are GMT. The time now is 02:43 AM.
WowAce Forums






