error while implementing inventory

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
monkeykg
Regular
Posts: 48
Joined: Mon Jan 01, 2018 6:36 pm
Projects: DATE KNIGHT
Tumblr: monkeywiki
Contact:

error while implementing inventory

#1 Post by monkeykg »

money worked fine up until now, but now that i've added in a placeholder inventory system (it's someone else's example for now) it's starting to give me weird errors.
For reference, I'm just trying to allow the game to show the money count on screen- and have it cooperate with the inventory system.

EDIT: I'm also getting this error trying to buy anything even if i temporarily remove the main issue of just trying to show the correct money on screen.

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/script.rpy", line 69, in script
    jump buy_more
  File "game/script.rpy", line 69, in <module>
    jump buy_more
  File "game/script.rpy", line 24, in buy
    self.money -= item.cost
TypeError: unsupported operand type(s) for -=: 'unicode' and 'int'

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "game/script.rpy", line 69, in script
    jump buy_more
  File "C:\Users\monkey\Downloads\renpy-6.99.14.1-sdk\renpy\ast.py", line 1729, in execute
    if renpy.python.py_eval(condition):
  File "C:\Users\monkey\Downloads\renpy-6.99.14.1-sdk\renpy\python.py", line 1919, in py_eval
    return py_eval_bytecode(code, globals, locals)
  File "C:\Users\monkey\Downloads\renpy-6.99.14.1-sdk\renpy\python.py", line 1912, in py_eval_bytecode
    return eval(bytecode, globals, locals)
  File "game/script.rpy", line 69, in <module>
    jump buy_more
  File "game/script.rpy", line 24, in buy
    self.money -= item.cost
TypeError: unsupported operand type(s) for -=: 'unicode' and 'int'

Windows-8-6.2.9200
Ren'Py 6.99.14.3.3347
test 1.0
Fri May 25 16:44:52 2018

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/script.rpy", line 43, in script
    $ current_money = inventory.money
  File "game/script.rpy", line 43, in <module>
    $ current_money = inventory.money
NameError: name 'inventory' is not defined

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "game/script.rpy", line 43, in script
    $ current_money = inventory.money
  File "C:\Users\monkey\Downloads\renpy-6.99.14.1-sdk\renpy\ast.py", line 862, in execute
    renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
  File "C:\Users\monkey\Downloads\renpy-6.99.14.1-sdk\renpy\python.py", line 1888, in py_exec_bytecode
    exec bytecode in globals, locals
  File "game/script.rpy", line 43, in <module>
    $ current_money = inventory.money
NameError: name 'inventory' is not defined

Windows-8-6.2.9200
Ren'Py 6.99.14.3.3347
test 1.0
Fri May 25 16:24:54 2018

Code: Select all

screen money:
    zorder 999
    frame:
        background None xalign 0.1 yalign 0.0
        text "[inventory.money] g" color "#fff"

######################STUFF FOR SHOP
init python:
    class item:
        def __init__(self, name, cost):
            self.name = name
            self.cost = cost
            
    class Inventory:
        def __init__(self, goldcoins="200"):
            self.money = goldcoins
            self.items = []
            ##ABOVE IS INITIAL STUFF
    
        def buy(self, item):
            if self.money >= item.cost:
                self.money -= item.cost
                self.items.append(item)
                return True
            else:
                return False
            ##ABOVE IS FOR BUYING STUFF

        def earn(self, amount):
            self.money += amount
            ##ABOVE MEANS U EARN MONEY

    ##items
 ##FLOWERS
    roses = item("Bouquet of Roses", 10)
    tulips = item("Bouquet of Tulips", 10)
    daisies = item("Bouquet of Daisies", 10)

    
label start:
    $ current_money = inventory.money
    $ inventory = Inventory()
    pause
    jump morning
   
label morning:
   show screen money

label afternoon:
    "buy something fuck you."
    
