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.
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...
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.
.Traditional Chinese Site.
~Illustrator+Graphic/Web Designer~
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:
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...")
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..."
.Traditional Chinese Site.
~Illustrator+Graphic/Web Designer~
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.
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.