Variable not working as expected

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
User avatar
Imperf3kt
Eileen-Class Veteran
Posts: 1826
Joined: Mon Dec 14, 2015 5:05 am
Location: Your monitor
Contact:

Variable not working as expected

#1 Post by Imperf3kt » Sun Jan 14, 2018 1:28 am

For various reasons I am making a rock paper scissors style game.
I did some googling before attempting and found one game folder on github that was made with the deprecated ui. functions.
Though it does work flawlessly, I don't wish to use the code. Its also not as customisable as I require.

I found another post by PyTom, that doesn't work with the version of Ren'Py I am using (6.99.14.3134)

So I resigned to making my own.
The base "game" was simple to make, only took a few minutes of fiddling around and double-checking in the documentation.
My issue comes in when I try to customise how it looks.

What I am trying to do is have a bar on the left with a number inside it reflecting your total number of wins. I then plan to duplicate the bar for losses and ties.
After a certain number of wins (currently 1 for testing purposes) the bar is supposed to switch out with a flaming bar for visual effect.
My issue though is that the flaming bar is appearing instead of the non-flaming bar, despite me not even "winning" any games and I cannot tell why. I checked and "wins" is definitely registering 0.

Here is the code I've been pulling my hair out over for the last ~4 hours

Code: Select all

define d = Character("Developer")

## 10fps
image bar_animated:
    "gui/interface/00_bar_hot.png"
    pause 0.1
    "gui/interface/01_bar_hot.png"
    pause 0.1
    "gui/interface/02_bar_hot.png"
    pause 0.1
    "gui/interface/03_bar_hot.png"
    pause 0.1
    "gui/interface/04_bar_hot.png"
    pause 0.1
    "gui/interface/05_bar_hot.png"
    pause 0.1
    "gui/interface/06_bar_hot.png"
    pause 0.1
    "gui/interface/07_bar_hot.png"
    pause 0.1
    "gui/interface/08_bar_hot.png"
    pause 0.1
    "gui/interface/09_bar_hot.png"
    pause 0.1
    "gui/interface/10_bar_hot.png"
    pause 0.1
    "gui/interface/11_bar_hot.png"
    repeat

default result = "none"
default rps_opponent = "1"

default selection = "none"
default wins = "0"

label start:
    
    scene rps
    show screen stats

label rps:

    menu:
        "Rock, Paper, Scissors!"
        
        "Rock":
            $selection = "rock"
            $rps_opponent = renpy.random.randint(1, 3)
        "Paper":
            $selection = "paper"
            $rps_opponent = renpy.random.randint(1, 3)
        "Scissors":
            $selection = "scissors"
            $rps_opponent = renpy.random.randint(1, 3)
        "End":
            jump fin
    
label results:

####rock####
    
    if rps_opponent == 1 and selection == "rock":
        $ result = "rock"
        jump tie
        
    elif rps_opponent == 1 and selection == "paper":
        $ result = "rock"
        jump win
        
    elif rps_opponent == 1 and selection == "scissors":
        $ result = "rock"
        jump lose

####paper####

    if rps_opponent == 2 and selection == "rock":
        $ result = "paper"
        jump lose
        
    elif rps_opponent == 2 and selection == "paper":
        $ result = "paper"
        jump tie
        
    elif rps_opponent == 2 and selection == "scissors":
        $ result = "paper"
        jump win

####scissors####

    if rps_opponent == 3 and selection == "rock":
        $ result = "scissors"
        jump win
        
    elif rps_opponent == 3 and selection == "paper":
        $ result = "scissors"
        jump lose
        
    elif rps_opponent == 3 and selection == "scissors":
        $ result = "scissors"
        jump tie

label tie:
    
    d "We tied with [result]!"
    jump rps
    
label win:
    
    d "[selection] beats [result], you win!"
    $ wins += 1
    jump rps
    
label lose:
    
    d "[result] beats [selection], you lost!"
    jump rps
    
label fin:
    return

Code: Select all

screen stats():
    
    modal False
    zorder 100
    if wins >= 1:
        vbox:

            yalign 0
            xalign 0

            add "bar_animated"
    else:
        vbox:

            yalign 0
            xalign 0
            add "gui/interface/bar.png"

I've tried deleting persistent, adding [wins] to the script to check it is indeed "0", recompiling all files, etc.
I cannot work out why the wrong image is showing.
If I reverse the if to if wins <= 1: then I get the opposite effect, the flaming bar never shows, despite winning numerous times. Only the regular bar shows instead.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

User avatar
Ocelot
Miko-Class Veteran
Posts: 849
Joined: Tue Aug 23, 2016 10:35 am
Skype: miinipaa
Contact:

