Weirdest issue with Class and loop

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
eggtart_prince
Newbie
Posts: 7
Joined: Mon Apr 23, 2018 5:56 am
Contact:

Weirdest issue with Class and loop

#1 Post by eggtart_prince »

I'm trying to create an inventory and store in my game. In my loop through my inventory, it's producing items even when there is no items in it.

Here are my classes:

Code: Select all

init python:
    class Inventory():
        def __init__(self):
            self.inventory = []

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

        def use(self, item):
            self.inventory.remove(item)

        def check(self, item):
            for i in self.inventory:
                if i == item:
                    return True

        def list(self):
            return self.inventory

    class Store():
        def __init__(self):
            self.inv = {}

        def add(self, name, price=0):
            self.inv[name] = price

        def sold(self, name):
            del self.inv[name]

        def stock(self):
            return self.inv
And my declarations:

Code: Select all

define inv = Inventory()
define sto = Store()

label start:
	sto.add('Sword')
Then, I have a screen called store and in it I have a loop that loops through sto.stock(). It shows Sword, which is correct. But when I do the same in my inventory, it's showing 5 entries of Sword when I haven't added it to the inventory.

Here is my loop

Code: Select all

# in screen store, it lists the items and a buy button beside it
hbox:
	for i in sto.stock():
		text i:
			yanchor -0.25
		textbutton 'Buy':
			xpos 0.95
			action inv.add(i)
	
# in screen inventory (basically the same)
for i in inv.list():
	text i
And the weird thing is, when I progress with my story by clicking through the dialogue, another 4 or 5 Sword is appended to the inventory screen. Anyone know why?

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

Re: Weirdest issue with Class and loop

#2 Post by kivik »

There're a few things going on that you need to fix in your code, although the key answer is here:

viewtopic.php?p=319388#p319388

Edit: ignore the above, DannX's answer is far simpler. I think the above thread may have been before Function() was introduced to Ren'py

Basically every time your store screen is called, it's executing inv.add(i), but the thread reply above goes through now to actually do it.

You also have a few problems in your code:

- Don't define inv and sto, use "default" instead. "define" is for static variables that doesn't change throughout the game, "default" are for variables that will change. If I'm not mistaken, what that means is your inv and sto variables won't be saved when your player saves the game.
- Your code for def check(self,item) is redundant. In python you can do "return item in self.inventory"
- More of a future proofing, but you probably want to create an item class, and physically put the items inside the store and inventory. This way you can have things like item.description, item.price, item.effect etc. You'll have to rewrite the classes to manage this though.
Last edited by kivik on Mon Apr 23, 2018 9:21 am, edited 1 time in total.

DannX
Regular
Posts: 99
Joined: Mon Mar 12, 2018 11:15 am
Contact:

Re: Weirdest issue with Class and loop

#3 Post by DannX »

Adding on to what kivik explained, if you use define ren'py will also save the variable, but I remember reading somewhere that PyTom recommended using default for variables that change regularly and define for the rest, so it's considered best practice.

For your inventory buttons, you'll probably want to use Function screen action when you need to call a function from a screen:

Code: Select all

textbutton 'Buy':
                xpos 0.95
                action Function(inv.add, i)

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

Re: Weirdest issue with Class and loop

#4 Post by kivik »

DannX wrote: Mon Apr 23, 2018 9:08 am Adding on to what kivik explained, if you use define ren'py will also save the variable, but I remember reading somewhere that PyTom recommended using default for variables that change regularly and define for the rest, so it's considered best practice.

For your inventory buttons, you'll probably want to use Function screen action when you need to call a function from a screen:

Code: Select all

textbutton 'Buy':
                xpos 0.95
                action Function(inv.add, i)

Oh man, I tried doing that but I left the brackets in (Function(inv.add(),i) and couldn't get it working, thought that other thread would be the solution. Good answer!

DannX
Regular
Posts: 99
Joined: Mon Mar 12, 2018 11:15 am
Contact:

Re: Weirdest issue with Class and loop

#5 Post by DannX »

kivik wrote: Mon Apr 23, 2018 9:19 am Oh man, I tried doing that but I left the brackets in (Function(inv.add(),i) and couldn't get it working, thought that other thread would be the solution. Good answer!
Yeah, it took me a bit to fully figure out how to use it. I wonder why it doesn't have an example in the documentation, since it's probably one of the most useful actions out there, and not using it almost always leads to this kind of problem.
Last edited by DannX on Mon Apr 23, 2018 9:32 am, edited 2 times in total.

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

Re: Weirdest issue with Class and loop

#6 Post by kivik »

DannX wrote: Mon Apr 23, 2018 9:29 am Yeah, it took me a bit to fully figure out how to use it. I wonder why it doesn't have an example in the documentation, since it's probably one of the most useful actions out there, and not using it almost always leads to this kind of problem.
I'm guessing PyTom just needs some time to redo the documentations since the changes. He's clearly not making a living from Ren'py so I can understand. Maybe a few key people should be given permission to extend the documentations and add the change-log examples to the documentations?

eggtart_prince
Newbie
Posts: 7
Joined: Mon Apr 23, 2018 5:56 am
Contact:

Re: Weirdest issue with Class and loop

#7 Post by eggtart_prince »

The documentation is a little confusing and it expects users to have a little bit of understanding on python programming. I wish he would give us more examples on how to use some of the functions.

And DannX, thanks for the help.

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

Re: Weirdest issue with Class and loop

#8 Post by kivik »

I think to be fair, for basic visual novels (not that I've played any in years to know) the early sections of the documentations does give you all that you need. It's when you want to do the more advanced stuff but don't have experience with python / renpy conventions that it gets hard!

Post Reply

Who is online

Users browsing this forum: No registered users