ESOUI

ESOUI (https://www.esoui.com/forums/index.php)
-   General Authoring Discussion (https://www.esoui.com/forums/forumdisplay.php?f=174)
-   -   Why doesn't this piece of code work? (https://www.esoui.com/forums/showthread.php?t=6663)

Xego 12/04/16 01:48 AM

Why doesn't this piece of code work?
 
Hello, I have this code in my addon:

Code:

RegisterForEvent(EVENT_LOOT_RECEIVED, XL.OnItemLooted) 

               
function XL.OnItemLooted (numID, lootedBy, itemName, quantity, itemSound, lootType, self)

        bFound = false

        d (itemName)

        if (string.find(itemName, "Needle") ~= nil) then bFound  = true
        end

        -- Do something here
end

The above never detects the presence of the "Needle" string in the itemName. Why? The code seems correct.

1) I verified that the IF works by pre-injecting my own strings into itemName.

2) The d(itemName) displays correctly formed item link in chat.

How do I convert the itemName link to searchable string?
Thanks!

Baertram 12/04/16 05:41 AM

How exactly does itemName look like if the event callback function is triggered? What is in the variable? And what is shown if you post it to the chat using d(itemName)?

The event callback function parameters seem to be correct.
EVENT_LOOT_RECEIVED (integer eventCode, string receivedBy, string itemName, number quantity, number itemSound, number lootType, boolean self, boolean isPickpocketLoot, string questItemIcon, number itemId)

Xego 12/04/16 08:02 AM

Quote:

Originally Posted by Baertram (Post 29058)
How exactly does itemName look like if the event callback function is triggered? What is in the variable? And what is shown if you post it to the chat using d(itemName)?

The event callback function parameters seem to be correct.
EVENT_LOOT_RECEIVED (integer eventCode, string receivedBy, string itemName, number quantity, number itemSound, number lootType, boolean self, boolean isPickpocketLoot, string questItemIcon, number itemId)

d(itemName) shows a valid link in chat that can be clicked. Do you know how to display the "raw" content of itemName?

Is it a unicode issue?

Baertram 12/04/16 09:45 AM

No, it's the item's itemLink then, and not only the name of the item.
To get the item's name from that link use the function here:

Code:

* GetItemLinkName(*string* _itemLink_)
** _Returns:_ *string* _itemName_

Use it like this then:

btw: Is it correct that your variable "bFound" is a global one?
If you use global variables please create one global array for your addon's namespace like this
Code:

myAddonName = {}
and add your addon's variables to it then. Otherwise you might get in trouble if any other addon uses "bFound" too!

Code:

myAddonName.bFound = false
Lua Code:
  1. RegisterForEvent(EVENT_LOOT_RECEIVED, XL.OnItemLooted)  
  2.  
  3.        
  4. function XL.OnItemLooted (numID, lootedBy, itemLinkStr, quantity, itemSound, lootType, self)
  5.  
  6.     myAddonName.bFound = false
  7.  
  8.         d (itemLinkStr)
  9.         local itemName = GetItemLinkName(itemLinkStr)
  10.  
  11.     if itemName ~= nil and (string.find(itemName, "Needle") ~= nil) then myAddonName.bFound  = true
  12.     end
  13.  
  14.     -- Do something here
  15. end

Xego 12/04/16 01:24 PM

Thanks that function solved the problem.

As for the globals, I did use the "local" keyword but I removed it to make the code shorter here.


All times are GMT -6. The time now is 05:39 AM.

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