Thread Tools Display Modes
01/14/17, 03:42 PM   #1
Augestflex
Join Date: Jan 2016
Posts: 4
LootAll for ITEMTYPE_CONTAINER After UseItem

Hello!


This is my first post in the developer discussions, and I hope I got the right sub topic, so please bear with me and thank you in advance for any assistance or comments. I have been searching through the very wonderful wiki and API calls as well as looking through other addon code.

I'm working on my first ESO LUA addon, with great help from looking at exiting addons. Note I do not have LUA experience but I have spent around 10 years programming with strongly typed languages though the syntax of LUA is driving me nuts (I am learning... ok slowly).

Now for the problem:
From the user's inventory I've called "UseItem" using CallSecureProtected passing in the bag and slotIndex on an item type of ITEMTYPE_CONTAINER. This pops up a loot window with several items that are in the container sitting in the loot window. All I want to do for this step is loot all these items, but I can't seem to hook into being able to interact with this loot window.

Additionally I've registered for events of EVENT_LOOT_UPDATED thinking this would give me an event call to my addon method that I could then interact with. Here is how I registered for the event:

Code:
EVENT_MANAGER:RegisterForEvent(addon.name, EVENT_LOOT_UPDATED, LootAllContents)
Note that I've tried implementing both of the following and I never see the debug messages for entering and leaving my LootallContents method though I see my debug messages elsewhere, and obviously since the loot window opens up I know I've at least made it that far. conPrint just implements d().

Code:
-- lootall with eventcode passed
local function LootAllContents(eventCode)
conPrint("Entering LootAllContents")
  LootAll(true)
conPrint("Leaving LootallContents")
end

-- lootall with no params passed
local function LootAllContents()
conPrint("Entering LootAllContents")
  LootAll(true)
conPrint("Leaving LootallContents")
end

Any thoughts on what I am doing wrong? I'm sure I am either missing something totally simple/obvious.


Thanks in advance for any comments and advice.

Last edited by Augestflex : 01/15/17 at 01:28 PM.
  Reply With Quote
01/14/17, 06:51 PM   #2
Dolgubon
 
Dolgubon's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2016
Posts: 408
This is the code that I use (stripped of some additional unnecessary logic) It's possible the show inventory bit doesn't need to be on a delay, it's been a while since I coded it.

[code]
local function OnLootUpdated(event)
if autoLoot then
LOOT_SHARED:LootAllItems()
zo_callLater(function() SYSTEMS:GetObject("mainMenu"):ShowCategory(MENU_CATEGORY_INVENTORY)end, 50)
end
end

EVENT_MANAGER:RegisterForEvent(WritCreater.name, EVENT_LOOT_UPDATED ,OnLootUpdated )
[\code]
l think that you need to do LOOT_SHARED:LootAllItems() instead of just LootAllItems(). Also, are you coming from Java or something? Lua has no concept of overloading functions. The second function you have listed will simply overwrite the first one listed. Instead, you'll need to build it in yourself, likely with if eventcode then 1stfunction() else 2ndfunction() end, or if not eventcode then eventcode = 1 end.

It looks like you've added the event properly, but double check that the event is added below the lootallcontents, or that it's added in the initialization function. If you add it before that, it's not going to call anything.
  Reply With Quote
01/15/17, 04:36 AM   #3
Baertram
Super Moderator
 
Baertram's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 4,912
Welcome. Just a quick question:
You know there is an option ingame to autoloot all (works with containers too if I remember right).

So maybe check the esoui sources too for this option.
  Reply With Quote
01/15/17, 01:27 PM   #4
Augestflex
Join Date: Jan 2016
Posts: 4
Dolgubon, Baertram: Thank you so much for reading my post, I realize it is a bit lengthy, and for taking the time to respond to it.

Originally Posted by Dolgubon View Post
This is the code that I use (stripped of some additional unnecessary logic) It's possible the show inventory bit doesn't need to be on a delay, it's been a while since I coded it.

Code:
local function OnLootUpdated(event)
	if autoLoot then
		LOOT_SHARED:LootAllItems()
		zo_callLater(function() SYSTEMS:GetObject("mainMenu"):ShowCategory(MENU_CATEGORY_INVENTORY)end, 50)
	end
end

