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

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
gensuta
Newbie
Posts: 19
Joined: Sun Apr 17, 2016 2:31 am
Completed: Green Hero, Solar Sprout, Saturday Fright, I Dream of OCs, Rendezvous
Projects: Skate & Date, Blastagirl, Iris, A Slow Ride
Tumblr: gensuta
Deviantart: gensuta
Github: gensuta
Soundcloud: gensuta
itch: gensuta
Contact:

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

#1 Post by gensuta » Fri May 27, 2016 1:30 am

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!!
Last edited by gensuta on Sat May 28, 2016 9:46 am, edited 1 time in total.

User avatar
saguaro
Miko-Class Veteran
Posts: 556
Joined: Sun Feb 12, 2012 9:17 am
Completed: Locked-In, Sunrise, The Censor
Projects: ?
Organization: Lucky Special Games
Tumblr: saguarofoo
itch: saguarofoo
Location: USA
Contact:

Re: Help with making a shop in renpy!!

#2 Post by saguaro » Fri May 27, 2016 6:55 am

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()

User avatar
gensuta
Newbie
Posts: 19
Joined: Sun Apr 17, 2016 2:31 am
Completed: Green Hero, Solar Sprout, Saturday Fright, I Dream of OCs, Rendezvous
Projects: Skate & Date, Blastagirl, Iris, A Slow Ride
Tumblr: gensuta
Deviantart: gensuta
Github: gensuta
Soundcloud: gensuta
itch: gensuta
Contact:

Re: Help with making a shop in renpy!!

#3 Post by gensuta » Fri May 27, 2016 8:36 pm

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

User avatar
saguaro
Miko-Class Veteran
Posts: 556
Joined: Sun Feb 12, 2012 9:17 am
Completed: Locked-In, Sunrise, The Censor
Projects: ?
Organization: Lucky Special Games
Tumblr: saguarofoo
itch: saguarofoo
Location: USA
Contact:

Re: Help with making a shop in renpy!!

#4 Post by saguaro » Fri May 27, 2016 9:59 pm

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

User avatar
gensuta
Newbie
Posts: 19
Joined: Sun Apr 17, 2016 2:31 am
Completed: Green Hero, Solar Sprout, Saturday Fright, I Dream of OCs, Rendezvous
Projects: Skate & Date, Blastagirl, Iris, A Slow Ride
Tumblr: gensuta
Deviantart: gensuta
Github: gensuta
Soundcloud: gensuta
itch: gensuta
Contact:

Re: Help with making a shop in renpy!!

#5 Post by gensuta » Sat May 28, 2016 9:44 am

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!

User avatar
computistxyz
Regular
Posts: 58
Joined: Thu Jul 10, 2014 8:35 am
Github: sjgriffiths
Location: Coventry/Norwich, UK
Contact:

Re: Help with making a shop in renpy!!

#6 Post by computistxyz » Sat May 28, 2016 12:12 pm

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.
computer scientist | programmer | game developer | writer | editor/proofreader

Check out my technical blog, computist.xyz

User avatar
gensuta
Newbie
Posts: 19
Joined: Sun Apr 17, 2016 2:31 am
Completed: Green Hero, Solar Sprout, Saturday Fright, I Dream of OCs, Rendezvous
Projects: Skate & Date, Blastagirl, Iris, A Slow Ride
Tumblr: gensuta
Deviantart: gensuta
Github: gensuta
Soundcloud: gensuta
itch: gensuta
Contact:

Re: Help with making a shop in renpy!!

#7 Post by gensuta » Sat May 28, 2016 6:28 pm

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!

Post Reply

Who is online

Users browsing this forum: Bing [Bot]