Using the for command

Runes of Magic/Radiant Arcana (http://www.runesofmagic.com)
Post Reply
Message
Author
noobbotter
Posts: 527
Joined: Fri Aug 31, 2012 1:15 pm

Using the for command

#1 Post by noobbotter » Wed Mar 06, 2013 11:21 am

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.

User avatar
rock5
Posts: 12173
Joined: Tue Jan 05, 2010 3:30 am
Location: Australia

Re: Using the for command

#2 Post by rock5 » Wed Mar 06, 2013 11:58 am

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.
  • Please consider making a small donation to me to support my continued contributions to the bot and this forum. Thank you. Donate
  • I check all posts before reading PMs. So if you want a fast reply, don't PM me but post a topic instead. PM me for private or personal topics only.
  • How to: copy and paste in micromacro
    ________________________
    Quote:
    • “They say hard work never hurt anybody, but I figure, why take the chance.”
          • Ronald Reagan

noobbotter
Posts: 527
Joined: Fri Aug 31, 2012 1:15 pm

Re: Using the for command

#3 Post by noobbotter » Wed Mar 06, 2013 12:19 pm

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?

User avatar
rock5
Posts: 12173
Joined: Tue Jan 05, 2010 3:30 am
Location: Australia

Re: Using the for command

#4 Post by rock5 » Wed Mar 06, 2013 1:09 pm

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".
  • Please consider making a small donation to me to support my continued contributions to the bot and this forum. Thank you. Donate
  • I check all posts before reading PMs. So if you want a fast reply, don't PM me but post a topic instead. PM me for private or personal topics only.
  • How to: copy and paste in micromacro
    ________________________
    Quote:
    • “They say hard work never hurt anybody, but I figure, why take the chance.”
          • Ronald Reagan

noobbotter
Posts: 527
Joined: Fri Aug 31, 2012 1:15 pm

Re: Using the for command

#5 Post by noobbotter » Wed Mar 06, 2013 1:30 pm

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.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest