Page 1 of 1

Help with making a shop in renpy!![SOLVED]

Posted: Fri May 27, 2016 1:30 am
by gensuta
Hiya again,

I have a feeling I'll be posting a lot in here because I'm fairly new to the whole coding thing, but I've been trying my best to make things work. I've been trying to solve this for several hours but had no luck whatsoever! All I'm trying to do is make a scene for when you go buy things. However, when I first did it on my own without what I added now, if the player had $0 they could still buy an object even if i put that the money had to be >= 10, for instance.

This would just make the money go down in the negatives. So I tried using the renpy cookbook and other things for help. And this happened. I'm not sure how to fix it. I apologize for the mess I'm going to put below since I was trying to figure out a way to make things work.


Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/script.rpy", line 546, in script call
    call Shop
  File "game/Daily_Things.rpy", line 135, in script
    if inventory.buy(roses):
  File "game/Daily_Things.rpy", line 135, in <module>
    if inventory.buy(roses):
NameError: name 'inventory' is not defined

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

Full traceback:
  File "game/script.rpy", line 546, in script call
    call Shop
  File "game/Daily_Things.rpy", line 135, in script
    if inventory.buy(roses):
  File "C:\Users\Gen\Desktop\Super cool game stuff\renpy-6.99.8-sdk\renpy\ast.py", line 1648, in execute
    if renpy.python.py_eval(condition):
  File "C:\Users\Gen\Desktop\Super cool game stuff\renpy-6.99.8-sdk\renpy\python.py", line 1606, in py_eval
    return py_eval_bytecode(code, globals, locals)
  File "C:\Users\Gen\Desktop\Super cool game stuff\renpy-6.99.8-sdk\renpy\python.py", line 1601, in py_eval_bytecode
    return eval(bytecode, globals, locals)
  File "game/Daily_Things.rpy", line 135, in <module>
    if inventory.buy(roses):
NameError: name 'inventory' is not defined

Windows-8-6.2.9200
Ren'Py 6.99.10.1227
Lady Killers 0.0

Code: Select all

## Insert Shop Details here
python:

        ##FLOWERS
        roses = item("Bouquet of Roses", 10)
        tulips = item("Bouquet of Tulips", 10)
        daisies = item("Bouquet of Daisies", 10)
label Shop:
    scene store
play music storesong
$ showmoney = True
shop " Hi, what would you like to buy today?"


label buy:
    $ inventory = Inventory()
        
    scene store
    menu:
        "Flowers":
            menu:
                "Bouquet of Roses":
                    show roses at center
                    shop " 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
                    shop " 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
                    shop " 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
shop " Anything else?"
menu:
    " No":
        shop " Have a nice day and come back real soon!"
        jump home
    "Yes.":
        jump buy
        

Code: Select all

#############################
init:
    $ TIME = 0
    $ items = []
    $ Day = 1
    $ money = 10
            
######################STUFF FOR SHOP!!
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 = []
            ##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
            
            

Thank you so much in advance!!

Re: Help with making a shop in renpy!!

Posted: Fri May 27, 2016 6:55 am
by saguaro
The error indicates the object "inventory" has not been defined, however in the example you posted it is defined in the shop label? Make sure you are creating that object in the code that's throwing the error.

Also, you have some variables in an init block:

Code: Select all

init:
    $ TIME = 0
    $ items = []
    $ Day = 1
    $ money = 10
money and items are created by the Inventory class and changed by that class's functions. So inventory.money is the variable that's being decreased, and inventory.items is where the item list is being stored when you use inventory.buy()

Re: Help with making a shop in renpy!!

Posted: Fri May 27, 2016 8:36 pm
by gensuta
saguaro wrote:The error indicates the object "inventory" has not been defined, however in the example you posted it is defined in the shop label? Make sure you are creating that object in the code that's throwing the error.

Also, you have some variables in an init block:

Code: Select all

init:
    $ TIME = 0
    $ items = []
    $ Day = 1
    $ money = 10
money and items are created by the Inventory class and changed by that class's functions. So inventory.money is the variable that's being decreased, and inventory.items is where the item list is being stored when you use inventory.buy()

Thanks for the advice! I tried messing around with it a bit more so everything is in that one label and so that money and items aren't in that init block. Below is what I have in that label now.

Code: Select all




label Shop:
    
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
            ##ABOVE IS FOR BUYING STUFF

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

###########################################################


    
## Insert Shop Details here
python:
    
        inventory = Inventory()
        ##FLOWERS
        roses = item("Bouquet of Roses", 10)
        tulips = item("Bouquet of Tulips", 10)
        daisies = item("Bouquet of Daisies", 10)
    
    
    
scene store
play music storesong
shop " Hi, what would you like to buy today?"


label buy:
        
    scene store
    menu:
        "Flowers":
            menu:
                "Bouquet of Roses":
                    show roses at center
                    shop " 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
                    shop " 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
                    shop " 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

 " Go home":
            jump home
        
        
label buy_more:
    scene store

shop " Anything else?"
menu:
    " No":
        shop " Have a nice day and come back real soon!"
        jump home
    "Yes.":
        jump buy
 