label shop:
    "so many wares.."
    menu:
        "Flowers":
            menu:
                "Bouquet of Roses":
                    show roses at center
                    " That'll cost $10."
                    menu:
                        " I'll buy it.":
                            if inventory.buy(roses):
                                " Thanks for your purchase!"
                            else:
                                "Not enough money..."
                        " Eh, nevermind.":
                            jump buy_more
                       
                "Bouquet of Tulips":
                    show tulips at center
                    " That'll cost $10."
                    menu:
                        " I'll buy it.":
                            if inventory.buy(tulips):
                                " Thanks for your purchase!"
                            else:
                                "Not enough money..."
                        " Eh, nevermind.":
                            jump buy_more
                
                "Bouquet of Daisies":
                    show daisies at center
                    " That'll cost $10."
                    menu:
                        " I'll buy it.":
                            if inventory.buy(daisies):
                                " Thanks for your purchase!"
                            else:
                                "Not enough money..."
                        " Eh, nevermind.":
                            jump buy_more
label buy_more:
    scene store
    " Anything else?"
    menu:
        " No":
            " Have a nice day and come back real soon!"
            jump leave
        "Yes.":
            jump shop
label leave:
    "you leave.."

         
return 

kivik
Miko-Class Veteran
Posts: 786
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

Re: error while implementing inventory

#2 Post by kivik »

Error 1: NameError: name 'inventory' is not defined

Code: Select all

    $ current_money = inventory.money
    $ inventory = Inventory()
Note you're using inventory.money before inventory was declared.

Also, if your inventory is going to manage the money, there seems little point of using the variable current_money


Error 2: TypeError: unsupported operand type(s) for -=: 'unicode' and 'int'

It's saying that you're trying to do subtraction between unicode and int. Here's your problem:

Code: Select all

    class Inventory:
        def __init__(self, goldcoins="200"):
            self.money = goldcoins
You declared goldcoins as a string with goldcoins="200" instead of goldcoins=200.

User avatar
monkeykg
Regular
Posts: 48
Joined: Mon Jan 01, 2018 6:36 pm
Projects: DATE KNIGHT
Tumblr: monkeywiki
Contact:

Re: error while implementing inventory

#3 Post by monkeykg »

kivik wrote: Fri May 25, 2018 7:08 pm Error 1: NameError: name 'inventory' is not defined

Code: Select all

    $ current_money = inventory.money
    $ inventory = Inventory()
Note you're using inventory.money before inventory was declared.

Also, if your inventory is going to manage the money, there seems little point of using the variable current_money


Error 2: TypeError: unsupported operand type(s) for -=: 'unicode' and 'int'

It's saying that you're trying to do subtraction between unicode and int. Here's your problem:

Code: Select all

    class Inventory:
        def __init__(self, goldcoins="200"):
            self.money = goldcoins
You declared goldcoins as a string with goldcoins="200" instead of goldcoins=200.
Ah ty for the reply.

EDIT: With the current fix of THIS entire code, the game no longer gives me errors, but the money count screen isn't visibly showing any decrease of amount with purchases.

Code: Select all


######################STUFF FOR SHOP!!
init python:
    class item:
        def __init__(self, name, cost):
            self.name = name
            self.cost = cost
            
    class Inventory:
        def __init__(self, goldcoins=200):
            self.money = goldcoins
            self.items = []
            ##ABOVE IS INITIAL STUFF??
    
        def buy(self, item):
            if self.money >= item.cost:
                self.money -= item.cost
                self.items.append(item)
                return True
            else:
                return False
            ##ABOVE IS FOR BUYING STUFF

        def earn(self, amount):
            self.money += amount
            ##ABOVE MEANS U EARN MONEY

    ##items
 ##FLOWERS
    roses = item("Bouquet of Roses", 10)
    tulips = item("Bouquet of Tulips", 10)
    daisies = item("Bouquet of Daisies", 10)

