Thread Tools Display Modes
12/04/16, 01:48 AM   #1
Xego
Join Date: Apr 2016
Posts: 3
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!

Last edited by Xego : 12/04/16 at 01:52 AM. Reason: Premature send
  Reply With Quote
12/04/16, 05:41 AM   #2
Baertram
Super Moderator
 
Baertram's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 4,912
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)

Last edited by Baertram : 12/04/16 at 05:44 AM.
  Reply With Quote
12/04/16, 08:02 AM   #3
Xego
Join Date: Apr 2016
Posts: 3
Originally Posted by Baertram View Post
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?

Last edited by Xego : 12/04/16 at 08:15 AM.
  Reply With Quote
12/04/16, 09:45 AM   #4
Baertram
Super Moderator
 
Baertram's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 4,912
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
  Reply With Quote
12/04/16, 01:24 PM   #5
Xego
Join Date: Apr 2016
Posts: 3
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.
  Reply With Quote

ESOUI » Developer Discussions » General Authoring Discussion » Why doesn't this piece of code work?

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off