Thread Tools Display Modes
09/16/15, 11:31 PM   #1
Motokosworld
Join Date: Sep 2015
Posts: 6
move item from bag to bank

Hi!
I a new to this forum here, i hope i got the right tread to ask a question. I am from Germany so sorry for any wrong English sentences/words.
I wanna make some Addons for TESO.
On the first hand to get better in coding on the other hand to have some to fit my needs.
I learn java at university and I am new to lua.
I tried to move an item from the bagpack to the bank but it do not work.
I used the "my first addon" help out of the wiki from esoui. And looked into other persons code. But still not working.

I want to move all items of a special kind to the bank, for example all food. I also wanted to use the "y" key function to stack if you can use it. ATM i do not find that function.

Hope someone can give me an example or some tipps. Also some webseites with more tutorials or code explanation would be helpful.
AT my programming its always a little problem /wrong thinking that stops all. >.<""!


My code atm: move 1 item and then crash game >.<



function FooAddon.OnOpenBank(eventcode)
for num = 1, GetBagSize(1), 1 do
firstFreeSlot = FindFirstEmptySlotInBag(2)
icon, istack, sellPrice, meetsUsageRequirement, locked, equipType, itemStyle, quality = GetItemInfo(1, num)
if 0 ~= GetNumBagFreeSlots(2) then
if IsProtectedFunction("RequestMoveItem") then
CallSecureProtected("RequestMoveItem", 1, num, 2, firstFreeSlot, istack)
else
RequestMoveItem(1, num, 2, firstFreeSlot, istack)
end
end
end
end

Last edited by Motokosworld : 09/17/15 at 01:23 AM.
  Reply With Quote
09/17/15, 01:27 AM   #2
sirinsidiator
 
sirinsidiator's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 1,581
Hello and welcome to EsoUI, Motokosworld!

You found the right place for asking this kind of question.

What you want to do can be broken down into four parts:
1, Offer a way to trigger your function
2. Detect when you are at a bank
3. Find all instances of the item in your inventory and move them to the bank
4. Use the 'y' stack function to stack them

I will offer you a basic example that you can use as a starting point.
ad 1)
There are many ways how this can happen.
You can offer a chat command, automatically trigger it whenever the user visits a bank, add a keybind or show some GUI elements that can be clicked.
The first two are comparatively simple while the others require quite some code, so we will stick to a chat command for now.
Lua Code:
  1. SLASH_COMMANDS["/dumpall"] = function()
  2. MyAddon.DumpItemsToBank()
  3. end

ad 2)
This is necessary because you cannot access BAG_BANK unless you are at a banker and chose the bank dialog.
For now we will add a function to prevent DumpItemsToBank from doing anything while we are not at a bank.
Lua Code:
  1. local function IsAtBank()
  2.   return GetInteractionType() == INTERACTION_BANK
  3. end
  4.  
  5. function MyAddon.DumpItemsToBank()
  6.   if not IsAtBank() then return end
  7. end

ad 3)
Now we get to the interesting part. First we have to iterate over all our items and test if they are of the type we want to dump.
You mentioned that you want to move all food to bank, so we stick to that for now.
You already found the necessary methods so everything we need to do is combine it like this:
Lua Code:
  1. for slotIndex = 1, GetBagSize(BAG_BACKPACK) do
  2.   if GetItemType(BAG_BACKPACK, slotIndex) == ITEMTYPE_FOOD then
  3.     MoveItemToBank(slotIndex)
  4.   end
  5. end

GetFirstFreeValidSlotForItem seems to work on the same bag only as it requires a slotIndex to specify an item. FindFirstEmptySlotInBag(integer Bag bagId) is more likely to do what we want so we will try it.
We also need to get the stack size in order to move the correct amount. This can be done with GetSlotStackSize(integer bagId, integer slotIndex).
Once we have the slot we can move request to move it there with the function you already have found.
Lua Code:
  1. local function MoveItemToBank(slotIndex)
  2.   local stackCount = GetSlotStackSize(BAG_BACKPACK, slotIndex)
  3.   local destSlot = FindFirstEmptySlotInBag(BAG_BANK)
  4.   CallSecureProtected("RequestMoveItem", BAG_BACKPACK, slotIndex, BAG_BANK, destSlot, stackCount)
  5. end

ad 4)
All that is left now is to find out how the key bind to stack items is working. For this we need to have a look at the UI source code which can be found here.
The bank is simply another inventory so we take a look at ingame/inventory/inventory.lua and by looking for the keyword "stack" we find StackBag(BAG_BANK).
So after we put everything together in DumpItemsToBank we arrive at:
Lua Code:
  1. function MyAddon.DumpItemsToBank()
  2.   if not IsAtBank() then return end
  3.  
  4.   for slotIndex = 0, GetBagSize(BAG_BACKPACK)-1 do
  5.     if GetItemType(BAG_BACKPACK, slotIndex) == ITEMTYPE_FOOD then
  6.       MoveItemToBank(slotIndex)
  7.     end
  8.   end
  9.  
  10.   StackBag(BAG_BANK)
  11. end

This is completely untested so it might not work right away, but it should give you a general idea how it can be done.

EDIT: Just saw that you updated your post. Instead of adding a chat command you can just call MyAddon.DumpItemsToBank() from your event handler too and of course it will be necessary to check if the bank has enough space left before trying to move the item.
EDIT2: corrected code in response to votan

