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.
Hi! I want to add a system that keeps track of money in the game so that the character can shop/earn coins. How would I do this?
I am visualizing something like:
You have 10 coins remaining. What would you like to buy?
Spaghetti -3
Olives - 4
Chocolate - 11
so that when you buy the spaghetti the system knows you have 7 coins left, etc., and will offer a sample message such as "You don't have enough money for that!" if you try to buy the chocolate.
It should be a system that works for both adding AND subtracting money.
If anyone can direct me to somewhere with info for this or tell me some code, I would really appreciate it! Simplest code is best for me since I'm just starting out.
This is a versatile function to create lists containing arithmetic progressions. It is most often used in for loops. The arguments must be plain integers. If the step argument is omitted, it defaults to 1. If the start argument is omitted, it defaults to 0. The full form returns a list of plain integers [start, start + step, start + 2 * step, ...]. If step is positive, the last element is the largest start + i * step less than stop; if step is negative, the last element is the smallest start + i * step greater than stop. step must not be zero (or else ValueError is raised). Example:
While I can't find the dedicated documentation in the Ren'Py wiki, there it is in the Python one (and you call a Python statement by using a cash sign, $ in front of the coding). But clearly it's used...
$ coins = 0 #You can keep track of coins with a variable.
$ items = [] #You can also keep track of items with a list.
"Today you made 10 coins! Good job!"
$ coins += 10 #By using the += operator, you can add coins.
"You have %(coins)d coins remaining. What would you like to buy?" #%(money)d is what you say if you want to put the number of coins into someone's dialogue.
menu:
"Spaghetti":
$ coins -= 3 #By using the -= operator, you can subtract coins.
$ items.append("spaghetti") #If you have a list named "items", you can add things to the list by saying items.append(_thing_)
"Olives":
$ coins -= 4
$ items.append("olives")
"Chocolate":
$ coins -= 11
$ items.append("chocolate")
if "chocolate" in items: #the phrase "Object" in "list" tells you whether or not the list contains that object.
"You have a bar of chocolate! Yummy!"
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
label start:
python:
inventory = Inventory()
spaghetti = Item("Spaghetti", 3)
olives = Item("Olives", 4)
chocolate = Item("Chocolate", 11)
"Oh look! I found ten coins!"
$ inventory.earn(10)
$ current_money = inventory.money # hack to make the field a global
"Now I have %(current_money)d coins."
"I'll buy some chocolate."
if inventory.buy(chocolate):
"Mmm, chocolate. I'll save that for later"
else:
"Not enough money..."
"Suddenly I feel hungry."
if chocolate in inventory.items:
"Good thing I bought that chocolate earlier."
else:
"If only I had some chocolate..."
P.S. this would be less hackish if Ren'Py would accept object fields for interpolation directly, not just globals.
menu shop1:
"I go into the store."
"Buy spaghetti for three coins.":
$ coins -= 3
"Hey, those are uncooked. I can't eat those yet!"
"Buy olives for four coins.":
$ coins -= 4
"I hate olives."
"And they cost more than the spaghetti."
"But at least I don't have to cook them... "
"Buy chocolate for eleven coins.":
$ coins -= 11
"Mmmm, dark semi-sweet chocolate! My favorite!"
I haven't added it into the Recipe list because I want to break it up and explain the code in the main page instead of the comments and work on adding that menu to Delta's code before I do so.
Don't worry, we can get through it together. I didn't forget about you! I just got overwhelmed. https://cherylitou.wordpress.com
label preshop:
$ spaghetticost = spaghetti.cost
$ olivescost = olives.cost
$ chocolatecost = chocolate.cost
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 shop
label game_continues:
"And so I left the store."
On second thought, I'd also add the following method to the inventory class:
It's a fallthrough; if none of the conditions inside the menu triggers (i.e., you cannot buy anything), there will be no jump and the script just continues.
Apart from that, I would ask you again not to change code you don't understand. You added a return that breaks the script. And I'm not sure if the script runs with that indentation. Also, you added a label shop; you don't need that because menus can take a label identifier. Plus, using the same identifier twice as you did is not allowed and won't even start up. Or... well, maybe it doesn't bail out, I wouldn't know. But it SHOULD, if it doesn't it's a bug.
out of curiosity, one has tried to implement the actual content of wallet/purse ? that is, handling notes & coins & the exchange of these, rest included...