screen language: 'for' loop & calling functions

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
vociferocity
Regular
Posts: 93
Joined: Sat Jun 12, 2010 11:27 am
Projects: Rogue of Heart, Valkyrie
Contact:

screen language: 'for' loop & calling functions

#1 Post by vociferocity »

okayyy so, I have a few questions:

first off, I'm trying to display all the stocked items in a shop with a for loop in my shop screen, since all the items have the same code... but instead of displaying four different items, it's displaying the final item four times, with some extra data in the front. here's my code:

Code: Select all

        vbox:
            xalign 0.5
            yalign 0.5
            for item in enumerate(a_Stock):
                vbox:
                    text "[item]"
it's displaying "(3, ["Crossbow", 1, "A new crossbow", 70, 35, 10, -5])" four times, so I'm clearly using ren'py's "for" wrong. the documentation was pretty unclear on how to use it, though? so if anyone has any hints, I'd be super greatful :)

also, I'm trying to call a "buy" function I wrote in python, but my understanding of currying is sooo poor, so I'm probably doing this all wrong. it's not giving me any errors, it just doesn't seem to actually be doing anything? my code looks like this:

Code: Select all

## this is over in my init python block
    def BuyItem(item, money):
         
        for x in range(0, len(a_OwnedItems)):
            if a_OwnedItems[x] == item:
                a_OwnedItems[x][1] += 1
                money -= item[3]
                return
        
        a_OwnedItems.append( copy.deepcopy(item[:]))
        money -= item[3]
        return

## shop code (I removed a lot of extra stuff to make this post cleaner, but this basic code doesn't work either)
screen Buy:    
    
    python:
        buy = renpy.curry(BuyItem)
    
    frame:
        xalign 0.5
        yalign 0.5
        
        vbox:
            xalign 0.5
            yalign 0.5
            text "[i_Money]" # displays current amount of money
            text "[a_Stock[0][0]]" # displays the name of first item in stock
            textbutton "Buy Item?" action buy(a_Stock[0], i_Money) 
            textbutton "return" action Return
help..?

User avatar
SleepKirby
Veteran
Posts: 255
Joined: Mon Aug 09, 2010 10:02 pm
Projects: Eastern Starlight Romance, Touhou Mecha
Organization: Dai-Sukima Dan
Location: California, USA
Contact:

Re: screen language: 'for' loop & calling functions

#2 Post by SleepKirby »

first off, I'm trying to display all the stocked items in a shop with a for loop in my shop screen, since all the items have the same code... but instead of displaying four different items, it's displaying the final item four times, with some extra data in the front.
I don't see anything wrong with the for loop. Have you confirmed that the contents of a_Stock are correct?
---
also, I'm trying to call a "buy" function I wrote in python, but my understanding of currying is sooo poor, so I'm probably doing this all wrong. it's not giving me any errors, it just doesn't seem to actually be doing anything?
Well, if I understand currying correctly, these two statements are equivalent:

Code: Select all

buy = renpy.curry(BuyItem)
buy = BuyItem
This is probably closer to what you want to do:

Code: Select all

buyFirstItem = renpy.curry(BuyItem, a_Stock[0], i_Money)
Since it would allow you to do this:

Code: Select all

textbutton "Buy Item?" action buyFirstItem
I'm having trouble finding a good reference about currying, for some reason. The best I can find is this page.

User avatar
nyaatrap
Crawling Chaos
Posts: 1824
Joined: Mon Feb 13, 2012 5:37 am
Location: Kimashi Tower, Japan
Contact:

Re: screen language: 'for' loop & calling functions

#3 Post by nyaatrap »

When you "call" functions, only return values can be changed. Maybe some values can be preserved, but I'm using Global for secure reason (it's my guess of why items can't be shown correctly, but don't know before seeing the full code)
Last edited by nyaatrap on Sat Mar 10, 2012 5:41 am, edited 1 time in total.

vociferocity
Regular
Posts: 93
Joined: Sat Jun 12, 2010 11:27 am
Projects: Rogue of Heart, Valkyrie
Contact:

#4 Post by vociferocity »

nyaatrap wrote:When you "call" functions, only return values can be changed. Maybe some values can be preserved, but I'm using Global for secure reason.
oh, that makes sense! is there any way to actually return values from a function called as a button?

@sleepkirby: when I display the contents of a_Stock separately, it all works fine :( I guess I'll just go back to doing that? sigh, there goes my dream of having cleaner code XD

User avatar
nyaatrap
Crawling Chaos
Posts: 1824
Joined: Mon Feb 13, 2012 5:37 am
Location: Kimashi Tower, Japan
Contact:

Re: screen language: 'for' loop & calling functions

#5 Post by nyaatrap »

Code: Select all

def BuyItem(item, money):
    global a_OwnedItems
Maybe you need something like this to change variables into the global.

vociferocity
Regular
Posts: 93
Joined: Sat Jun 12, 2010 11:27 am
Projects: Rogue of Heart, Valkyrie
Contact:

Re: screen language: 'for' loop & calling functions

#6 Post by vociferocity »

ah, it totally worked! thank you so much :D

sciencewarrior
Veteran
Posts: 356
Joined: Tue Aug 12, 2008 12:02 pm
Projects: Valentine Square (writer) Spiral Destiny (programmer)
Location: The treacherous Brazilian Rainforest
Contact:

Re: screen language: 'for' loop & calling functions

#7 Post by sciencewarrior »

it's displaying "(3, ["Crossbow", 1, "A new crossbow", 70, 35, 10, -5])" four times, so I'm clearly using ren'py's "for" wrong. the documentation was pretty unclear on how to use it, though? so if anyone has any hints, I'd be super greatful :)
The first problem is that "[item]" isn't evaluated in the spot. In fact, these interpolated strings are re-evaluated after every interaction, so that's why only the last value of the item variable appears in all the four vboxes.
The second problem is that you are passing the whole stock data structure instead of item[0], and it is doing its best to render it.
I didn't test it, but maybe something like this would work:

Code: Select all

            for item in enumerate(a_Stock):
                vbox:
                    text "{0}".format(item[0])
Keep your script in your Dropbox folder.
It allows you to share files with your team, keeps backups of previous versions, and is ridiculously easy to use.

vociferocity
Regular
Posts: 93
Joined: Sat Jun 12, 2010 11:27 am
Projects: Rogue of Heart, Valkyrie
Contact:

Re: screen language: 'for' loop & calling functions

#8 Post by vociferocity »

oh that totally worked! I had to call item[1][0] to get the actual name of the item, but that totally did the trick :) thanks!

although I just realised that if I have to curry the functions with the parameters before the for loop, that means I can't really go "action buyitem(item)" or whatever, can I? bluh, back to the drawing board. at least now I understand how to do this stuff a bit better :) thanks for helping me out, guys! I super appreciate it

Post Reply

Who is online

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