Page 1 of 1

Using the for command

Posted: Wed Mar 06, 2013 11:21 am
by noobbotter
I'm working on building a function to call to deposit certain items from my backpack into my bank. I'm not exactly certain how the "for" command works with using a table.

For example, I build a table with the names or IDs of the items I want to deposit into my bank, let's call this table "bankitems". How would I loop through that table checking my inventory and if it finds the item in inventory, it moves it to the bank? Would this do it?

Code: Select all

function move_to_bank()
	for i, item in pairs(bankitems) do 
		item.moveTo("bank")
	end
end
I'm not exactly sure what the "pairs" thing means, and I'm not very good with "for" functions. I basically want it to say "for each item in the "bankitems" table, do this with it"...

Thanks.

Re: Using the for command

Posted: Wed Mar 06, 2013 11:58 am
by rock5
There are basically 2 common forms of the for loop and you can use both to iterate through a table.

Code: Select all

for i = 1, #bankitems do
   v = bankitems[i]   
   ...
end
In each iteration 'i' increments from 1 to the number of items in the list. 'v' will equal the item name or id in the list.

Code: Select all

for i, v in pairs(bankitems) do
   ...
end
What 'in pairs' does is return the index and value of each item in the list. Note, the way these are written they do exactly the same thing. The i and v values would be the same.

Your example wouldn't work because 'item' would just be the name or id from the list. You can use it, though, to find and move the item. eg

Code: Select all

itemObj = inventory:findItem(item)
if itemObj then
    itemObj:moveTo("bank")
end
Hope that helps.

Re: Using the for command

Posted: Wed Mar 06, 2013 12:19 pm
by noobbotter
Thanks for the good explanation Rock. So then what I am looking for would be something like this?

Code: Select all

function move_to_bank()
	for i, v in pairs(bankitems) do 
		itemObj = inventory:findItem(v)
		if itemObj then
			itemObj:moveTo("bank")
		end
	end
end
Another question... In a separate table I'll be using to mail items to a different character, I list mostly IDs of items I want to mail, but some of the items in the table, I only list name because there are so many different variations of the same item (such as Random Fusion Stones). Will the UMM_SendByNameOrId("Charname",mailitems) be ok if the table is mixed between ID's and Names? Here's a portion of my table:

Code: Select all

520582,  -- Advance II
520622,  -- Block II
521092,  -- Ferocity II
"Fusion Stone",
"Random Fusion Stone",
205821,  -- Temporary Mount Use Voucher
205818,  -- Royal Furniture Recipe Collection Box
There are like 8 or so different IDs in runesdatabase for Random Fusion Stone so I figured I'd use the Name for that one instead. would that be ok to mix strings and numbers in that table if being used by your mailing function?

Re: Using the for command

Posted: Wed Mar 06, 2013 1:09 pm
by rock5
Yes they can be mixed. It does say "NameOrId" after all. In fact, because it does partial name matches also, you only need "Fusion Stone" because it will also match "Random Fusion Stone".

Re: Using the for command

Posted: Wed Mar 06, 2013 1:30 pm
by noobbotter
Oh, sweet. I didn't know that function would do partial patches because I couldn't find any examples of that function where someone used partials. That will also prevent me from having to do "Fearless I", "Fearless II, "Potential I", "Potential II", etc....

Thanks again.