Page 1 of 1

Conditionals & Lists? (solved)

Posted: Sun Nov 23, 2014 3:07 pm
by meiri
There's a small explanation through comments in the code below, but I'll still try to explain it.
BTW, I'm building a rhythm game, which should hopefully explain some of the code?

Code: Select all

screen r:
    add "#FFBCD6"
    text "Rhythm Game" xalign 0.5 yalign 0.05 color "#921F4B"
    text "Level: [level]" xalign 0.5 yalign 0.3 color "#921F4B"
        
    hbox:
        yalign 0.5 xalign 0.5
        textbutton "E3" action Play("sound", bsound1), SetVariable("pressed1", True)
        textbutton "E4" action Play("sound", bsound2), SetVariable("pressed2", True)
        textbutton "D4" action Play("sound", bsound3), SetVariable("pressed3", True)

    if pressed1: # When button E3 is pressed, pressed1 is set to true 
        $ count += 1 
        $ order.append(1)
    elif pressed2: # When button E4 is pressed, pressed2 is set to true
        $ order.append(2)
        $ count += 1
    elif pressed3: # When button D4 is pressed, pressed3 is set to true 
        $ count += 1
        $ order.append(3)
        
    if count >= 3: # No matter what order you press them in, when to count is 3 or greater...
        if (order[0] == 1) and (order[1] == 2) and (order[2] == 3): # ... It'll check to see if index in the list "order" is exactly each number
            $ levelcompleted = True # But level completed is always being set to true
        else:  
            $ levelnotcompleted = True

    if levelcompleted:
        text "Good job! You won the first level!" xalign 0.5 yalign 0.6
        textbutton "Next Level!" action Jump("level_2")
        
    elif levelnotcompleted:
        text "Sorry! Better luck next time, you didn't win!"
        textbutton "Try again?" action Jump("playagain")
Here is where I tell the player what sounds to play:

Code: Select all

label start: 

    $ bsound1 = "E3.mp3"
    $ bsound2 = "E4.mp3"
    $ bsound3 = "D4.mp3"

# some irrelevant code in a label in between here...

label level_1:
    "LEVEL 1"

    queue sound [ bsound1, bsound2, bsound3 ] 
They are supposed to memorize what bsound1, bsound2, and bsound3 all sound like (which is shown in a label earlier).

I should also mention that I have $ order = [] and $ count = 0 initialized earlier in the code...

Anyways, I calls screen r, and there are 3 buttons on the screen.
If the player presses button E4 only 3 times then the code should look like this:

Code: Select all

$ order = [2, 2, 2]
$ count = 3
and this if statement

Code: Select all

    if count >= 3: # No matter what order you press them in, when to count is 3 or greater...
        if (order[0] == 1) and (order[1] == 2) and (order[2] == 3): # ... It'll check to see if index in the list "order" is exactly each number
            $ levelcompleted = True # But level completed is always being set to true
        else:  
            $ levelnotcompleted = True
SHOULD come out false, and jump to the else beneath it and set levelnotcompleted to True. But no matter what, every time, that first if statement comes out True and levelcompleted sets itself to True.

So basically, no matter what order those textbuttons are pressed, levelcompleted is set to True and the player can advance in the game... I feel like it either has something to do with all those if statements, or with the list/array.
Because, before I even press the buttons, order already has 1, 2, and 3, indexed within it?
screenshot0021.png
So, I'm really not sure why it's caused, but thanks to any help in advance.

Re: Conditionals & Lists?

Posted: Sun Nov 23, 2014 3:56 pm
by akemicchi
You're not supposed to change variables in screens unless it's with SetVariable. I don't think the variables even change if you try to do it that way.

You can get around this by putting the changes in a python block (count += 1 and append), but the better solution would be to only have a screen that displays things (the buttons, as well as the text when conditions are met). Then, in the label in Ren'Py language, that's where you change the values. Doing it like this won't mess with rollback, either.

I'm also getting weird behavior like multiple values getting appended in the list and I have no idea why, haha. If you comment out one of the order.append(#) lines, the number will disappear from the list, but the others will still appear multiple times. I'm guessing Ren'Py actually follows through and appends it even if there's a conditional statement that SHOULDN'T allow it to do that, but I have no idea why it does so multiple times.

Re: Conditionals & Lists?

Posted: Mon Nov 24, 2014 6:47 pm
by meiri
akemicchi wrote:You're not supposed to change variables in screens unless it's with SetVariable. I don't think the variables even change if you try to do it that way.
Yeah, the only reason why I put that in the screen was because I thought it'd help. I originally had it inside the label, but I still came up with the same issue.
akemicchi wrote: You can get around this by putting the changes in a python block (count += 1 and append), but the better solution would be to only have a screen that displays things (the buttons, as well as the text when conditions are met). Then, in the label in Ren'Py language, that's where you change the values. Doing it like this won't mess with rollback, either.
I did try this, and it ended up causing the variables not to change at all, which is just... strange.
I'll definitely keep working with your suggestions, though, thanks!

Re: Conditionals & Lists?

Posted: Tue Nov 25, 2014 4:58 pm
by Alex
Try

Code: Select all

screen r:
    add "#FFBCD6"
    text "Rhythm Game" xalign 0.5 yalign 0.05 color "#921F4B"
    text "Level: [level]" xalign 0.5 yalign 0.3 color "#921F4B"
        
    hbox:
        yalign 0.5 xalign 0.5
        textbutton "E3" action Play("sound", bsound1), Return(1)
        textbutton "E4" action Play("sound", bsound2), Return(2)
        textbutton "D4" action Play("sound", bsound3), Return(3)

label start:
    $ bsound1 = "E3.mp3"
    $ bsound2 = "E4.mp3"
    $ bsound3 = "D4.mp3"
    $ order = []
    $ levelcompleted = False

label level_1
    $ level = 1
    "LEVEL 1"

label loop:
    show screen r
    while len(order) < 3:
        $ res = ui.interact()
        $ order.append(res)

    hide screen r
    if (order[0] == 1) and (order[1] == 2) and (order[2] == 3):
        $ levelcompleted = True # But level completed is always being set to true
    else:  
        $ levelcompleted = False

    if levelcompleted:
        text "Good job! You won the first level!" xalign 0.5 yalign 0.6
        jump level_2
        
    elif:
        text "Sorry! Better luck next time, you didn't win!"
        jump level_1

Re: Conditionals & Lists?

Posted: Tue Nov 25, 2014 6:17 pm
by Saltome
If that doesn't work, could you provide a demo of the problem? I should be able to trace the problem if I can actually run it.

Re: Conditionals & Lists?

Posted: Tue Nov 25, 2014 6:48 pm
by meiri
Alex wrote:Try
[ ... ]
This worked, thank you! Also learned something new (never knew ui.interact() could be used in that way)!
Saltome wrote:If that doesn't work, could you provide a demo of the problem? I should be able to trace the problem if I can actually run it.
It did work. I appreciate the offer, though!