Thread Tools Display Modes
11/19/14, 01:52 PM   #1
laiqalasse
Join Date: Nov 2014
Posts: 3
Help for new addon programer

Hi guys,

I'm totally new in lua and addon programming. I'm trying to make my first addon but getting an ui error 'checking type on argument callback failed in ScriptEventManagerRegisterforEventlua stack traceback: [C]:addons/.... .lua 53 in function 'Initialize'. I googled it but couldn't find anything do u know why i am getting this error.

Thanks.
  Reply With Quote
11/19/14, 03:04 PM   #2
merlight
AddOn Author - Click to view addons
Join Date: Jul 2014
Posts: 671
EVENT_MANAGER:RegisterForEvent("SomeGloballyUniqueString_PrefereablyYourAddonName", EVENT_CODE, callback)

callback must be a function, you're passing something else. Can't tell you more atm, error message is not enough, you'll need to give us the code that leads to it
  Reply With Quote
11/19/14, 03:28 PM   #3
laiqalasse
Join Date: Nov 2014
Posts: 3
oh sorry,but it is shame to share this bad algorithm and codes as i told this is my first addon and i did it for myself
Code:
local junker = {
	
	name = "junker"
}


local junk_types =
{
	"ITEMTYPE_ADDITIVE",
	"ITEMTYPE_ALCHEMY_BASE",
	"ITEMTYPE_AVA_REPAIR",
	"ITEMTYPE_CLOTHIER_BOOSTER",
	"ITEMTYPE_CLOTHIER_MATERIAL",
	"ITEMTYPE_CLOTHIER_RAW_MATERIAL",
	"ITEMTYPE_COLLECTIBLE",
	"ITEMTYPE_CONTAINER",
	"ITEMTYPE_DISGUISE",
	"ITEMTYPE_ENCHANTING_RUNE",
	"ITEMTYPE_ENCHANTMENT_BOOSTER",
	"ITEMTYPE_FLAVORING",
	"ITEMTYPE_FOOD",
	"ITEMTYPE_GLYPH_ARMOR",
	"ITEMTYPE_GLYPH_JEWELRY",
	"ITEMTYPE_GLYPH_WEAPON",
	"ITEMTYPE_INGREDIENT",
	"ITEMTYPE_LURE",
	"ITEMTYPE_NONE",
	"ITEMTYPE_PLUG",
	"ITEMTYPE_RAW_MATERIAL",
	"ITEMTYPE_STYLE_MATERIAL",
	"ITEMTYPE_TRASH"

}


local junkarmor_types ={

"ARMORTYPE_LIGHT",
"ARMORTYPE_MEDIUM",
"ARMORTYPE_NONE"

}




local name = "junker"

local function Initialize()


EVENT_MANAGER:RegisterForEvent(name,EVENT_INVENTORY_SINGLE_SLOT_UPDATE,junkJunk)
end


local function  OnAddOnLoaded(event,addonName)
if addonName == "junker" then

	Initialize()

	end
end

local function junkJunk(eventCode,bagId,slotId,isNewItem,itemSoundCategory, updateReason)

local armor_type = nil
item_type = GetItemType(bagId,slotId)
local item_link = GetItemLink(bagId,slotId)

	if item_type == "ITEMTYPE_ARMOR" then
	
		local armor_type= GetItemLinkArmorType(item_link)
	
	end
	if contains(junk_types,item_type) or contains(junkarmor_types,armor_type) then
		
		SetItemIsJunk(bagId,slotId)
		
		d("junked")
	end
	
end


local function contains(junk_types, element)

  for _, value in pairs(junk_types) do

    if value == element then

      return true
    end

  end

  return false
end


EVENT_MANAGER:RegisterForEvent("junker",EVENT_ADD_ON_LOADED,OnAddOnLoaded)
  Reply With Quote
11/19/14, 04:09 PM   #4
Garkin
 
Garkin's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 832
Local function "junkJunk" must be defined before line where it is called. Fixed code:

