Property exceeds below 0

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 :smiley: (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.

THANK YOU SO MUCH! :slight_smile:

Script:

import bge

def money():

cont = bge.logic.getCurrentController()
own = cont.owner

scene = bge.logic.getCurrentScene()
gun1 = scene.objects["gun1"]

keyboard = bge.logic.keyboard


key1 = bge.logic.KX_INPUT_JUST_ACTIVATED == keyboard.events[bge.events.RKEY]


###own["money"] = 1800
own.text = str(own["money"])

if key1:
    own["money"] -= 472
    gun1["ammo"] += 15

money()

CHEERS!!

  • Chris

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


Hi! Thanks for the answer! You’re awesome!I’ll change the script and see what happens.Cheers!- Chris

Here are a few hints:

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


money = 0

def getMoney():
   return money

def setMoney(amount):
   global money
   money

theBigSpender.py


import pocket

def showHowToSetAndGetMoney():
   print("current money", pocket.getMoney())
   print("put 112 into the pocket)
   setMoney(112)
   print("current money", pocket.getMoney())