Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I'm working on a game but Im havin a bit of a problem. The problem is for example if i buy 1 ham it charges 55 gold and I get 1 ham but if I want 5 ham I get 5 ham but it only charge 55 gold when it should charge 275 gold.

_state = 0
_count = 0
_index = 0

-- List of items you can buy
items = {}
items[0] = {name = 'ham', id = 456, price = 55}
items[1] = {name = 'fish', id = 201, price = 60}
items[2] = {name = 'mushroom', id = 355, price = 150}
items[3] = {name = 'bread', id = 356, price = 10}

The Function

local function onActionItem(action)

amount = ''
suffix = ''
plural = 'a'

if (_count > 1) then
    amount = ' ' .. tostring(_count)
    suffix = 's'
    plural = ''
end

cost = items[_index].price


selfSay('Do you want to buy ' .. plural .. amount .. ' ' .. items[_index].name .. suffix .. ' for ' .. cost .. ' gold?', 1000)
end
share|improve this question
Your question is off-topic in this site, we strictly improve working code. – Winston Ewert Aug 8 '12 at 15:40

closed as off topic by Winston Ewert Aug 8 '12 at 15:40

Questions on Code Review Stack Exchange are expected to relate to code review request within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.

2 Answers

up vote 1 down vote accepted

You've missed off using _count in the cost

cost = items[_index].price * _count

You also have some tidying up to do with spaces "Do you want to buy5 hams" - two spaces between buy and 5.

For completeness, you'll need extra work on plurals - fish -> fishes, and you want "loaf of bread" (-> loaves of bread) as an item. You may be better off extending the items array to include the plural name rather than assembling it.

share|improve this answer
doh! thx! yes youre right "breads" and "fishs" wont be so popular haha – tomek Aug 1 '12 at 16:16

Change this line:

cost = items[_index].price

to:

cost = items[_index].price * _count
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.