lua Code:
  1. local junker = {
  2.     name = "junker"
  3. }
  4.  
  5. local junk_types = {
  6.     "ITEMTYPE_ADDITIVE",
  7.     "ITEMTYPE_ALCHEMY_BASE",
  8.     "ITEMTYPE_AVA_REPAIR",
  9.     "ITEMTYPE_CLOTHIER_BOOSTER",
  10.     "ITEMTYPE_CLOTHIER_MATERIAL",
  11.     "ITEMTYPE_CLOTHIER_RAW_MATERIAL",
  12.     "ITEMTYPE_COLLECTIBLE",
  13.     "ITEMTYPE_CONTAINER",
  14.     "ITEMTYPE_DISGUISE",
  15.     "ITEMTYPE_ENCHANTING_RUNE",
  16.     "ITEMTYPE_ENCHANTMENT_BOOSTER",
  17.     "ITEMTYPE_FLAVORING",
  18.     "ITEMTYPE_FOOD",
  19.     "ITEMTYPE_GLYPH_ARMOR",
  20.     "ITEMTYPE_GLYPH_JEWELRY",
  21.     "ITEMTYPE_GLYPH_WEAPON",
  22.     "ITEMTYPE_INGREDIENT",
  23.     "ITEMTYPE_LURE",
  24.     "ITEMTYPE_NONE",
  25.     "ITEMTYPE_PLUG",
  26.     "ITEMTYPE_RAW_MATERIAL",
  27.     "ITEMTYPE_STYLE_MATERIAL",
  28.     "ITEMTYPE_TRASH"
  29. }
  30.  
  31.  
  32. local junkarmor_types ={
  33.     "ARMORTYPE_LIGHT",
  34.     "ARMORTYPE_MEDIUM",
  35.     "ARMORTYPE_NONE"
  36. }
  37.  
  38. local function contains(junk_types, element)
  39.     for _, value in pairs(junk_types) do
  40.         if value == element then
  41.             return true
  42.         end
  43.     end
  44.     return false
  45. end
  46.  
  47. local function junkJunk(eventCode,bagId,slotId,isNewItem,itemSoundCategory, updateReason)
  48.     local armor_type = nil
  49.     local item_type = GetItemType(bagId,slotId)
  50.     local item_link = GetItemLink(bagId,slotId)
  51.  
  52.     if item_type == "ITEMTYPE_ARMOR" then
  53.         armor_type= GetItemLinkArmorType(item_link)
  54.     end
  55.  
  56.     if contains(junk_types,item_type) or contains(junkarmor_types,armor_type) then
  57.         SetItemIsJunk(bagId,slotId)
  58.         d("junked")
  59.     end
  60. end
  61.  
  62. local function Initialize()
  63.     EVENT_MANAGER:RegisterForEvent(junker.name, EVENT_INVENTORY_SINGLE_SLOT_UPDATE, junkJunk)
  64. end
  65.  
  66.  
  67. local function  OnAddOnLoaded(event,addonName)
  68.     if addonName == "junker" then
  69.         Initialize()
  70.     end
  71. end
  72.  
  73. EVENT_MANAGER:RegisterForEvent(junker.name, EVENT_ADD_ON_LOADED, OnAddOnLoaded)
I have just changed order when functions are defined.