Last edited by sirinsidiator : 09/17/15 at 02:59 AM.
  Reply With Quote
09/17/15, 01:49 AM   #3
Motokosworld
Join Date: Sep 2015
Posts: 6
Thank you very much for your answer!
I will work now with the things you wrote and will write later how far i came with it!
  Reply With Quote
09/17/15, 02:30 AM   #4
Motokosworld
Join Date: Sep 2015
Posts: 6
Thx votan for your advice. About that i was also thinking, because out of java i know that everything start at 0 and then max-1 so i correct that now.

The problem is, it work 1 time if i open the bank and then again if i open the bank again. SO i think the loop do not work, but why? If i try out some thinks like quote out the if that i only move food, it works once and then if i talk to the banker the second time my game crash.
Maybe there is a problem with that function(RequestMoveItem) or the inventory do not update correctly? i will try out...


My code now much more clean and nice written code with a lot of comments^^, some are still from the tutorial, but i leave them there to learn the functions of all the lua code parts:


Lua Code:
  1. -- First, we create a namespace for our addon by declaring a top-level table that will hold everything else.
  2. BankService = {}
  3.  
  4. -- This isn't strictly necessary, but we'll use this string later when registering events.
  5. -- Better to define it in a single place rather than retyping the same string.
  6. BankService.name = "BankService"
  7.  
  8. -- if the bank is open start this
  9. function BankService.OnOpenBank()
  10.  
  11.     --Run through all slots of the bag
  12.     for slotInBag = 0, GetBagSize(BAG_BACKPACK)-1, 1 do
  13.    
  14.         --check if there is still space in bank and the slot is not empty
  15.         if 0 ~= GetNumBagFreeSlots(BAG_BANK) and GetItemName(BAG_BACKPACK, slotInBag) ~= "" then
  16.    
  17.             -- check if the itemtype is food
  18.             if GetItemType(BAG_BACKPACK, slotInBag) == ITEMTYPE_FOOD then
  19.            
  20.                 -- find a free bankslot
  21.                 local freeBankSlot = FindFirstEmptySlotInBag(BAG_BANK)
  22.                
  23.                 -- read out the stackSize of that item
  24.                 local stackSize = GetSlotStackSize(BAG_BACKPACK, slotInBag)
  25.                
  26.                 --use the protected function to move the item to the bank
  27.                 if IsProtectedFunction("RequestMoveItem") then
  28.                     CallSecureProtected("RequestMoveItem", BAG_BACKPACK, slotInBag, BAG_BANK, freeBankSlot, stackSize)
  29.                 else
  30.                     RequestMoveItem(BAG_BACKPACK, slotInBag, BAG_BANK, freeBankSlot, stackSize)
  31.                 -- close if checkProtectedFunction
  32.                 end
  33.                
  34.                 --use the integrated stack function to stack all in bank
  35.                 StackBag(BAG_BANK)
  36.                
  37.             -- close if checkItemtype
  38.             end
  39.         -- close if checkSpace and notEmpty
  40.         end
  41.     --close loop       
  42.     end
  43. --close function   
  44. end
  45.  
  46.  
  47. -- initialze function
  48. function BankService:Initialize()
  49.     --Register our event
  50.     EVENT_MANAGER:RegisterForEvent(self.name,  EVENT_OPEN_BANK, self.OnOpenBank)       
  51. end
  52.  
  53.  
  54. -- Event handler function, check if the loaded addon is mine
  55. -- Initialize our addon after all of its resources are fully loaded.
  56. function BankService.OnAddOnLoaded(event, addonName)
  57.   -- The event fires each time *any* addon loads - but we only care about when our own addon loads.
  58.   if addonName == BankService.name then
  59.     BankService:Initialize()
  60.   end
  61. end
  62.  
  63. -- Finally, we'll register our event handler function to be called when the proper event occurs.
  64. EVENT_MANAGER:RegisterForEvent(BankService.name, EVENT_ADD_ON_LOADED, BankService.OnAddOnLoaded)

Last edited by Motokosworld : 09/17/15 at 03:00 AM.
  Reply With Quote
09/17/15, 02:57 AM   #5
sirinsidiator
 
sirinsidiator's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 1,581
Originally Posted by Motokosworld View Post
The problem is, it work 1 time if i open the bank and then again if i open the bank again. SO i think the loop do not work, but why?
That's what you are doing with your code. You react to EVENT_OPEN_BANK which is fired once when the bank opens, so it only runs one time when you open the bank and again when you open the bank another time.
If you want it to run again while you are still at the bank you need to provide a different way to trigger the function.
  Reply With Quote
09/17/15, 08:23 AM   #6
Motokosworld
Join Date: Sep 2015
Posts: 6
But I wrote a loop around that all, why it do not go through the loop? Normaly a loop means go through it until you checked all values inside the loop...

Or do I only need to write the loop into another subfunction?

Last edited by Motokosworld : 09/17/15 at 08:36 AM.
  Reply With Quote
09/17/15, 01:56 AM   #7
votan
 
votan's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2014
Posts: 578
slot-indexes are (the only) zero-based indexes.

Lua Code:
  1. for slotIndex = 0, GetBagSize(BAG_BACKPACK)-1 do ...

Und Willkommen, Motokosworld.
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » move item from bag to bank


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