Re: Variable not working as expected

#2 Post by Ocelot » Sun Jan 14, 2018 2:45 am

default wins = "0" Here wins is a string. In your screen it compare it to integer. And string is always larger than any integer. So there is your problem. I am curious, why don't you have any errors: wins += 1 should result in TypeError
< < insert Rick Cook quote here > >

User avatar
Imperf3kt
Eileen-Class Veteran
Posts: 1826
Joined: Mon Dec 14, 2015 5:05 am
Location: Your monitor
Contact:

Re: Variable not working as expected

#3 Post by Imperf3kt » Sun Jan 14, 2018 4:09 am

Not sure myself but I knew it had something to do with that line.

Thanks for pointing it out, I'll go make the required adjustments.

E: Perfect! Solved the problem instantly. Slipped my mind somehow.
I also fixed up the default rps_opponent = 1 line

to anyone out there pretty new to Ren'Py or Python, what was done was simply remove the quotes "


Oh and of course, if anyone happens upon this in the future, feel free to use it for anything you like. Its only made up of the most basic Ren'Py language.
I'm going to see if I can't optimise it somewhat :)
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

User avatar
Ocelot
Miko-Class Veteran
Posts: 849
Joined: Tue Aug 23, 2016 10:35 am
Skype: miinipaa
Contact:

Re: Variable not working as expected

#4 Post by Ocelot » Sun Jan 14, 2018 4:28 am

I'm going to see if I can't optimise it somewhat :)
You can ditch rps_opponent, and use only result by doing:
$ result = ['rock', 'paper', 'scissors'][renpy.random.randint(1, 3)]
< < insert Rick Cook quote here > >

User avatar
IrinaLazareva
Veteran
Posts: 254
Joined: Wed Jun 08, 2016 1:49 pm
Projects: Legacy, Эра Тёмных Небес
Organization: IKG
Location: St.Petersburg, Russia
Contact:

Re: Variable not working as expected

#5 Post by IrinaLazareva » Sun Jan 14, 2018 3:13 pm

Code: Select all

#Rock Paper Scissors Lizard Spock

default player1 = None
default player2 = None
default situation = [
    'Scissors cuts Paper',
    'Paper covers Rock',
    'Rock crushes Lizard', 
    'Lizard poisons Spock',
    'Spock smashes Scissors',
    'Scissors decapitates Lizard',
    'Lizard eats Paper',
    'Paper disproves Spock',
    'Spock vaporizes Rock', 
    'Rock crushes Scissors',
    ]
    
default mwinner = [
    ('Scissors', 'Paper'),   #Scissors cuts Paper 
    ('Paper', 'Rock'),     #Paper covers Rock
    ('Rock', 'Lizard'),  #Rock crushes Lizard 
    ('Lizard', 'Spock'),     #Lizard poisons Spock
    ('Spock', 'Scissors'),     #Spock smashes Scissors
    ('Scissors', 'Lizard'),  # Scissors decapitates Lizard
    ('Lizard', 'Paper'),     #Lizard eats Paper 
    ('Paper', 'Spock'),        # Paper disproves Spock 
    ('Spock', 'Rock'),      # Spock vaporizes Rock
    ('Rock', 'Scissors'), #Rock crushes Scissors
    ]

image implayer1 = ConditionSwitch(
    'player1==None', Animation(
        'rock.png', .5, 
        'paper.png', .5,
        'scissors.png', .5,
        'lizard.png', .5,
        'spock.png', .5,
        ),
    'player1=="Rock" ', 'rock.png',
    'player1=="Paper" ', 'paper.png',
    'player1=="Scissors" ', 'scissors.png',
    'player1=="Lizard" ', 'lizard.png',    
    'player1=="Spock" ', 'spock.png',
    )
image implayer2 = ConditionSwitch(
    'player2==None', Animation(
        'rock2.png', .5, 
        'paper2.png', .5,
        'scissors2.png', .5,
        'lizard2.png', .5,
        'spock2.png', .5,
        ),
    'player2=="Rock" ', 'rock2.png',
    'player2=="Paper" ', 'paper2.png',
    'player2=="Scissors" ', 'scissors2.png',     
    'player2=="Lizard" ', 'lizard2.png',   
    'player2=="Spock" ', 'spock2.png',    
    )

screen statgame():
    frame:
        align (.5,.1)
        grid 2 2:
            add 'implayer1' size(150,150) # null   # <- replace for test without images
            add 'implayer2' size(150,150) # null   # <- replace for test without images
            text 'You: [player1]'
            text 'Computer: [player2]'
    frame:
        align (.5, .35)
        for i in range(len(situation)):
            if (player1, player2)==mwinner[i]:
                vbox:
                    text situation[i] color "#3f0"
                    text "You win!"
            elif (player2, player1)==mwinner[i]:
                vbox:
                    text situation[i] color "#fe0"
                    text "You lost!"
                    
