Coding Money??
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.
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.
Coding Money??
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.
Thank you!
Thanks
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.
Thank you!
Thanks
-
- Eileen-Class Veteran
- Posts: 1686
- Joined: Mon Sep 21, 2009 8:03 pm
- Projects: E-mail me if you wanna rock the planet
- Location: USA
- Contact:
Re: Coding Money??
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...range([start], stop[, step])¶
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:
Code: Select all
>>> range(10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> range(1, 11) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> range(0, 30, 5) [0, 5, 10, 15, 20, 25] >>> range(0, 10, 3) [0, 3, 6, 9] >>> range(0, -10, -1) [0, -1, -2, -3, -4, -5, -6, -7, -8, -9] >>> range(0) [] >>> range(1, 0) []
http://www.renpy.org/w/index.php?title= ... ange&go=Go
Don't worry, we can get through it together. I didn't forget about you! I just got overwhelmed.
https://cherylitou.wordpress.com
https://cherylitou.wordpress.com
Re: Coding Money??
Code: Select all
$ 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!"
Re: Coding Money??
An inventory system is one of the textbook examples for object orientated programming. Off the top of my head, something like this:
P.S. this would be less hackish if Ren'Py would accept object fields for interpolation directly, not just globals.
Code: Select all
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..."
The rest is left as an exercise for the reader.
-
- Eileen-Class Veteran
- Posts: 1686
- Joined: Mon Sep 21, 2009 8:03 pm
- Projects: E-mail me if you wanna rock the planet
- Location: USA
- Contact:
Re: Coding Money??
Mind if I put those two up in a Wiki cookbook, guys?
Don't worry, we can get through it together. I didn't forget about you! I just got overwhelmed.
https://cherylitou.wordpress.com
https://cherylitou.wordpress.com
Re: Coding Money??
I don't. Then again, I don't give any guarantee that the above actually works in all circumstances.
The rest is left as an exercise for the reader.
-
- Eileen-Class Veteran
- Posts: 1686
- Joined: Mon Sep 21, 2009 8:03 pm
- Projects: E-mail me if you wanna rock the planet
- Location: USA
- Contact:
Re: Coding Money??
Well, we can cross that bridge when we burn it...
I'll try to annotate some of the code with comments. Is there some way to save a draft of a page on the Wiki without submitting it or finalizing it?
I'll try to annotate some of the code with comments. Is there some way to save a draft of a page on the Wiki without submitting it or finalizing it?
Don't worry, we can get through it together. I didn't forget about you! I just got overwhelmed.
https://cherylitou.wordpress.com
https://cherylitou.wordpress.com
-
- Eileen-Class Veteran
- Posts: 1686
- Joined: Mon Sep 21, 2009 8:03 pm
- Projects: E-mail me if you wanna rock the planet
- Location: USA
- Contact:
Re: Coding Money??
Hey, delta, I'm trying to make a menu for yours but do I declare $ items.append("olives") and $ inventory.buy(olives) at the same time?
You can also see the partial page here:
http://www.renpy.org/wiki/renpy/doc/coo ... ney_System
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.
Code: Select all
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!"
http://www.renpy.org/wiki/renpy/doc/coo ... ney_System
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
https://cherylitou.wordpress.com
Re: Coding Money??
Code: Select all
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."
Code: Select all
def has_item(self, item):
if item in self.items:
return True
else:
return False
Code: Select all
if inventory.has_item(chocolate):
"Good thing I bought that chocolate earlier."
else:
"If only I had some chocolate..."
The rest is left as an exercise for the reader.
-
- Eileen-Class Veteran
- Posts: 1686
- Joined: Mon Sep 21, 2009 8:03 pm
- Projects: E-mail me if you wanna rock the planet
- Location: USA
- Contact:
Re: Coding Money??
I agree, but I wanted the Cookbook Recipe to "stand alone" as an example, as it were. It's up to the user to modify it to their purposes.
Thank you for the expanded code, I'll work on it later to update it.
Thank you for the expanded code, I'll work on it later to update it.
Don't worry, we can get through it together. I didn't forget about you! I just got overwhelmed.
https://cherylitou.wordpress.com
https://cherylitou.wordpress.com
-
- Eileen-Class Veteran
- Posts: 1686
- Joined: Mon Sep 21, 2009 8:03 pm
- Projects: E-mail me if you wanna rock the planet
- Location: USA
- Contact:
Re: Coding Money??
I finalized the Inventory and Money Systems page late this morning and added it to the Cookbook.
http://www.renpy.org/wiki/renpy/doc/coo ... cellaneous
http://www.renpy.org/wiki/renpy/doc/coo ... ney_System
The only weird thing would be that nothing actually calls
so how do we get there? Do I need to add it to the buy function?
http://www.renpy.org/wiki/renpy/doc/coo ... cellaneous
http://www.renpy.org/wiki/renpy/doc/coo ... ney_System
The only weird thing would be that nothing actually calls
Code: Select all
label fallthrough:
"Not enough money..."
jump shop
Don't worry, we can get through it together. I didn't forget about you! I just got overwhelmed.
https://cherylitou.wordpress.com
https://cherylitou.wordpress.com
Re: Coding Money??
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.
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.
The rest is left as an exercise for the reader.
-
- Eileen-Class Veteran
- Posts: 1686
- Joined: Mon Sep 21, 2009 8:03 pm
- Projects: E-mail me if you wanna rock the planet
- Location: USA
- Contact:
Re: Coding Money??
You know what they say...
If you want it done right...
If you want it done right...
Don't worry, we can get through it together. I didn't forget about you! I just got overwhelmed.
https://cherylitou.wordpress.com
https://cherylitou.wordpress.com
-
- Veteran
- Posts: 345
- Joined: Sun May 18, 2008 1:52 pm
- Contact:
Re: Coding Money??
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...
Best regards from Italy,
Dott. Piergiorgio.
Best regards from Italy,
Dott. Piergiorgio.
Who is online
Users browsing this forum: Bing [Bot], Google [Bot]