If you want also make this code a bit faster, do not use function "contains", but define your tables as follows and just check if table keys exists:
Lua Code:
  1. local junk_types = {
  2.     "ITEMTYPE_ADDITIVE" = true,
  3.     "ITEMTYPE_ALCHEMY_BASE" = true,
  4.     "ITEMTYPE_AVA_REPAIR" = true,
  5.     "ITEMTYPE_CLOTHIER_BOOSTER" = true,
  6.     "ITEMTYPE_CLOTHIER_MATERIAL" = true,
  7.     "ITEMTYPE_CLOTHIER_RAW_MATERIAL" = true,
  8.     "ITEMTYPE_COLLECTIBLE" = true,
  9.     "ITEMTYPE_CONTAINER" = true,
  10.     "ITEMTYPE_DISGUISE" = true,
  11.     "ITEMTYPE_ENCHANTING_RUNE" = true,
  12.     "ITEMTYPE_ENCHANTMENT_BOOSTER" = true,
  13.     "ITEMTYPE_FLAVORING" = true,
  14.     "ITEMTYPE_FOOD" = true,
  15.     "ITEMTYPE_GLYPH_ARMOR" = true,
  16.     "ITEMTYPE_GLYPH_JEWELRY" = true,
  17.     "ITEMTYPE_GLYPH_WEAPON" = true,
  18.     "ITEMTYPE_INGREDIENT" = true,
  19.     "ITEMTYPE_LURE" = true,
  20.     "ITEMTYPE_NONE" = true,
  21.     "ITEMTYPE_PLUG" = true,
  22.     "ITEMTYPE_RAW_MATERIAL" = true,
  23.     "ITEMTYPE_STYLE_MATERIAL" = true,
  24.     "ITEMTYPE_TRASH" = true
  25. }
  26.  
  27.  
  28. local junkarmor_types ={
  29.     "ARMORTYPE_LIGHT" = true,
  30.     "ARMORTYPE_MEDIUM" = true,
  31.     "ARMORTYPE_NONE" = true
  32. }
  33.  
  34. --modified function without "contains"
  35. local function junkJunk(eventCode,bagId,slotId,isNewItem,itemSoundCategory, updateReason)
  36.     local armor_type = nil
  37.     local item_type = GetItemType(bagId,slotId)
  38.     local item_link = GetItemLink(bagId,slotId)
  39.  
  40.     if item_type == "ITEMTYPE_ARMOR" then
  41.         armor_type= GetItemLinkArmorType(item_link)
  42.     end
  43.  
  44.     if junk_types[item_type] or (armor_type and junkarmor_types[armor_type]) then --make sure that armor_type is not nil before you use it as an index of the table
  45.         SetItemIsJunk(bagId,slotId)
  46.         d("junked")
  47.     end
  48. end
  49.  
  50. local function Initialize()
  51.     EVENT_MANAGER:RegisterForEvent("junker", EVENT_INVENTORY_SINGLE_SLOT_UPDATE, junkJunk)
  52. end
  53.  
  54.  
  55. local function  OnAddOnLoaded(event,addonName)
  56.     if addonName == "junker" then
  57.         Initialize()
  58.     end
  59. end
  60.  
  61. EVENT_MANAGER:RegisterForEvent(junker.name, EVENT_ADD_ON_LOADED, OnAddOnLoaded)

Last edited by Garkin : 11/19/14 at 04:15 PM.
  Reply With Quote
11/19/14, 04:20 PM   #5
merlight
AddOn Author - Click to view addons
Join Date: Jul 2014
Posts: 671
Lua Code:
  1. local junk_types = {
  2.     ITEMTYPE_ADDITIVE = true, -- like this
  3.     ["ITEMTYPE_ALCHEMY_BASE"] = true, -- or this, it's the same
  4.     "ITEMTYPE_AVA_REPAIR" = true, -- not this, that's wrong syntax :)
  5.         ...
  6. }

edit: but neither will do what you need, see below, circonian has it right

Last edited by merlight : 11/19/14 at 05:16 PM.
  Reply With Quote
11/19/14, 05:01 PM   #6
circonian
AddOn Author - Click to view addons
Join Date: May 2014
Posts: 613
In this code:
Lua Code:
  1. item_type = GetItemType(bagId,slotId)
  2. if item_type == "ITEMTYPE_ARMOR" then
  3. --...--

GetItemType(bagId,slotId) could return ITEMTYPE_ARMOR if the item is a piece of armor, but ITEMTYPE_ARMOR is actually an integer, so you can't compare it to a string with:
Lua Code:
  1. if item_type == "ITEMTYPE_ARMOR" then
  2. -- that wont work

