View Single Post
10/14/23, 03:33 PM   #2
sirinsidiator
 
sirinsidiator's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 1,579
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:
  1. local startIndex, endIndex = victomCharacterName:find("^%w+%s*")
  2. if startIndex then
  3. victomCharacterName = victomCharacterName:sub(startIndex, endIndex)
  4. end
A (imho) more elegant way would be to let the localization system take care of it for you and simply pass the name through zo_strformat:
Lua Code:
  1. victomCharacterName = zo_strformat("<<1>>", victomCharacterName)

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.
  Reply With Quote