EVENT_MANAGER:RegisterForEvent(WritCreater.name, EVENT_LOOT_UPDATED ,OnLootUpdated )
l think that you need to do LOOT_SHARED:LootAllItems() instead of just LootAllItems(). Also, are you coming from Java or something? Lua has no concept of overloading functions. The second function you have listed will simply overwrite the first one listed. Instead, you'll need to build it in yourself, likely with if eventcode then 1stfunction() else 2ndfunction() end, or if not eventcode then eventcode = 1 end.

It looks like you've added the event properly, but double check that the event is added below the lootallcontents, or that it's added in the initialization function. If you add it before that, it's not going to call anything.

For structured languages that are developed with functions I'm not used to a positional requirement that certain code needs to be placed above calling code/events. Generally if the method name and parameters are correct, and you use the correct class/object/etc. then the location of a method becomes irrelevant in my experience. Good to know this is not the case with LUA. I've now moved my LootAllContents(eventCode) above the line number where I register it for the event.

Regarding function overloading, my programming experience is C++, light Java, and many years of C# and VB.NET development. Thanks for the heads up regarding no overloading support. No worries, as you describe, it is easy enough to make one method and then manually check with if/then to change program flow instead of the convenience of overloaded functions.

I've haven't tried LOOT_SHARED:LootAllItems() yet, but that is coming next.

Silly question, I do need the eventCode parameter in the functional decoration for LootAllItems() right?

e.g. I need this:
Code:
local function LootAllContents(eventCode)
Not this:
Code:
local function LootAllContents()

Originally Posted by Baertram View Post
Welcome. Just a quick question:
You know there is an option ingame to autoloot all (works with containers too if I remember right).

So maybe check the esoui sources too for this option.

Hello Baertram, thank you for the time to read and respond, greatly appreciated.

I do have the in game Settings --> Gameplay --> autoloot options (even for stolen items) set to Yes. Interestingly (and maybe this is due to an addon or ZOS bug, that I should look into), it works for "most" containers but not for all.

However, in this case looting all contents of the container in the inventory is only the second (or third) step of several things I want to do in the addon. e.g. In this case, the user explicitly calls the addon through either a keybind or item context menu. Then step two the addon opens and loots the container, then step three the addon continues to the next container, loots it, (repeat that step), then the last actions occur.


Thanks!

Last edited by Augestflex : 01/15/17 at 01:32 PM. Reason: edit to address question regarding overloading.
  Reply With Quote
01/15/17, 04:26 PM   #5
Dolgubon
 
Dolgubon's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2016
Posts: 408
You don't need the eventcode in the function declaration, technically. I prefer to add it, because some events do pass useful stuff, and it makes it a bit clearer that it's an event function, but you don't need it.

So, since I realized I wasn't too clear on how the 'overloading' works:

local function abc(first, second, third, fourth)
end

If you pass no parameters, then in abc, first, through fourth will be nil. Passing just one will mean that second, third, and fourth are nil. You could also do abc(nil, 1, nil, 2) meaning first and third are nil. You'll probably see eventually in the eso API that there's some parameters labelled nilable. That means that it can be nil.

I highly suggest reading some lua tutorials. Lua has quite a few things that are different, that can trip people up, not least of which is function placement.
  Reply With Quote
01/15/17, 09:47 PM   #6
Augestflex
Join Date: Jan 2016
Posts: 4
Lua

Thanks again!! Greatly appreciated.

It is very interesting that you can actually call a method with less parameters than the decoration and that it automatically nil's any remaining parameters. So it is almost like a form of built in simple override functionality.
  Reply With Quote
01/16/17, 01:59 AM   #7
sirinsidiator
 
sirinsidiator's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 1,566
Hi and welcome, Augustflex!
You should take a look at this Lua guide for programmers. It explains some of the core concepts of Lua.
If you haven't done so already, you should also read the Getting Started guide on the wiki and do this tutorial which gives you an overview of the ESO api.
  Reply With Quote
01/16/17, 07:56 PM   #8
Augestflex
Join Date: Jan 2016
Posts: 4
Originally Posted by sirinsidiator View Post
Hi and welcome, Augustflex!
You should take a look at this Lua guide for programmers. It explains some of the core concepts of Lua.
If you haven't done so already, you should also read the Getting Started guide on the wiki and do this tutorial which gives you an overview of the ESO api.

Thanks for the links and tips! I've read through the Simple Notebook Tutorial but had not created it. I now have LootAllContents being called and am just about to start a new test to see if it will work as expected.

The LUA tutorial looks good!
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » LootAll for ITEMTYPE_CONTAINER After UseItem

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