the number of dice and the modifier do not change the value

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
Andredron
Miko-Class Veteran
Posts: 729
Joined: Thu Dec 28, 2017 2:37 pm
Location: Russia
Contact:

the number of dice and the modifier do not change the value

#1 Post by Andredron »

I am trying to create a dice roll calculator for renpy but I encountered an error that the number of dice does not change and the modifier does not change either

Code: Select all

init python:
    import json
    import random

    # Dictionary for storing the results of rolls
    roll_history = {}

    # Function for rolling dice
    def roll_dice(dice_type, dice_count, modifier):
        result = 0
        for _ in range(dice_count):
            result += random.randint(1, dice_type)
        result += modifier
        return result

    # Function for saving the results of a roll to history
    def save_roll(dice_type, dice_count, modifier, result):
        roll_id = len(roll_history) + 1
        roll_history[roll_id] = {
            “dice_type”: dice_type,
            “dice_count”: dice_count,
            “modifier”: modifier,
            “result”: result
        }
        with open(“roll_history.json”, “w”) as f:
            json.dump(roll_history, f)

# Dice roll screen
screen roll_dice_screen:
    # Variables to store the current values
    $ dice_type = 6
    $ dice_count = 1
    $ modifier = 0
    $ result = 0

    vbox:
        xalign 0.5
        yalign 0.5
        spacing 10

        # Select a die type
        text “Select cube type:”
        hbox:
            spacing 10
            textbutton “d4” action SetVariable(“dice_type”, 4)
            textbutton “d6” action SetVariable(“dice_type”, 6)
            textbutton “d8” action SetVariable(“dice_type”, 8)
            textbutton “d10” action SetVariable(“dice_type”, 10)
            textbutton “d12” action SetVariable(“dice_type”, 12)
            textbutton “d20” action SetVariable(“dice_type”, 20)
            textbutton “d100” action SetVariable(“dice_type”, 100)

        # Select the number of dice. ###############EROR!
        text “Select the number of dice:”
        hbox:
            spacing 10
            textbutton “-1” action If(dice_count > 1, SetVariable(“dice_count”, dice_count - 1))
            text “[dice_count]d”
            textbutton “+1” action SetVariable(“dice_count”, dice_count + 1)

        # Select modifier ###############EROR!
        text “Select modifier:”
        hbox:
            spacing 10
            textbutton “-1” action SetVariable(“modifier”, modifier - 1)
            text “[modifier:+]” # Fixed modifier display
            textbutton “+1” action SetVariable(“modifier”, modifier + 1)


        # Dice roll button
        textbutton “roll” action [
            SetVariable(“result”, roll_dice(dice_type, dice_count, modifier)),
            Function(save_roll, dice_type, dice_count, modifier, result),
            Show(“roll_result_screen”, result=result)
        ] xalign 0.5 yalign 0.9

# Screen to display the result of the roll
screen roll_result_screen(result):
    text “[result]” xalign 0.5 yalign 0.5
    textbutton “OK” action Return() xalign 0.5 yalign 0.7

label start:
    # Attempt to load the throw history from a file
    python:
        try:
            with open(“roll_history.json”, “r”) as f:
                roll_history = json.load(f)
        except FileNotFoundError:
            roll_history = {}

    # Display the screen for rolling dice
    call screen roll_dice_screen

    # Display the result of the last roll
    python:
        if roll_history:
            last_roll = roll_history[str(len(roll_history))]]
            last_roll_str = f “Your last roll: {last_roll['dice_count']}d{last_roll['dice_type']}+{last_roll['modifier']} = {last_roll['result']}“”
        else:
            last_roll_str = “No previous rolls.”

    “{last_roll_str}”

label show_old_roll:
    # Loading the history of rolls
    python:
        With open(“roll_history.json”, “r”) as f:
            roll_history = json.load(f)

    # Select a roll (e.g., roll #2)
    $ roll_id = 2


    # Check if there is a roll with the given ID
    if str(roll_id) in roll_history:
        $ old_roll = roll_history[str(roll_id)]
        “Roll #[roll_id]: [old_roll['dice_count']]d[old_roll['dice_type']]+[old_roll['modifier']] = [old_roll['result']]”
    else:
        “Roll #[roll_id] was not found.”
I've got a problem, I'm not changing the values of these

Code: Select all

        # Select the number of dice. ###############EROR!
        text “Select the number of dice:”
        hbox:
            spacing 10
            textbutton “-1” action If(dice_count > 1, SetVariable(“dice_count”, dice_count - 1))
            text “[dice_count]d”
            textbutton “+1” action SetVariable(“dice_count”, dice_count + 1)

        # Select modifier ###############EROR!
        text “Select modifier:”
        hbox:
            spacing 10
            textbutton “-1” action SetVariable(“modifier”, modifier - 1)
            text “[modifier:+]” # Fixed modifier display
            textbutton “+1” action SetVariable(“modifier”, modifier + 1)
please help me to fix this error

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2420
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: the number of dice and the modifier do not change the value

#2 Post by Ocelot »

Your buttons are working fine and changing values as expected. There just a litlle thing to remember about screens: their code is constantly rerun. For example before each interaction, which happens to be after you press the button and it execute its actions. Mainly I am talking about this piece of code:

Code: Select all

screen roll_dice_screen:
    # Variables to store the current values
    $ dice_type = 6
    $ dice_count = 1
    $ modifier = 0
    $ result = 0
It resets values every time screen updates.
You want to set default values for the values, not to reset them every time screen is evaluated
The default statement sets the default value of a variable, if it is not passed as an argument to the screen, or inherited from a screen that calls us using the use statement.

Code: Select all

screen roll_dice_screen():
    default dice_type = 6
    # . . .
< < insert Rick Cook quote here > >

User avatar
Andredron
Miko-Class Veteran
Posts: 729
Joined: Thu Dec 28, 2017 2:37 pm
Location: Russia
Contact:

Re: the number of dice and the modifier do not change the value

#3 Post by Andredron »

Thanks a lot, changed both in action and def, didn't help.

Code: Select all

init python:
    import json
    import random

    # Dictionary for storing the results of rolls
    roll_history = {}

    # Function for rolling dice
    def roll_dice(dice_type, dice_count, modifier):
        result = random.randint(dice_count, dice_type * dice_count) + modifier
        return result

    # Function for saving the results of a roll to history
    def save_roll(dice_type, dice_count, modifier, result):
        roll_id = len(roll_history) + 1
        roll_history[roll_id] = {
            "dice_type": dice_type,
            "dice_count": dice_count,
            "modifier": modifier,
            "result": result
        }
        with open("roll_history.json", "a") as f:
            json.dump(roll_history, f)

# Dice roll screen
screen roll_dice_screen:
    # Variables to store the current values
    default dice_type = 6
    default dice_count = 1
    default modifier = 0
    default result = 0

    vbox:
        xalign 0.5
        yalign 0.5
        spacing 10

        # Select a die type
        text "Select cube type:"
        hbox:
            spacing 10
            textbutton "d4" action SetVariable("dice_type", 4)
            textbutton "d6" action SetVariable("dice_type", 6)
            textbutton "d8" action SetVariable("dice_type", 8)
            textbutton "d10" action SetVariable("dice_type", 10)
            textbutton "d12" action SetVariable("dice_type", 12)
            textbutton "d20" action SetVariable("dice_type", 20)
            textbutton "d100" action SetVariable("dice_type", 100)

        # Select the number of dice
        text "Select the number of dice:"
        hbox:
            spacing 10
            textbutton "-1" action If(dice_count > 1, SetVariable("dice_count", dice_count - 1))
            text "[dice_count]d[dice_type]"
            textbutton "+1" action SetVariable("dice_count", dice_count + 1)

        # Select modifier
        text "Select modifier:"
        hbox:
            spacing 10
            textbutton "-1" action SetVariable("modifier", modifier - 1)
            text "[" + str(modifier) + "]"
            textbutton "+1" action SetVariable("modifier", modifier + 1)

        # Dice roll button
        textbutton "roll" action [
            SetVariable("result", roll_dice(dice_type, dice_count, modifier)),
            Function(save_roll, dice_type, dice_count, modifier, result),
            Show("roll_result_screen", result=result)
        ] xalign 0.5 yalign 0.9

# Screen to display the result of the roll
screen roll_result_screen(result):
    text "[result]" xalign 0.5 yalign 0.5
    textbutton "OK" action Return() xalign 0.5 yalign 0.7

label start:
    # Attempt to load the throw history from a file
    python:
        try:
            with open("roll_history.json", "r") as f:
                roll_history = json.load(f)
        except FileNotFoundError:
            roll_history = {}

    # Display the screen for rolling dice
    call screen roll_dice_screen

    # Display the result of the last roll
    python:
        if roll_history:
            last_roll = roll_history[str(len(roll_history))]
            last_roll_str = f"Your last roll: {last_roll['dice_count']}d{last_roll['dice_type']}+{last_roll['modifier']} = {last_roll['result']}"
        else:
            last_roll_str = "No previous rolls."

    "{last_roll_str}"

Last edited by Andredron on Sat May 11, 2024 10:03 am, edited 3 times in total.

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2420
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: the number of dice and the modifier do not change the value

#4 Post by Ocelot »

And as those are not global variables, you need to use SetScreenVariable instead. Or, even better: IncrementScreenVariable
https://www.renpy.org/doc/html/screen_a ... enVariable
< < insert Rick Cook quote here > >

Post Reply

Who is online

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