Page 1 of 1

screen language: 'for' loop & calling functions

Posted: Fri Mar 09, 2012 9:03 pm
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..?

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

Posted: Sat Mar 10, 2012 4:47 am
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.

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

Posted: Sat Mar 10, 2012 5:29 am
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)

Posted: Sat Mar 10, 2012 5:40 am
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

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

Posted: Sat Mar 10, 2012 5:49 am
by nyaatrap

Code: Select all

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

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

Posted: Sat Mar 10, 2012 7:09 am
by vociferocity
ah, it totally worked! thank you so much :D

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

Posted: Sat Mar 10, 2012 7:30 am
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])

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

Posted: Sat Mar 10, 2012 7:47 am
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