Script: trouble with tables.

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
Post Reply
User avatar
cromcrom
Posts: 549
Joined: Tue Sep 11, 2012 7:16 am
Location: Chateauroux in a socialist s#!$*&% formerly known as "France"

Script: trouble with tables.

Post 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 ?
A trip of a thousand leagues starts with a step.
Batty
Posts: 509
Joined: Sun Apr 15, 2012 7:04 pm

Re: Script: trouble with tables.

Post 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?
User avatar
Montis
Posts: 340
Joined: Sun Apr 15, 2012 1:25 am
Location: Grimrock II 2nd playthrough (hard/oldschool)

Re: Script: trouble with tables.

Post 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
When destiny calls, the chosen have no choice.

My completed dungeon (LoG1): Hypercube
Lilltiger
Posts: 95
Joined: Sun Sep 16, 2012 1:12 am

Re: Script: trouble with tables.

Post 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
User avatar
cromcrom
Posts: 549
Joined: Tue Sep 11, 2012 7:16 am
Location: Chateauroux in a socialist s#!$*&% formerly known as "France"

Re: Script: trouble with tables.

Post 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.
Last edited by cromcrom on Sun Oct 07, 2012 12:37 am, edited 1 time in total.
A trip of a thousand leagues starts with a step.
Lilltiger
Posts: 95
Joined: Sun Sep 16, 2012 1:12 am

Re: Script: trouble with tables.

Post by Lilltiger »

You are welcome :)
Post Reply