10/14/23, 02:29 PM | #1 |
|
string.find or string.sub question
Ive been messing with thew new ingame pvp kill feed which i love but I wanted to refine ot to a mmore personal level to not spam out my chat So ive added it to my addon but have been having ocasional popup error which I haven't found the exact cause of.
The new EVENT_PVP_KILL_FEED_DEATH is perfect for my need but has some issues. it fires twice for every kill/death so ive had to make a switch to not display same data twice which was easy enough but now I have a rare popup error "expected integer, got nil" on this line: Code:
victomCharacterName = victomCharacterName:sub(victomCharacterName:find("^%w+%s*")) Code:
"^%w+%s*" "^%w+(%s%w+)" "^%w+%s*%w*%s*%w*%s*%w*%s*%w*" error example: Code:
bad argument #2 to 'string.sub' (integer expected, got nil) stack traceback: [C]: in function 'string.sub' user:/AddOns/RidinDirty/RidinDirty.lua:1626: in function 'RidinDirty.KillFeedAudio' |caaaaaa<Locals> eventCode = 131386, killLocation = "Chalman Keep", killerDisplayName = "@sinnereso", killerCharacterName = "Michael Cullen^Mx", killerAlliance = 2, killerRank = 50, victomDisplayName = "@Irishpierre", victomCharacterName = "Imize^Mx", victomAlliance = 1, victomRank = 16 </Locals>|r Code:
victomCharacterName = victomCharacterName:sub(victomCharacterName:find("^%w+%s*")) Last edited by sinnereso : 10/14/23 at 02:46 PM. |
10/14/23, 03:33 PM | #2 |
Your error happens because string.find can either return 2 numbers when a result is found, or nil otherwise.
As a result, in case the name string does not contain a localization tag like ^F your code will produce an error since string.sub expects two numbers as input. One solution would be to store the start and end values into local variables and only call sub when start is not nil: Lua Code:
Lua Code:
Regardless you should never directly pass return values from one function into another unless you own them, as it's a troublesome source of future errors in case either of them changes their signature and the number of returns or input arguments changes. For example a while ago ZOS introduced a new argument "_aSuppressCallbackHandler_" to the SetText method of the editbox. In one of my addons I simply passed the return value of another function that returned the text and some extra values since the extras would just go into the void, right? Hope you can imagine how difficult it was to figure out why the code that worked perfectly fine for years suddenly stopped working. Last edited by sirinsidiator : 10/14/23 at 03:36 PM. |
|
10/14/23, 03:47 PM | #3 | |
|
|
|
10/14/23, 04:20 PM | #4 |
|
this seems to be working so far:
Code:
victomCharacterName = victomCharacterName:sub(victomCharacterName:find("^[%w ]+")) EDIT: Still gettin the random error.. ill have to revert back to stock CHARACTER NAME^Mx chat output for now until I get get it sorted. I thought I was gonna be a simple fix. Last edited by sinnereso : 10/14/23 at 05:00 PM. |
10/15/23, 03:59 AM | #5 | |
Code:
victomCharacterName = zo_strformat("<<1>>", victomCharacterName) Code:
<<C:1>> |
||
10/15/23, 08:10 AM | #6 | |
In other words, you are making assumptions about the input value that do not seem to hold true. EVENT_PVP_KILL_FEED_DEATH can and will return character names that do not include ^Mx or ^Fx at the end. |
||
10/15/23, 08:53 AM | #7 | |
|
||
10/15/23, 08:57 AM | #8 |
|
Code:
victomCharacterName = zo_strformat("<<1>>", victomCharacterName) |
10/15/23, 09:26 AM | #9 | |
|
Code:
function RidinDirty.KillFeedAudio(eventCode, killLocation, killerDisplayName, killerCharacterName, killerAlliance, killerRank, victomDisplayName, victomCharacterName, victomAlliance, victomRank) if GetUnitDisplayName('player') == killerDisplayName and RidinDirty.victomName ~= victomDisplayName then--GetUnitName("player") RidinDirty.savedVariables.pvpKills = (RidinDirty.savedVariables.pvpKills + 1) RidinDirty.victomName = victomDisplayName-- Duplicate entry filter PlaySound(RidinDirty.ultSnd) if RidinDirty.PvpKillFeedEnabled() then return end if not ZO_ShouldPreferUserId() then victomCharacterName = zo_strformat("<<1>>", victomCharacterName)-- Strip gemders df(RidinDirty.logo .. " |cCC6600You have killed " .. tostring(victomCharacterName) .. " [KB-" .. tostring(RidinDirty.savedVariables.pvpKills) .. " / D-" .. tostring(RidinDirty.savedVariables.pvpDeaths) .. "]|r") else df(RidinDirty.logo .. " |cCC6600You have killed " .. tostring(victomDisplayName) .. " [KB-" .. tostring(RidinDirty.savedVariables.pvpKills) .. " / D-" .. tostring(RidinDirty.savedVariables.pvpDeaths) .. "]|r") end elseif GetUnitDisplayName('player') == victomDisplayName and RidinDirty.victomName ~= victomDisplayName then RidinDirty.savedVariables.pvpDeaths = (RidinDirty.savedVariables.pvpDeaths + 1) RidinDirty.victomName = victomDisplayName-- Duplicate entry filter zo_callLater(function() RidinDirty.victomName = "empty" end, 3000) if RidinDirty.PvpKillFeedEnabled() then return end if not ZO_ShouldPreferUserId() then killerCharacterName = zo_strformat("<<1>>", killerCharacterName)-- Strip genders df(RidinDirty.logo .. " |cCC6600You were killed by " .. tostring(killerCharacterName) .. " [KB-" .. tostring(RidinDirty.savedVariables.pvpKills) .. " / D-" .. tostring(RidinDirty.savedVariables.pvpDeaths) .. "]|r") else df(RidinDirty.logo .. " |cCC6600You were killed by " .. tostring(killerDisplayName) .. " [KB-" .. tostring(RidinDirty.savedVariables.pvpKills) .. " / D-" .. tostring(RidinDirty.savedVariables.pvpDeaths) .. "]|r") end end end Also the EVENT_PVP_KILL_FEED_DEATH has the killer & victom charactername/displayname reversed from the event in the api notes. |
|
ESOUI » Developer Discussions » General Authoring Discussion » string.find or string.sub question |
«
Previous Thread
|
Next Thread
»
|
Display Modes |
Linear Mode |
Switch to Hybrid Mode |
Switch to Threaded Mode |
|
|