Ren'Py Shop for item

johannes89

Member
Game Developer
Mar 23, 2018
133
147
Hey guys i hope you can assist me im still fairly new with coding so still learning all the ways to it i need help with a code

what i want to do is my guy have to go to a store to purchase a certain book so i need to get a code for a store where i can have items in it and then i want to make my MC be able to buy a book from store and a radio as well when he get home he will be able to go into his room and read the book so ill have a book on the table if he did not purchase it he will not be able to read the book once he purchased it he will be able to read it same with the radio if he click on the radio after the purchase he will be able to listen to radio if he switch it on if he does not switch it on it will not play and after he is done he will need to switch off the radio (is this anyhow possible)

1. Create a store where Mc will purchase items
2. have a code to read the book in his room (only if purchased)
3. listen to radio (only if purchased and if you switch it on also need to switch it off)

Thank you in advance
Regards
 

TearStar

Developer of Lesbian/Futa Games
Game Developer
Mar 12, 2018
514
1,089
I understand you fairly simply however what am I about to tell you will be not a simple code.

Store:
Could be either done two ways.
Label or screen.
Labels:
Code:
+
Easy to program
Easy to understand
Easy to scale later
-
Needs a "quest" for the item
Screen:
Code:
+
Detailed visual
Doesn't need any "quest"
-
Harder to program
Harder to scale
What I mean by "quest". Labels would use menu blocks but at some point it would be stuffed with a lot of stuff to buy. To avoid that you should first give a "quest" to fetch the item and then you can use it. If it's not quest related at all and there would be a lot that's an issue, will talk about later.

I would choose:
Labels if there are many items you need your protagonist to buy but implement "quest" or "fetch" system for that.
Screens if there a limited amount of things to buy. (I would set the limit at 20 but depends on the "store" asset you use or background).

The coding part:
It's fairly easy actually. I'm going with labels now for simplicity.

Python:
define money = 5000
define fetchShopItem = {
    "bookScifi" : true,
    "wine" : false,
    "radio" : false
}
define invetoryItems = {
    "bookScifi" : false,
    "wine" : false,
    "radio" : false,
    "headset" : false
}
python:
    def buyItem(mon):
        global money
        if money < mon:
            renpy.notify("You don't have enough money!")
        else:
            money -= mon

label shop:
    menu:
        "Buy Sci-Fi Book ($500)" if fetchShopItem["bookScifi"]:
            $ invetoryItems["bookScifi"] = true
            $ fetchShopItem["bookScifi"] = false
            $ buyItem(500)
        "Buy Wine ($200)" if fetchShopItem["wine"]:
            $ invetoryItems["wine"] = true
            $ fetchShopItem["wine"] = false
            $ buyItem(200)
        "Buy Radio ($950)" if fetchShopItem["radio"]:
            $ invetoryItems["radio"] = true
            $ fetchShopItem["radio"] = false
            $ buyItem(950)
        "Buy Headset ($450)" if not invetoryItems["headset"]:
            $ invetoryItems["headset"] = true
            $ buyItem(450)
This is a very simple behaviour. Everything had a fetch quest except for the headset which you could buy if you did not own previously. Radio turn on/off requires a different behaviour if you want to have turn it on/off.


Python:
define radioStatus = false

label room:
    menu:
        "Read Sci-Fi Book" if invetoryItems["bookScifi"]:
            "You read the scifi book."
        "Turn on the radio" if not radioStatus and inventoryItems["radio"]:
            "You turned on the radio"
            $ radioStatus = true
        "Turn off the radio" if radioStatus and inventoryItems["radio"]:
            "You turned off the radio"
            $ radioStatus = off
Could make it a slightly reasonably function make it more understandable but unreasonable addition:

Python:
define radioStatus = false

python:
    def have(item):
        global inventoryItem
        try:
            if inventoryItem[item]:
                return True;
            else:
                return False;
        except:
            return False;

label room:

    menu:

        "Read Sci-Fi Book" if have("bookScifi"):
            "You read the scifi book."
        "Turn on the radio" if not radioStatus and have("radio"):
            "You turned on the radio"
            $ radioStatus = true
        "Turn off the radio" if radioStatus and have("radio"):
            "You turned off the radio"
            $ radioStatus = off