screen money:
    zorder 999
    frame:
        background None xalign 0.1 yalign 0.0
        text "[goldcoins] g" color "#fff"

    
label start:
    $ inventory = Inventory()
    $ goldcoins = inventory.money
label waresandbewares:
    show screen money
    scene shop 
    with Dissolve(0.8)
    play music "dateshop_3.mp3" fadein 1.0 fadeout 1.0
    "You enter a mysterious shop."
    
label morning:


label afternoon:
    "buy something fuck you."
    
label shop:
    "so many wares.."
    menu:
        "Flowers":
            menu:
                "Bouquet of Roses":
                    show roses at center
                    " That'll cost $10."
                    menu:
                        " I'll buy it.":
                            if inventory.buy(roses):
                                " Thanks for your purchase!"
                            else:
                                "Not enough money..."
                        " Eh, nevermind.":
                            jump buy_more
                       
                "Bouquet of Tulips":
                    show tulips at center
                    " That'll cost $10."
                    menu:
                        " I'll buy it.":
                            if inventory.buy(tulips):
                                " Thanks for your purchase!"
                            else:
                                "Not enough money..."
                        " Eh, nevermind.":
                            jump buy_more
                
                "Bouquet of Daisies":
                    show daisies at center
                    " That'll cost $10."
                    menu:
                        " I'll buy it.":
                            if inventory.buy(daisies):
                                " Thanks for your purchase!"
                            else:
                                "Not enough money..."
                        " Eh, nevermind.":
                            jump buy_more
label buy_more:
    scene store
    " Anything else?"
    menu:
        " No":
            " Have a nice day and come back real soon!"
            jump leave
        "Yes.":
            jump shop
label leave:
    "you leave.."

         
return 

kivik
Miko-Class Veteran
Posts: 786
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

Re: error while implementing inventory

#4 Post by kivik »

Use inventory.money instead of goldcoins to show how much money there is. Your inventory code changes inventory.money, not goldcoin when you buy stuff.

Assigning inventory.money to goldcoin at the start just puts the current amount in the goldcoin variable, it doesn’t update it afterwards.

User avatar
monkeykg
Regular
Posts: 48
Joined: Mon Jan 01, 2018 6:36 pm
Projects: DATE KNIGHT
Tumblr: monkeywiki
Contact:

Re: error while implementing inventory

#5 Post by monkeykg »

kivik wrote: Sat May 26, 2018 4:15 am Use inventory.money instead of goldcoins to show how much money there is. Your inventory code changes inventory.money, not goldcoin when you buy stuff.

Assigning inventory.money to goldcoin at the start just puts the current amount in the goldcoin variable, it doesn’t update it afterwards.
Ah I'm a bit confused on where to replace this. Do i just get rid of all instances of goldcoins?? I'm pretty sure this is wrong but here's the code anyway

Code: Select all


######################STUFF FOR SHOP!!
init python:
    class item:
        def __init__(self, name, cost):
            self.name = name
            self.cost = cost
            
    class Inventory:
        def __init__(self,inventory.money=200):
            self.money = inventory.money
            self.items = []
            ##ABOVE IS INITIAL STUFF??
    
        def buy(self, item):
            if self.money >= item.cost:
                self.money -= item.cost
                self.items.append(item)
                return True
            else:
                return False
            ##ABOVE IS FOR BUYING STUFF

        def earn(self, amount):
            self.money += amount
            ##ABOVE MEANS U EARN MONEY

    ##items
 ##FLOWERS
    roses = item("Bouquet of Roses", 10)
    tulips = item("Bouquet of Tulips", 10)
    daisies = item("Bouquet of Daisies", 10)

screen money:
    zorder 999
    frame:
        background None xalign 0.1 yalign 0.0
        text "[inventory.money] g" color "#fff"

    
label start:
    $ inventory = Inventory()
    $ inventory.money = inventory.money
