Ren'Py Class problems

CrimsonAxisStudio

Member
Game Developer
Nov 7, 2017
271
1,585
Having some issues with earning money with classes.

Here's the code.

Code:
init python:
    class Item:
        def __init__(self, name, cost):
            self.name = name
            self.cost = cost

    class Inventory:
        def __init__(self, money=0):
            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

        def has_item(self, item):
                if item in self.items:
                    return True
                else:
                    return False
Trying to use the earn to add money and spend it.

Code:
c "Ok. Here's $200. There's a store nearby."
$ earn.money(200)
Getting "earn money is not defined.". Not quite sure what to do, because it seems to be defined in the class, unless I'm wrong about something. I've looked at a number of tutorials online and they have things set up this way. It's possible that these tutorials are out of date and that's the issue.

Any help would be appreciated.

Thanks!
 

CrimsonAxisStudio

Member
Game Developer
Nov 7, 2017
271
1,585
Ahh, I already had an inventory defined. It was inventory.earn vs earn.money that was causing the problem.

Now, is it possible to reference the money value for a text button or for a say statement? So the player knows how much money they have, etc.
 

Rich

Old Fart
Modder
Donor
Respected User
Game Developer
Jun 25, 2017
2,566
7,382
Probably something like

text "[myInventory.money]"
Yes, that generally works.

There are, in general, three options:
  • What you listed, where you use the Ren'py interpolation feature. This works for simple variables and object.property references.

  • If all you want is the value, you can frequently just do
Code:
text my_string_variable
in other words, you don't HAVE to put a variable interpolated inside a string - Ren'py can evaluate the variable name.

  • Sometimes, the expression that you want to show confuses Ren'py's interpolation. In that case, you can use Python string formatting:
Code:
text "Welcome to my {}".format(get_the_value(27))
 
  • Like
Reactions: CrimsonAxisStudio

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,967
16,215
Code:
   class Inventory:
[...}
        def earn(self, amount):
[...]
Code:
$ earn.money(200)
What is this earn object that have a money method ? There's absolutely nothing in the code you gave as example that can relate to this...

You meant :
Code:
default inventory = Inventory()
[...]
label whatever:
    $ inventory.earn( 200 )
 
  • Like
Reactions: CrimsonAxisStudio