Coding Money??

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.
Message
Author
jlavnu

Coding Money??

#1 Post by jlavnu »

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

JinzouTamashii
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??

#2 Post by JinzouTamashii »

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)
    []
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...

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

Dusty
Regular
Posts: 126
Joined: Fri Jul 25, 2008 11:51 pm
Contact:

Re: Coding Money??

#3 Post by Dusty »

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!"

delta
Epitome of Generic
Posts: 525
Joined: Sat Dec 22, 2007 12:59 pm
Projects: yes
Contact:

Re: Coding Money??

#4 Post by delta »

An inventory system is one of the textbook examples for object orientated programming. Off the top of my head, something like this:

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..."
        
P.S. this would be less hackish if Ren'Py would accept object fields for interpolation directly, not just globals.
The rest is left as an exercise for the reader.

JinzouTamashii
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??

#5 Post by JinzouTamashii »

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

delta
Epitome of Generic
Posts: 525
Joined: Sat Dec 22, 2007 12:59 pm
Projects: yes
Contact:

Re: Coding Money??

#6 Post by delta »

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.

JinzouTamashii
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??

#7 Post by JinzouTamashii »

Well, we can cross that bridge when we burn it... :mrgreen:

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

JinzouTamashii
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??

#8 Post by JinzouTamashii »

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?

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!"

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.
Don't worry, we can get through it together. I didn't forget about you! I just got overwhelmed.
https://cherylitou.wordpress.com

delta
Epitome of Generic
Posts: 525
Joined: Sat Dec 22, 2007 12:59 pm
Projects: yes
Contact:

Re: Coding Money??

#9 Post by delta »

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."
On second thought, I'd also add the following method to the inventory class:

Code: Select all

    def has_item(self, item):
            if item in self.items:
                return True
            else:
                return False
So you can do the following:

Code: Select all

    if inventory.has_item(chocolate):
        "Good thing I bought that chocolate earlier."
    else:
        "If only I had some chocolate..."
If you're going to use OOP anyway, it's better to always use object interfaces.
The rest is left as an exercise for the reader.

JinzouTamashii
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??

#10 Post by JinzouTamashii »

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.
Don't worry, we can get through it together. I didn't forget about you! I just got overwhelmed.
https://cherylitou.wordpress.com

JinzouTamashii
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??

#11 Post by JinzouTamashii »

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

Code: Select all

label fallthrough:
"Not enough money..."
jump shop
so how do we get there? Do I need to add it to the buy function?
Don't worry, we can get through it together. I didn't forget about you! I just got overwhelmed.
https://cherylitou.wordpress.com

delta
Epitome of Generic
Posts: 525
Joined: Sat Dec 22, 2007 12:59 pm
Projects: yes
Contact:

Re: Coding Money??

#12 Post by delta »

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.
The rest is left as an exercise for the reader.

JinzouTamashii
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??

#13 Post by JinzouTamashii »

You know what they say...

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

jlavnu

Re: Coding Money??

#14 Post by jlavnu »

Thank you so much you guys. This is really helpful!! :)

dott.Piergiorgio
Veteran
Posts: 345
Joined: Sun May 18, 2008 1:52 pm
Contact:

Re: Coding Money??

#15 Post by dott.Piergiorgio »

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.

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot]