money & 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
saskuto
Regular
Posts: 64
Joined: Fri Jan 15, 2010 2:56 pm
Contact:

money & inventory

#1 Post by saskuto »

Ok so I'm an official noob when it comes to inventory and money systems, so i tried copying the cookbooks source code into a test renpy game (don't want to mess up my actual game ^^)

i've got this:

Code: Select all

# You can place the script of your game in this file.

python:
    inventory = Inventory()
    spaghetti = Item("Spaghetti", 3)
    olives = Item("Olives", 4)
    chocolate = Item("Chocolate", 11)

init python:
    
    class Item:
        def __init__(self, name, cost):
            self.name = name
            self.cost = cost

    class inventory:
        def __init__(self, money=10):
            self.money = money
            self.items = []

        def buy(self, item):
            if self.money >= item.cost:
                self.money -= item.cost
                self.items.append(item)
                return True
            else:
                return False

        def earn(self, amount):
            self.money += amount

        def has_item(self, item):
                if item in self.items:
                    return True
                else:
                    return False
    
init:
    
    # Declare images below this line, using the image statement.
    # eg. image eileen happy = "eileen_happy.png"

    # Declare characters used by this game.
    $ e = Character('Eileen', color="#c8ffc8")


# The game starts here.
    
label start:

    "Oh, look! I found ten coins!"

    $ inventory.earn(10)

    $ current_money = inventory.money

    "Now I have %(current_money)d coins."

    "My stomach growls loudly."

    if inventory.buy(chocolate):
        "Mmm, chocolate. I'll save that for later... "
    else:
       "Not enough money... "

    "Suddenly, I feel hungry."

    jump preshop
    jump shop2

    if inventory.has_item(chocolate):
        "Good thing I bought that chocolate earlier."
    else:
        "If only I had some chocolate..."

label preshop:
$ spaghetticost = spaghetti.cost
$ olivescost = olives.cost
$ chocolatecost = chocolate.cost

label shop2:
menu shop:

    "I go into the store."

    "Buy spaghetti for %(spaghetticost)d coins.":
        if inventory.buy(spaghetti):
            "Hey, those are uncooked. I can't eat those yet!"
            jump game_continues

    "Buy olives for %(olivescost)d coins.":
         if inventory.buy(olives):
            "I hate olives."
            "And they cost more than the spaghetti."
            "But at least I don't have to cook them... "
            jump game_continues

    "Buy chocolate for %(chocolatecost)d coins.":
         if inventory.buy(chocolate):
            "Mmmm, dark semi-sweet chocolate! My favorite!"
            jump game_continues

    "Buy nothing.":
        jump game_continues

label fallthrough:
    "Not enough money..."
jump shop2

label game_continues:
    "And so I left the store."
    
and i keep getting this error:

Code: Select all

I'm sorry, but an exception occured while executing your Ren'Py
script.

TypeError: unbound method earn() must be called with inventory instance as first argument (got int instance instead)

While running game code:
 - script at line 54 of L:\Visual Novels\software\renpy-6.10.0\test/game/script.rpy
 - python at line 54 of L:\Visual Novels\software\renpy-6.10.0\test/game/script.rpy.

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

  File "L:\Anime\Visual Novels\software\renpy-6.10.0\renpy\bootstrap.py", line 260, in bootstrap
  File "L:\Anime\Visual Novels\software\renpy-6.10.0\renpy\main.py", line 308, in main
  File "L:\Anime\Visual Novels\software\renpy-6.10.0\renpy\main.py", line 92, in run
  File "L:\Anime\Visual Novels\software\renpy-6.10.0\renpy\execution.py", line 230, in run
  File "renpy-6.10.0/renpy/ast.py", line 557, in execute
  File "L:\Anime\Visual Novels\software\renpy-6.10.0\renpy\python.py", line 927, in py_exec_bytecode
  File "L:\Visual Novels\software\renpy-6.10.0\test/game/script.rpy", line 54, in <module>
TypeError: unbound method earn() must be called with inventory instance as first argument (got int instance instead)

While running game code:
 - script at line 54 of L:\Visual Novels\software\renpy-6.10.0\test/game/script.rpy
 - python at line 54 of L:\Visual Novels\software\renpy-6.10.0\test/game/script.rpy.

Ren'Py Version: Ren'Py 6.10.0e
I dont really have a clue as to how to get it to work, any help will be much appreciated :)

chronoluminaire
Eileen-Class Veteran
Posts: 1153
Joined: Mon Jul 07, 2003 4:57 pm
Completed: Elven Relations, Cloud Fairy, When I Rule The World
Tumblr: alextfish
Skype: alextfish
Location: Cambridge, UK
Contact:

Re: money & inventory

#2 Post by chronoluminaire »

There are two problems: 1) is that you're defining the object up in a python block that's too early. I think it'll never actually get called. 2) is that you've defined the inventory class with a lower-case "i", so calling "inventory.earn(...)" is trying to call the method on the whole class (like a static method).

Make the following changes.
1) Move the definition of inventory, spaghetti, olives and chocolate down into a python block that follows the "label start:".

2) Change the capitalisation of "def inventory" to "def Inventory". The one with a capital I is the class type; the one with lower-case i is your instance of it. To make it clearer, I suggest you also replace all the mentions of "inventory" with a lower-case i to "my_inventory".

So your script after "label start:" would now start something like:

Code: Select all

label start:
  python:
    my_inventory = Inventory()
    spaghetti = Item("Spaghetti", 3)
    olives = Item("Olives", 4)
    chocolate = Item("Chocolate", 11)
  "Oh, look! I found ten coins!"
  $ my_inventory.earn(10)
Two bonus suggestions:
3) Move the code block starting "if inventory.has_item(chocolate)" to after "label game_continues:" because it can never be called at the moment.

4) I hope you edited that traceback slightly. Because if you didn't, it looks like Ren'Py is rather confused between whether it lives in L:\Visual Novels\ or L:\Anime\Visual Novels\. I don't know what kind of problems you might get if your installation is split between two copies like that which might have subtle differences, but it probably won't be good...
I released 3 VNs, many moons ago: Elven Relations (IntRenAiMo 2007), When I Rule The World (NaNoRenO 2005), and Cloud Fairy (the Cute Light & Fluffy Project, 2009).
More recently I designed the board game Steam Works (published in 2015), available from a local gaming store near you!

Jake
Support Hero
Posts: 3826
Joined: Sat Jun 17, 2006 7:28 pm
Contact:

Re: money & inventory

#3 Post by Jake »

chronoluminaire wrote: 4) I hope you edited that traceback slightly. Because if you didn't, it looks like Ren'Py is rather confused between whether it lives in L:\Visual Novels\ or L:\Anime\Visual Novels\. I don't know what kind of problems you might get if your installation is split between two copies like that which might have subtle differences, but it probably won't be good...
I don't know how long the feature's been in Ren'Py exactly, but recent versions allow you to have a 'projects directory' which is separate from your Ren'Py directory, and this is what it (quite normally) looks like in your traceback - all the files in the stack which are part of the Ren'Py distribution are shown as originating in the Ren'Py directory, and all the files which are part of your game script are shown as originating in the game directory. Which seems to be what's happening there. (That said, the directories installed into look a bit odd, to my eye... but that's down to user preference.)
Server error: user 'Jake' not found

saskuto
Regular
Posts: 64
Joined: Fri Jan 15, 2010 2:56 pm
Contact:

Re: money & inventory

#4 Post by saskuto »

Thanks so much! I think I have a better understanding of it now :)
And i have sorted the traceback issue (i think lol)

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot]