Default ability not affecting action?

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
DewyNebula
Newbie
Posts: 15
Joined: Sun Apr 21, 2024 10:57 am
Contact:

Default ability not affecting action?

#1 Post by DewyNebula »

Hiya. I edited my code to be smoother, changing the player stats to default so I didn't have to constantly create a Python block when I needed to refer to them. However, the section in my character creator to increase the player's abilities no longer impacts the stats. For example, the strength ability doesn't affect attacking anymore. Here is a snippet of my code:

Code: Select all

    # Abilities increase
    vbox: 
        align(0.2, 0.6)
        spacing 35
        if creation == 0 or strength == 8:
            imagebutton auto "button-increase-%s" action NullAction() 
        else:
            imagebutton auto "button-increase-%s" action [SetVariable("strength", strength + 1), SetVariable("creation", creation - 1)]
# Ability stats
default strength = 3
default dexterity = 3
default constitution = 3
default intelligence = 3
default wisdom = 3
default charisma = 3
# Clothing stats
default weapon_attack = 0
# Battle stats
default player_attack = strength + weapon_attack + renpy.random.randint(0, 2)
    while (player_health > 0) and (burglar_health > 0):
        # Player Attack menu
        menu:
            "Attack":
                if burglar_dodge >= 10:
                    "The man manages to dodge your attack. Damn!"                
                elif player_attack <= 4:
                    $ burglar_health -= player_attack
                    $ random_dialogue = renpy.random.choice(weak_attack_dialogue)
                    "[random_dialogue]"

User avatar
m_from_space
Eileen-Class Veteran
Posts: 1012
Joined: Sun Feb 21, 2021 3:36 am
Contact:

Re: Default ability not affecting action?

#2 Post by m_from_space »

DewyNebula wrote: Mon May 13, 2024 7:02 pm Hiya. I edited my code to be smoother, changing the player stats to default so I didn't have to constantly create a Python block when I needed to refer to them.
I don't quite get what you mean by that, but maybe let me tell you things about variables in Renpy.

1. Better never declare variables inside a Python block, they are treated as constants
2. Declaring variables using "define" (for constants) and "default" (for variables) is the way to go
3. If you declare variables using "default" outside of a screen, that means that this variables will have this value when starting a new game
4. If you declare it inside a screen instead, it means you are creating a (local) screen variable, which will be set to this value whenever the screen is shown (not drawn!!) using "show" or "call" or the Python equivalents.

Your code is a bit weird, you probably left out important parts, but here are how certain "default" variables work:

Code: Select all

# this is a global variable, which will only be "calculated" once a new game starts
default strength = 10
# same goes for this one, it will not react to changes in "strength" later on
default player_attack_1 = strength + 100

###

screen myscreen():
    # placed here instead, you are creating a screen variable, which will be set every time the screen is shown
    # (shown, not drawn! it won't update automatically when "strength" changes, unless you re-show the screen)
    default player_attack_2 = strength + 100

    text "Attack Value: [player_attack_2]"

###

# just another global variable
default player_attack_3 = 0

screen myotherscreen():
    # this variable will be calculated every time the screen is drawn, so changes to "strength" appear instantly.
    $ player_attack_3 = strength + 100
    
    text "Attack Value: [player_attack_3]"

Post Reply

Who is online

Users browsing this forum: Google [Bot], Majestic-12 [Bot]