If you have anymore question about anything just ask. This is probably the most simplistic way to do it.
 

Saki_Sliz

Well-Known Member
May 3, 2018
1,403
1,011
I really like programming, I have lots of fun with it. So it bugs me that I don't use renpy because most of the programming questions on these forums are for renpy, so I am absolutely no help, I can't even critique code.
 
  • Like
Reactions: Catapo

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,978
16,235
It all depend of how you effectively want to handle this, and how far you want to go with the objects.

The most basic way is to rely on two sets, one for the inventory, one for the shop ; there's really no need to use an object for this if you intend to have the shop for just few objects.

Python:
# Inventory of the player, empty by default.
default playerInventory = set( [] )
# Inventory of the shop, only the items available from start.
# Tuples values are the name of the item, its cost.
default shopInventory = set( [ ( "book", 15 ) , ( "radio", 30 ) ] )
# Amount of money owned by the player
default money = 0

# State of the radio. Off by default.
default radioState = False


# Few functions to simplify your job later.
init python:
   
    # Return True if the given item is in player inventory, False else.
    def hasItem( item ):
        return item in playerInventory

    # Remove an item from the shop, and add it in player's inventory.
    def buyItem( item, cost ):
        playerInventory.add( item )
        if item in shopInventory is True:
            shopInventory.remove( item )
        store.money -= cost

    # Extend the inventory of the shop
    def addItem( item ):
        shopInventory.add( item )

    # Remove the object from player's inventory.
    def useItem( item ):
        # Security, will works only if the item is in the object.
        if hasItem( item ) is True:
            playerInventory.remove( item )


# The screen to buy something
screen shopInventory:
    vbox:
        xalign 0.5
        yalign 0.5

        # For each item available
        for i, c in shopInventory:
            # Show a button with its name, and its cost
            textbutton "[i]  ([c] money )":
                # Buy it if the player click on the button
                action Function( buyItem, i, c )
                # The button will be sensible only if the player
                # have enough money.
                sensitive money >= c

label whatever:
    [...]
    mc "I'll need a tool for this"
    # The /tool/ is now available in the shop, it cost 10
    $ addItem( ( "tool", 10 ) )
    [...]


label playersRoom:
    menu:
        "Pass time":
            [...]
        # The choice will be available only if the player have the
        # book in his inventory.
        "Read the book" if hasItem( "book" ):
            [...]
        "Use the tool" if hasItem( "tool" ):
            $ useItem( "tool" )
            [...]
        # The choice will be available only if the player have the radio
        # in his inventory, and the radio is actually off.
        "Turn on the Radio" if hasItem( "radio" ) and not radioState:
            $ radioState = True
            [...]
        # The radio can't be On if the player don't have it in is inventory
        # so, the condition can be shortened.
        "Turn off the Radio" if radioState:
            $ radioState = False
            [...]
And that's all, you don't need an object if you effectively intend to only have few objects in your game.
It would complicate your code, and like you needed to ask for help for it, there's real risk that you misunderstood something, and end breaking your game without seeing it.

Edit: Corrected a typo in the code.
 
Last edited:

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,978
16,235
Python:
    "bookScifi" : true,
    "wine" : false,
Warning, you forgot the capital in all this part of the code. It's just a code throw like this, but you can't be sure that the person that will read it know that it should be True and False. Most of the time, they'll end using "true" and "false".


Python:
define radioStatus = false
python:
    def have(item):
        global inventoryItem
        try:
            if inventoryItem[item]:
                return True;
            else:
                return False;
        except:
            return False;
You don't need to use global, the variables of the store are already set as locale to the function. It's only for direct assignation that you can have a problem, and prefixing by store. is enough to solve the problem.
Also, you don't need to have try bloc, and you can directly return the value of the dict's key.
Code:
    def have(item):
        if not item in inventoryItem: return False
        return inventoryItem[item]
And obviously, there's no ; at the end of Python's lines.


Code:
            $ radioStatus = off
off isn't a know value either.
 

johannes89

Member
Game Developer
Mar 23, 2018
133
147
thank you so much for assisting in getting the shop code im getting real close to launch a prologue beta version of my game just need to render few more images thats only thing taking up my time now