label letsbegin:
    $ player1 = None
    $ player2 = None
    "Ready!"
    show screen statgame
    menu:
        "Rock! Paper! Scissors! Lizard! Spock!"
        'Rock':
            $ player1 = 'Rock'
        'Paper':
            $ player1 = 'Paper'
        'Scissors':
            $ player1 = 'Scissors'
        'Lizard':
            $ player1 = 'Lizard'
        'Spock':
            $ player1 = 'Spock'
    $ player2 = renpy.random.choice(['Rock', 'Paper', 'Scissors', 'Lizard', 'Spock'])        
    pause
    hide screen statgame
    menu:
        "Replay?"
        "Yep":
            jump letsbegin
        "Nope":
            pass        
    return
There is a light at the end of the tunnel, just pray it's not a train.

User avatar
IrinaLazareva
Veteran
Posts: 254
Joined: Wed Jun 08, 2016 1:49 pm
Projects: Legacy, Эра Тёмных Небес
Organization: IKG
Location: St.Petersburg, Russia
Contact:

Re: Variable not working as expected

#6 Post by IrinaLazareva » Sun Jan 14, 2018 3:18 pm

and classic...

Code: Select all

#Rock Paper Scissors

default situation = ['Scissors cuts Paper', 'Paper covers Rock', 'Rock crushes Scissors']
default mwinner = [('Scissors', 'Paper'), ('Paper', 'Rock'), ('Rock', 'Scissors')]

image implayer1 = ConditionSwitch(
    'player1==None', Animation('rock.png', .5, 'paper.png', .5, 'scissors.png', .5),
    'player1=="Rock" ', 'rock.png',
    'player1=="Paper" ', 'paper.png',
    'player1=="Scissors" ', 'scissors.png',
    )
image implayer2 = ConditionSwitch(
    'player2==None', Animation('rock2.png', .5, 'paper2.png', .5, 'scissors2.png', .5),
    'player2=="Rock" ', 'rock2.png',
    'player2=="Paper" ', 'paper2.png',
    'player2=="Scissors" ', 'scissors2.png',  
    )

screen statgame():
    frame:
        align (.5,.1)
        grid 2 2:
            add 'implayer1' size(150,150) # null   # <- replace for test without images
            add 'implayer2' size(150,150) # null   # <- replace for test without images
            text 'You: [player1]'
            text 'Computer: [player2]'
    frame:
        align (.5, .35)
        for i in range(len(situation)):
            if (player1, player2)==mwinner[i]:
                vbox:
                    text situation[i] color "#3f0"
                    text "You win!"
            elif (player2, player1)==mwinner[i]:
                vbox:
                    text situation[i] color "#fe0"
                    text "You lost!"
                    
label letsbegin:
    $ player1 = None
    $ player2 = None
    "Ready!"
    show screen statgame
    menu:
        "Rock! Paper! Scissors!"
        'Rock':
            $ player1 = 'Rock'
        'Paper':
            $ player1 = 'Paper'
        'Scissors':
            $ player1 = 'Scissors'
    $ player2 = renpy.random.choice(['Rock', 'Paper', 'Scissors'])        
    pause
    hide screen statgame
    menu:
        "Replay?"
        "Yep":
            jump letsbegin
        "Nope":
            pass        
    return
There is a light at the end of the tunnel, just pray it's not a train.

User avatar
Imperf3kt
Eileen-Class Veteran
Posts: 1826
Joined: Mon Dec 14, 2015 5:05 am
Location: Your monitor
Contact:

Re: Variable not working as expected

#7 Post by Imperf3kt » Sun Jan 14, 2018 4:36 pm

Ocelot wrote:
Sun Jan 14, 2018 4:28 am
I'm going to see if I can't optimise it somewhat :)
You can ditch rps_opponent, and use only result by doing:
$ result = ['rock', 'paper', 'scissors'][renpy.random.randint(1, 3)]
Thanks a bunch! I need to learn more Python, I barely understand the basics. I mostly rely on logic I learnt through HTML, CSS and the tiny bit of JavaScript I touched upon before I discovered Ren'Py :P


IrinaLazareva, both of those look quite complex but thank you for posting them. I might be able to understand how better to do certain things by studying them.

Certainly, I intend to convert from a choicemenu, to imagebuttons eventually, which may not turn out quite as optimal as it currently is.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Post Reply

Who is online

Users browsing this forum: TomDowd