label waresandbewares:
    show screen money
    scene shop 
    with Dissolve(0.8)
    play music "dateshop_3.mp3" fadein 1.0 fadeout 1.0
    "You enter a mysterious shop."
    
label morning:


label afternoon:
    "buy something fuck you."
    
label shop:
    "so many wares.."
    menu:
        "Flowers":
            menu:
                "Bouquet of Roses":
                    show roses at center
                    " That'll cost $10."
                    menu:
                        " I'll buy it.":
                            if inventory.buy(roses):
                                " Thanks for your purchase!"
                            else:
                                "Not enough money..."
                        " Eh, nevermind.":
                            jump buy_more
                       
                "Bouquet of Tulips":
                    show tulips at center
                    " That'll cost $10."
                    menu:
                        " I'll buy it.":
                            if inventory.buy(tulips):
                                " Thanks for your purchase!"
                            else:
                                "Not enough money..."
                        " Eh, nevermind.":
                            jump buy_more
                
                "Bouquet of Daisies":
                    show daisies at center
                    " That'll cost $10."
                    menu:
                        " I'll buy it.":
                            if inventory.buy(daisies):
                                " Thanks for your purchase!"
                            else:
                                "Not enough money..."
                        " Eh, nevermind.":
                            jump buy_more
label buy_more:
    scene store
    " Anything else?"
    menu:
        " No":
            " Have a nice day and come back real soon!"
            jump leave
        "Yes.":
            jump shop
label leave:
    "you leave.."

         
return 

User avatar
Milkymalk
Miko-Class Veteran
Posts: 753
Joined: Wed Nov 23, 2011 5:30 pm
Completed: Don't Look (AGS game)
Projects: KANPEKI! ★Perfect Play★
Organization: Crappy White Wings
Location: Germany
Contact:

Re: error while implementing inventory

#6 Post by Milkymalk »

Almost.

Code: Select all

        def __init__(self, money=200):
            self.money = money
            self.items = []
            ##ABOVE IS INITIAL STUFF??
The variable in the def header and the actual self. variable don't have to have the same name, but I like to do that to avoid confusion.
What happens: When you call "inventory = Inventory()", the local variable "money" gets assigned its default 200. __init__ then assigns the value of this variable to "self.money", which is part of the object you created, unlike "money" which will cease to exist as soon as __init__ is finished.
From then on, whenever you want to access the money, use "inventory.money" (or "self.money" if from inside "Inventory").
Last edited by Milkymalk on Mon May 28, 2018 3:49 pm, edited 1 time in total.
Crappy White Wings (currently quite inactive)
Working on: KANPEKI!
(On Hold: New Eden, Imperial Sea, Pure Light)

kivik
Miko-Class Veteran
Posts: 786
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

Re: error while implementing inventory

#7 Post by kivik »

monkeykg wrote: Sat May 26, 2018 10:31 pm
kivik wrote: Sat May 26, 2018 4:15 am Use inventory.money instead of goldcoins to show how much money there is. Your inventory code changes inventory.money, not goldcoin when you buy stuff.

Assigning inventory.money to goldcoin at the start just puts the current amount in the goldcoin variable, it doesn’t update it afterwards.
Ah I'm a bit confused on where to replace this. Do i just get rid of all instances of goldcoins?? I'm pretty sure this is wrong but here's the code anyway
You show your goldcoins in your money screen:

Code: Select all

screen money:
    zorder 999
    frame:
        background None xalign 0.1 yalign 0.0
        text "[goldcoins] g" color "#fff"
But you can replace it with inventory.money

But yes, you should also remove all instances of goldcoins in general because inventory.money is what tracks your money.

User avatar
monkeykg
Regular
Posts: 48
Joined: Mon Jan 01, 2018 6:36 pm
Projects: DATE KNIGHT
Tumblr: monkeywiki
Contact:

Re: error while implementing inventory

#8 Post by monkeykg »

Aahhh it's working now, thank you both so much.

Post Reply

Who is online

Users browsing this forum: No registered users