Page 1 of 1

Script: trouble with tables.

Posted: Sat Oct 06, 2012 6:49 pm
by cromcrom
So, I created a table of tables for my merchant script:

Code: Select all

function setUpItems()
ItemOptions = {
{Item="dagger",sellPrice=0.5,buyPrice=5},
{Item="cutlass",sellPrice=0.7,buyPrice=7},
{Item="fist_dagger",sellPrice=0.7,buyPrice=10},
{Item="flail",sellPrice=1,buyPrice=15},
{Item="great_axe",sellPrice=2,buyPrice=25},
{Item="hand_axe",sellPrice=0.7,buyPrice=6},
{Item="knife",sellPrice=0.4,buyPrice=4},
{Item="long_sword",sellPrice=0.7,buyPrice=13},
{Item="machete",sellPrice=0.6,buyPrice=10},
{Item="shuriken",sellPrice=0.3,buyPrice=10},
{Item="throwing_axe",sellPrice=0.2,buyPrice=8},
{Item="throwing_knife",sellPrice=0.2,buyPrice=8},
end
but when I try to cycle through the tables one by one, with the click of a button, with this function:

Code: Select all

function changeItem()
setUpItems()
local counterItem = merchant_item_type_counter_1:getValue()
 for i,option in pairs(ItemOptions) do
 hudPrint(" A "..option.Item.." to buy for "..option.buyPrice.." gold coins.")
 end
 end
it cycles through all the items. i don't know how to use the counter to cycle through objects one by one.

Any help please ?

Re: Script: trouble with tables.

Posted: Sat Oct 06, 2012 7:00 pm
by Batty
I guess don't use a loop, just have a button activate a function that adds one to the counter (a global variable) or subtracts one for going backwards?

Re: Script: trouble with tables.

Posted: Sat Oct 06, 2012 7:15 pm
by Montis
well you're missing a closing bracket.
other than that I'm not familiar enough with lua tables such as this one to be of any further help

Re: Script: trouble with tables.

Posted: Sat Oct 06, 2012 7:57 pm
by Lilltiger
You want something like this

Code: Select all

function setUpItems()
   
   local ItemOptions = {
   {Item="dagger",sellPrice=0.5,buyPrice=5},
   {Item="cutlass",sellPrice=0.7,buyPrice=7},
   {Item="fist_dagger",sellPrice=0.7,buyPrice=10},
   {Item="flail",sellPrice=1,buyPrice=15},
   {Item="great_axe",sellPrice=2,buyPrice=25},
   {Item="hand_axe",sellPrice=0.7,buyPrice=6},
   {Item="knife",sellPrice=0.4,buyPrice=4},
   {Item="long_sword",sellPrice=0.7,buyPrice=13},
   {Item="machete",sellPrice=0.6,buyPrice=10},
   {Item="shuriken",sellPrice=0.3,buyPrice=10},
   {Item="throwing_axe",sellPrice=0.2,buyPrice=8},
   {Item="throwing_knife",sellPrice=0.2,buyPrice=8}

   return ItemOptions
}
end

function changeItem()
   -- I prefere to use return values rather then temporary globals
   item_list = setUpItems()
   
   if( merchant_item_type_counter_1:getValue() > #item_list ) then
      -- counter passed the available items reset it.
      merchant_item_type_counter_1:reset()
   end

   -- Get the item from the list
   item = item_list[merchant_item_type_counter_1]
   hudPrint(" A "..item.Item.." to buy for "..item.buyPrice.." gold coins.")
end

Re: Script: trouble with tables.

Posted: Sat Oct 06, 2012 9:35 pm
by cromcrom
Thank you so much for the lesson Lilltiger, adding you to TLC credits ;-)

Actually, I checked your code, return ItemOptions should be just before "end", and you forgot a :getValue(). But it works great, thanks again.

Re: Script: trouble with tables.

Posted: Sun Oct 07, 2012 12:11 am
by Lilltiger
You are welcome :)