Hi guys. I setup this script so whenever I press a button, it deducts my money. However, it continues to deduct to negative. I hope I could have explained it better. lol (Also, I setup my property to 1800 but itâs not getting what I set (unless I set it up directly to the properties in BGE). It just adds my ammo but not deducting the money. I commented it below with ###. I used to get it work but it just didnât). Please help me fix this.
you have two problems right? one that the own[âmoneyâ] = 1800 doesnt work and that the money goes below zero right?
well if you assign own[âmoneyâ] everytime you press r, then the money will not go downâŚ
#assumes your definitions before
# 1 either have a init prop, or 2 dont have "money" in own properties at game start
# 1:
if own["init"] == 0:
own["money"] = 1800
own["init"] = 1
# or 2:
if not "money" in own:
own["money"] = 1800
if key1 and own["money"] > 0:
own["money"] -= 472
own["ammo"] += 15
A) please use code tags when posting code :yes:
B) I strongly suggest to update the âmoney displayâ AFTER the amount changed (rather than before)
C) You better ensure you only take money that is available. (The proposed solution does not do that)
D) You need to define what should happens if more money should be taken than available ⌠Iâm sure someone wants feedback.
Here are a few suggestions:
Return process result:
def showHowToSpendMoneyWithProcessingResult(requestedAmount):
if takeMoney(requestedAmount):
moneyDisplay.text = getMoney()
else:
errorMessageDisplay.text = "not enough money"
def takeMoney(requestedAmount):
'Returns True if the money was spend, otherwise False.'
availableAmount = getMoney():
if availableAmount <= requestedAmount:
return False
setMoney(availableAmount - requestedAmount) # can't be below zero due to the above check
return True
def getMoney():
...
def setMoney(amount):
...
the same but with Error processing:
class NotEnoughMoneyError(Exception):
def __init__(self, missingAmount):
self.missingAmount = missingAmount
...
def showHowToSpendMoneyWithErrorHandling(requestedAmount):
try:
takeMoney(requestedAmount):
moneyDisplay.text = getMoney()
except NotEnoughMoneyError error:
errorMessageDisplay.text = "Not enough money. Missing {}".format(error.missingAmount)
def takeMoney(requestedAmount):
'Raises NotEnoughMoneyError when there is not enough money in stock.'
availableAmount = getMoney():
if availableAmount <= requestedAmount:
raise NotEnoughMoneyError(requestedAmount - availableAmount)
setMoney(availableAmount - requestedAmount) # can't be below zero due to the above check
def getMoney():
...
def setMoney(amount):
...
In both cases the display will only be updated when takeMoney was successful. The differences are:
The error is a real error situation, while the processing result makes âpoor man situationâ a normal situation
the True/False return is not obvious by naming
the error might be caught at higher abstraction levels, without the need to carry the result parameter through the call stack.
many beginners are not used to the âtryâŚexceptâŚâ statements
If you are unsure what getMoney() and setMoney() are doing, here is an example:
pocket.py