2016-03-12 2 views
1

Я хочу сделать это, когда я нажимаю кнопку, меняется переменная переменных.Изменение переменной в функции

function BuyItem(price, quantity, pps, text, quantitytext) 
    if(PixoosQuantity >= price) then 
     PixoosQuantity = PixoosQuantity - price 
     price = price * 1.1 

     quantity = quantity + 1 

     PixoosPerSecond = PixoosPerSecond + pps 
     PixoosPerSecondDisplay.text = "PPS: " .. string.format("%.3f", PixoosPerSecond) 
     PixoosQuantityDisplay.text = "Pixoos: " .. string.format("%.3f", PixoosQuantity) 

     text.text = "Deck of playing cards\nPrice: " .. string.format("%.3f", price) .. " Pixoos" 
     quantitytext.text = quantity 
    end 
end 

Это функция, которая вызывается при нажатии кнопки:

function ButtonAction(event) 
    if event.target.name == "DeckOfPlayingCards" then 
     BuyItem(DeckOfPlayingCardsPrice, DeckOfPlayingCardsQuantity, DeckOfPlayingCardsPPS, DeckOfPlayingCardsText, DeckOfPlayingCardsQuantityText) 
    end 
end 

Мой вопрос, почему не изменить переменные? Я попытался поставить return price и так далее, но он все еще не работает ...

ответ

1

Вы передаете переменную price по значению, а не by reference. Эта конструкция не существует в Lua, так что вам нужно, чтобы обойти его, например, с помощью возвращаемого значения:

DeckOfPlayingCardsPrice, DeckOfPlayingCardsText, DeckOfPlayingCardsQuantityText = BuyItem(DeckOfPlayingCardsPrice, [...], DeckOfPlayingCardsText, DeckOfPlayingCardsQuantityText) 

и возвращают ожидаемое значение правильно:

function BuyItem(price, quantity, pps, text, quantitytext) 
    if(PixoosQuantity >= price) then 
     [...] 
    end 
    return price, quantity, quantitytext 
end 

В Lua вы можете return multiple results.

+0

Проблема в том, что мне нужно изменить не только цену, но и количество, текст, а также количественный текст. Как я могу сделать все это в одной функции? Возможно ли это? – FICHEKK

+0

В Lua вы можете вернуть больше значений и присвоить их соответствующим переменным. Я изменил ответ. – Jakuje

+0

Ты брат! Спасибо, мужик! Это решило! – FICHEKK

Смежные вопросы