Money and Inventory needs help...(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
shuen
Regular
Posts: 72
Joined: Tue Mar 22, 2011 2:36 pm
Contact:

Money and Inventory needs help...(solved)

#1 Post by shuen »

I copy the Money and Inventory System in the cookbook and try to combine it into my game.
When I run the game, it shows "UnboundLocalError: local variable 'coins' referenced before assignment"
And I have no idea how to fix it...

First I make some default setting in the begining

Code: Select all

label start:
    $ coins = 50
    $ inventory = []
    .
    .
    .
    jump shopping
and in shopping.rpy

Code: Select all

init python:

    class Item:
        def __init__(self, name, cost):
            self.name = name
            self.cost = cost
        
    def buy(item):
         if coins >= item.cost:
              coins -= item.cost
              inventory.append(item) #put item into inventory
              return True
         else:
             return False

    def has_item(item):
         if item in inventory:
              return True
         else:
              return False

label shopping:

    python:
        woodsword = Item("Wood Sword", 3)
        hairbrush = Item("Hair Brush", 4)
        bracelet = Item("Bracelet", 11)

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

    "I want to buy something..."

    jump preshop
    jump shop2

    if has_item(bracelet):
        "Good thing I bought that bracelet earlier."
    else:
        "If only I had a bracelet..."

label preshop:
    $ woodswordcost = woodsword.cost
    $ hairbrushcost = hairbrush.cost
    $ braceletcost = bracelet.cost
    
label shop2:
    menu shop:
        "I go into the store."
        "Buy wood sword for %(woodswordcost)d coins.":
            if buy(woodsword):
                "Funny Stuff~"
                jump game_continues

        "Buy hair brush for %(hairbrushcost)d coins.":
            if buy(hairbrush):
                "I think i'll use it alot..."
                jump game_continues

        "Buy bracelet for %(braceletcost)d coins.":
            if buy(bracelet):
                "OH! Bracelet! My favorite!"
                jump game_continues

        "Buy nothing.":
            return #return to the 

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

label game_continues:
    "And so I left the store."
    "I have %(coins)d left"
    jump shop2
Any help would be appreciate...~>_<~
Last edited by shuen on Sun Apr 03, 2011 8:37 am, edited 5 times in total.
Image
.Traditional Chinese Site.
~Illustrator+Graphic/Web Designer~

User avatar
Aleema
Lemma-Class Veteran
Posts: 2677
Joined: Fri May 23, 2008 2:11 pm
Organization: happyB
Tumblr: happybackwards
Contact:

Re: Money and Inventory needs help...

#2 Post by Aleema »

Maybe it has something to do with your functions (def buy) references coins but it has no idea to know what it is. Either send the coin amount to the function along with the item name, more make coin a global and say:

Code: Select all

    def buy(item):
         global coins
         if coins >= item.cost:
              coins -= item.cost
              inventory.append(item) #put item into inventory
              return True
         else:
             return False

shuen
Regular
Posts: 72
Joined: Tue Mar 22, 2011 2:36 pm
Contact:

Re: Money and Inventory needs help...

#3 Post by shuen »

It works!@o@! Thank you Aleema!!!
Image
.Traditional Chinese Site.
~Illustrator+Graphic/Web Designer~

shuen
Regular
Posts: 72
Joined: Tue Mar 22, 2011 2:36 pm
Contact:

Re: Money and Inventory needs help...(solved)

#4 Post by shuen »

I find out that the $ inventory = [] doesn't store the self value of item
when I go shopping again , and I already have a bracelet in inventory,
It should show "Good thing I bought that bracelet earlier."
but the has_item(item) still got no item result as("If only I had a bracelet...")

Code: Select all

def has_item(item):
         if item in inventory:
              return True
         else:
              return False
*******************************************************
    if has_item(bracelet):
        "Good thing I bought that bracelet earlier."
    else:
        "If only I had a bracelet..."
Image
.Traditional Chinese Site.
~Illustrator+Graphic/Web Designer~

User avatar
Aleema
Lemma-Class Veteran
Posts: 2677
Joined: Fri May 23, 2008 2:11 pm
Organization: happyB
Tumblr: happybackwards
Contact:

Re: Money and Inventory needs help...(new problem...>_<)

#5 Post by Aleema »

Try has_item("Bracelet") ?

If that doesn't work, what is inside your inventory list? Do you know how to use the console with Ren'Py?

shuen
Regular
Posts: 72
Joined: Tue Mar 22, 2011 2:36 pm
Contact:

Re: Money and Inventory needs help...(new problem...>_<)

#6 Post by shuen »

I don't know how to use console
in the inventory, it stores items as <store.item instance at 0x0326C558>
Image
.Traditional Chinese Site.
~Illustrator+Graphic/Web Designer~

User avatar
Aleema
Lemma-Class Veteran
Posts: 2677
Joined: Fri May 23, 2008 2:11 pm
Organization: happyB
Tumblr: happybackwards
Contact:

Re: Money and Inventory needs help...(new problem...>_<)

#7 Post by Aleema »

I should have noticed this before, but it's the same problem as before. You're checking a variable that doesn't exist in the function. In this case, it's inventory. Say "global inventory" like the coins, and it will work (I checked personally).

If that doesn't solve your problem, I'm not sure what to say, because I got your code working in a sample game. If it helps, the placement of your has_item(bracelet) check should be inside of shop2, not shopping as you have posted above.

shuen
Regular
Posts: 72
Joined: Tue Mar 22, 2011 2:36 pm
Contact:

Re: Money and Inventory needs help...(new problem...>_<)

#8 Post by shuen »

hmmm.....I don't know why the "global inventory" doesn't work for me....
but I added a item.code to make it work:

Code: Select all

    class item:
        def __init__(self, code, name, cost):
            self.code = code
            self.name = name
            self.cost = cost
        
    def buy(item):
         global coins
         global inventory
         if coins >= item.cost:
              coins -= item.cost
              inventory.append(item.code)
              return True
         else:
             return False

    def has_item(item):
         if item.code in inventory:
              return True
         else:
              return False

label shopping:
    python:
        woodsword = item("woodsword", "Wood Sword", 3)
        hairbrush = item("hairbrush", "Hair Brush", 4)
        bracelet = item("bracelet", "Bracelet", 11)
Thanks for your help!!
Last edited by shuen on Sun Apr 03, 2011 8:44 am, edited 2 times in total.
Image
.Traditional Chinese Site.
~Illustrator+Graphic/Web Designer~

User avatar
Aleema
Lemma-Class Veteran
Posts: 2677
Joined: Fri May 23, 2008 2:11 pm
Organization: happyB
Tumblr: happybackwards
Contact:

Re: Money and Inventory needs help...(new problem...>_<)

#9 Post by Aleema »

Glad it works now, though!

tpgames
Regular
Posts: 53
Joined: Wed Jun 16, 2010 11:29 pm
Projects: Jade Universität
Contact:

Re: Money and Inventory needs help...(solved)

#10 Post by tpgames »

Thanks for posting all of this! This should help me with the other confusion I was having. I get to write this type of code right after I get the functionality of simply selecting an item working. (No questions yet, I'm still taking time to reread everything and process it.)
Kyteroo jaunting off....(won't add website until I've finished 1 Ren'Py game)
WIP: Jade Universität - A RPG puzzle game.

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot], Ocelot