[SOLVED] Help with conditional statement

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.
Post Reply
Message
Author
Andwiiger
Newbie
Posts: 14
Joined: Thu Oct 26, 2017 12:35 pm
Contact:

[SOLVED] Help with conditional statement

#1 Post by Andwiiger »

I have a piece of code that runs but it doesn't print what happens when the condition is false.
This is the text I can't get, it should come if self.money is lower than item.cost but it never shows.

Code: Select all

def buy(self, item):
            if (self.money >= item.cost):
                self.items.append(item)
                self.money -= item.cost
            else:
                "You can't afford [item.name] it costs [item.cost]$. You have [self.money]$"
It's part of this code:

Code: Select all

init -2 python:
    import renpy.store as store
    import renpy.exports as renpy

    inventory_page = 0
    item = None

    class Item(store.object):
        def __init__(self, name, cost=0):
            self.name = name
            self.cost = cost

        def use(self):
            inventory.drop(self)

    class Inventory(store.object):
        def __init__(self, money=0):
            self.money = money
            self.items = []

        def add(self, item):
            self.items.append(item)

        def drop(self, item):
            self.items.remove(item)

        def buy(self, item):
            if (self.money >= item.cost):
                self.items.append(item)
                self.money -= item.cost
            else:
                "You can't afford [item.name] it costs [item.cost]$. You have [self.money]$"


        def add_money(self, amount):
            self.money += amount
Then I set the cost of the items:

Code: Select all

chocolate = Item("Chocolate", 5)
        wine = Item("Wine", 30) 
And when I call the buy function from inventory it doesn't show the text I want, keep in mind that inventory.money = 0 so it should return false and thus return the text:

Code: Select all

$ inventory.buy(chocolate)
         $ inventory.buy(wine) 
Last edited by Andwiiger on Sat May 26, 2018 5:42 pm, edited 1 time in total.

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Help with conditional statement

#2 Post by Remix »

While inside a python block you have to use python, not Ren'Py... so:

Code: Select all

            else:
                renpy.say("", "You can't afford ...")
Frameworks & Scriptlets:

Andwiiger
Newbie
Posts: 14
Joined: Thu Oct 26, 2017 12:35 pm
Contact:

Re: Help with conditional statement

#3 Post by Andwiiger »

Ah that makes sense. Thanks alot.
Do you happen to know how I can print the item name and cost with renpy.say? It gives me "AttributeError: 'NoneType' object has no attribute name" and "AttributeError:'NoneType object has no attribute cost".
I tried to remove it and just use this instead:

Code: Select all

 renpy.say("", "You can't afford [item]. You have [inventory.money]$") 
but I end up with "You cant afford None. You have 0$"

kivik
Miko-Class Veteran
Posts: 786
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

Re: Help with conditional statement

#4 Post by kivik »

I think the issue is the scope of the variable. If I'm not mistaken:

item only exists in the scope of the function. renpy.say is just another function, which then interpolates the string - it does that by grabbing variables from the global store - but item isn't a global variable, so you get that error.

Try this instead:

Code: Select all

renpy.say("", "You can't afford %s. You have %s$" % (item.name, inventory.money)

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Ocelot