But I keep getting the same error from before about the inventory :(

Re: Help with making a shop in renpy!!

Posted: Fri May 27, 2016 9:59 pm
by saguaro
I should have explained better. The indentation in your original code is not consistent, but that code technically works. I had no trouble running it. 'NameError: name 'inventory' is not defined' means when Ren'Py tried to run the inventory.buy function, it could not find an object called 'inventory'. However, in the original code you posted you have indeed created that object:

Code: Select all

label buy:
    $ inventory = Inventory()
I am not able to replicate your error, which is why I asked if this object exists in the script that is currently throwing the error.

Regardless, putting the inventory object in the buy label means it will be overwritten each time the player buys something. You want to create the inventory object after the start label.

Regarding init blocks, keep in mind these blocks run whenever the game is initialized. You will only want to store data that does not change. I assume you do not want to reset the Day variable. If not, you would want to define it after the start label as well.
https://www.renpy.org/doc/html/quickstart.html#init

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, money=10):
            self.money = 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

    ##FLOWERS
    roses = item("Bouquet of Roses", 10)
    tulips = item("Bouquet of Tulips", 10)
    daisies = item("Bouquet of Daisies", 10)
       
label start:
    $ TIME = 0
    $ Day = 1   
    $ inventory = Inventory()
    call Shop   
        
label Shop:
    scene store
    play music storesong
    $ showmoney = True
    shop " Hi, what would you like to buy today?"

label buy:    
    scene store
    menu:
        "Flowers":
            menu:
                "Bouquet of Roses":
                    show roses at center
                    shop " 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
                    shop " 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
                    shop " 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
    shop " Anything else?"
    menu:
        " No":
            shop " Have a nice day and come back real soon!"
            jump home
        "Yes.":
            jump buy

Re: Help with making a shop in renpy!!

Posted: Sat May 28, 2016 9:44 am
by gensuta
saguaro wrote:I should have explained better. The indentation in your original code is not consistent, but that code technically works. I had no trouble running it. 'NameError: name 'inventory' is not defined' means when Ren'Py tried to run the inventory.buy function, it could not find an object called 'inventory'. However, in the original code you posted you have indeed created that object:

Code: Select all

label buy:
    $ inventory = Inventory()
I am not able to replicate your error, which is why I asked if this object exists in the script that is currently throwing the error.

Regardless, putting the inventory object in the buy label means it will be overwritten each time the player buys something. You want to create the inventory object after the start label.

Regarding init blocks, keep in mind these blocks run whenever the game is initialized. You will only want to store data that does not change. I assume you do not want to reset the Day variable. If not, you would want to define it after the start label as well.
https://www.renpy.org/doc/html/quickstart.html#init

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, money=10):
            self.money = 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

    ##FLOWERS
    roses = item("Bouquet of Roses", 10)
    tulips = item("Bouquet of Tulips", 10)
    daisies = item("Bouquet of Daisies", 10)
       
label start:
    $ TIME = 0
    $ Day = 1   
    $ inventory = Inventory()
    call Shop   
        
label Shop:
    scene store
    play music storesong
    $ showmoney = True
    shop " Hi, what would you like to buy today?"

label buy:    
    scene store
    menu:
        "Flowers":
            menu:
                "Bouquet of Roses":
                    show roses at center
                    shop " 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
                    shop " 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
                    shop " 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
    shop " Anything else?"
    menu:
        " No":
            shop " Have a nice day and come back real soon!"
            jump home
        "Yes.":
            jump buy
Ah it worked!! Thank you so much for the help and for explaining because I didn't know much about the init block nor did I realize the indentation wasn't constant. I was just trying out different things I saw from different tutorials, but now I understand this all lot better. Thanks again!

Re: Help with making a shop in renpy!!

Posted: Sat May 28, 2016 12:12 pm
by computistxyz
gensuta wrote:Ah it worked!! Thank you so much for the help and for explaining because I didn't know much about the init block nor did I realize the indentation wasn't constant. I was just trying out different things I saw from different tutorials, but now I understand this all lot better. Thanks again!
As a bit of background advice, Python parses its code through indentation - 4 space character (a tab, effectively) define a block of text as introduced by a colon, in comparison to other languages which ignore whitespace and defines blocks through, for example, curly braces. So, it's extremely important to code integrity that the indentation is kept consistent and correct.

Also, the inventory system is object-oriented. The inventory=Inventory() statement is instantiating an Inventory object and assigning it to the reference 'inventory' - you only do this once to initialise a given inventory. The beauty of this is that you can, for instance, create more than one Inventory object, say for different player characters.

Re: Help with making a shop in renpy!!

Posted: Sat May 28, 2016 6:28 pm
by gensuta
TARDISam24 wrote:
gensuta wrote:Ah it worked!! Thank you so much for the help and for explaining because I didn't know much about the init block nor did I realize the indentation wasn't constant. I was just trying out different things I saw from different tutorials, but now I understand this all lot better. Thanks again!
As a bit of background advice, Python parses its code through indentation - 4 space character (a tab, effectively) define a block of text as introduced by a colon, in comparison to other languages which ignore whitespace and defines blocks through, for example, curly braces. So, it's extremely important to code integrity that the indentation is kept consistent and correct.

Also, the inventory system is object-oriented. The inventory=Inventory() statement is instantiating an Inventory object and assigning it to the reference 'inventory' - you only do this once to initialise a given inventory. The beauty of this is that you can, for instance, create more than one Inventory object, say for different player characters.

Ah okay, I'll make note of it so I don't do it again and also I didn't know that I could create more than one inventory. As of yet I don't need to have more than one with the project I'm working on, but I'm pretty sure I will in the future. Thanks for the advice!