You might want to change your tables to something like this:
Lua Code:
  1. local junk_types =
  2. {
  3.     [ITEMTYPE_ADDITIVE]             = true,
  4.     [ITEMTYPE_ALCHEMY_BASE]         = true,
  5.     [ITEMTYPE_AVA_REPAIR]           = true,
  6.     [ITEMTYPE_CLOTHIER_BOOSTER]     = true,
  7.     [ITEMTYPE_CLOTHIER_MATERIAL]    = true,
  8.     [ITEMTYPE_CLOTHIER_RAW_MATERIAL] = true,
  9.     [ITEMTYPE_COLLECTIBLE]          = true,
  10.     [ITEMTYPE_CONTAINER]            = true,
  11.     [ITEMTYPE_DISGUISE]             = true,
  12.     [ITEMTYPE_ENCHANTING_RUNE]      = true,
  13.     [ITEMTYPE_ENCHANTMENT_BOOSTER]  = true,
  14.     [ITEMTYPE_FLAVORING]            = true,
  15.     [ITEMTYPE_FOOD]             = true,
  16.     [ITEMTYPE_GLYPH_ARMOR]      = true,
  17.     [ITEMTYPE_GLYPH_JEWELRY]    = true,
  18.     [ITEMTYPE_GLYPH_WEAPON]     = true,
  19.     [ITEMTYPE_INGREDIENT]       = true,
  20.     [ITEMTYPE_LURE]             = true,
  21.     [ITEMTYPE_NONE]             = true,
  22.     [ITEMTYPE_PLUG]             = true,
  23.     [ITEMTYPE_RAW_MATERIAL]     = true,
  24.     [ITEMTYPE_STYLE_MATERIAL]   = true,
  25.     [ITEMTYPE_TRASH]            = true,
  26. }
Since ItemTypes are all unique integers thier keys in the table will be unique. Set each of their values to true or false, based on whether or not the itemType is supposed to be junk.

Then when your trying to determine if something is junk or not you don't need to call an extra contains(...) function. you can just check that table to see if it is true/false (junk or not junk)
Lua Code:
  1. item_type = GetItemType(bagId,slotId)
  2. if junk_types[item_type] then
  3.    -- item is junk
  4. end

Oh and so the other part of your code would have to be changed to:
Lua Code:
  1. -- notice, no quotes around ITEMTYPE_ARMOR
  2. if item_type == ITEMTYPE_ARMOR then
  3.    local armor_type= GetItemLinkArmorType(item_link)
  4. end

Last edited by circonian : 11/19/14 at 05:09 PM.
  Reply With Quote
11/19/14, 05:17 PM   #7
Garkin
 
Garkin's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 832
Ah, circonian is right. I didn't think about what should be in the table. Tables should be defined as circonian said.

Originally Posted by circonian View Post
Oh and so the other part of your code would have to be changed to:
Lua Code:
  1. -- notice, no quotes around ITEMTYPE_ARMOR
  2. if item_type == ITEMTYPE_ARMOR then
  3.    local armor_type= GetItemLinkArmorType(item_link)
  4. end
Just one comment - if you write it like this, armor_type won't be visible outside of the if ... end code block.

Lua Code:
  1. local armor_type
  2. if item_type == ITEMTYPE_ARMOR then
  3.     armor_type= GetItemLinkArmorType(item_link)
  4. end

Last edited by Garkin : 11/19/14 at 05:23 PM.
  Reply With Quote
11/19/14, 05:50 PM   #8
circonian
AddOn Author - Click to view addons
Join Date: May 2014
Posts: 613
Originally Posted by Garkin View Post
Just one comment - if you write it like this, armor_type won't be visible outside of the if ... end code block.

Oh yeah, I didn't catch that one, I just copied what he had and took the ""s off

Hes right, if you do
Lua Code:
  1. if item_type == ITEMTYPE_ARMOR then
  2.     local armor_type= GetItemLinkArmorType(item_link)
  3. end
  4. -- Then armor_type wont be available out here

Depending upon what your going to do with it, you may or may not need it outside of that if statement. If you do then you'll have to do what he said.
Lua Code:
  1. local armor_type
  2. if item_type == ITEMTYPE_ARMOR then
  3.     armor_type= GetItemLinkArmorType(item_link)
  4. end
  5. -- Now you can use armor_type out here

Last edited by circonian : 11/19/14 at 06:06 PM.
  Reply With Quote
11/20/14, 01:34 AM   #9
laiqalasse
Join Date: Nov 2014
Posts: 3
guys i m realy gratefull for your interest, thanks it was really helpfull and instructive. now thanks to u i have my own addon
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » Help for new addon programer


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