Changing the random.randint values with variables ?

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
Gigan
Newbie
Posts: 21
Joined: Mon Jan 10, 2022 7:02 pm
Contact:

Changing the random.randint values with variables ?

#1 Post by Gigan »

Hello everybody ! My problem is a bit hard to explain, so I'll be as concise as possible.

Friendly version :
I have this code somewhere in my game : $ rng = renpy.random.randint(1,10)
Is there a way to make the variable pick some numbers only after an event occured ?

Not so friendly version :
I have a mini-game where the player can ask the croupier to see a card. There are 9 card multiplying or adding numbers to his score, but if he gets the one bad card, he looses everything. If he retreats before getting that card, his score is transformed into money he can spend elsewhere. The game works perfectly.

Thing is, each card represents a character from the game, and since the minigame is available very early, some are spoilers. I'd like the base deck to be composed of 4 cards only, and the player finding the other cards by exploring the world and talking to people. It will trigger events and variables that will automatically add new cards to the deck. I succeeded in doing that by adding a variable that makes some cards unavailable before the player triggers said event.

The problem is :

After each pull, the player has a menu where the croupier asks the player if they want another card, if they want to convert their points into money or if they want to leave the game.
Adding the new variable to the cards I want to hide doesn't prevent the renpy.random.randint from pulling the corresponding numbers. So I now have 6 chances out of 10 where, by choosing to get a new card, the player is sent to the "Do you want a new card or not" menu.

Basically, if in my renpy.random.randint(1,10), only number 1 to 4 are available at the start, I'd like the game to not be able to pull the other numbers before other events are triggered and other variable turned into 'True'.

I hope I was clear enough. That thing is melting my brain. ^^;

Here's the code of the previous game, when all 10 cards were available and everything worked fine (shortening it cause it's 10 times the same thing anyway...) :

Code: Select all

$ rng = renpy.random.randint(1,10)
    default money_points = 0
    default game_points = 10

    if rng == 1:
        if taureaugamecandice == True: ###If it's the first time the player gets this card, it triggers some additionnal talk with the croupier###
            show cartecandice at upleft ###The card appear###
            show croupier
            croupier "This one, don't piss her off, lemme tell you!"
            croupier "She's a bit like Fissa's unofficial mayor. I don't think she considers herself that way, but everyone here respects her a lot."
            croupier "That card muliplies your points by 2. Lucky."
            $ game_points *= 2 ###More points added to the game_points variable###
            $ taureaugamecandice = False ###This $ is now false, so the next time the player gets this card, he'll get the short talk version###
            hide cartecandice with dissolve ###The card disappear###
            jump choice ###The player jumps to the menu where he can choose to leave or play again###
        else:
            show cartecandice at upleft
            show croupier
            croupier "Candice Card ! It's a multiplier."
            $ game_points *= 2 
            hide cartecandice with dissolve
            jump choice

    elif rng == 2:
        if taureaugamekaeli == True:
            show cartekaeli at upleft
            show croupier
            croupier "Kaeli Windleaf."
            croupier "I think she lives in Fissa ? She often hangs around anyway."
            player "I met her on my way here. Her cat is cute !"
            croupier "Anyway, 10 more points."
            $ game_points += 10
            $ taureaugamekaeli = False          
            hide cartekaeli with dissolve
            jump choice
        else:
            show cartekaeli at upleft
            show croupier
            croupier "Kaeli and Cassiopée card ! 10 additionnal point." 
            hide cartekaeli with dissolve
            jump choice
            
            ...
...and here's the new code, with the new variables :

Code: Select all

default candice_card_in_inventory = False
default kaeli_card = True
...

label start:

...

$ rng = renpy.random.randint(1,10)
    default money_points = 0
    default game_points = 10

    if rng == 1 and if candice_card_in_inventory == True: ###The player won't see the Candice card or see the talk but nothing is preventing the rng variable from picking the number 1. The player will be sent to the choice menu###
        if taureaugamecandice == True: 
            show cartecandice at upleft 
            show croupier
            croupier "This one, don't piss her off, lemme tell you!"
            croupier "She's a bit like Fissa's unofficial mayor. I don't think she considers herself that way, but everyone here respects her a lot."
            croupier "That card muliplies your points by 2. Lucky."
            $ game_points *= 2 
            $ taureaugamecandice = False
            hide cartecandice with dissolve 
            jump choice ###The player jumps to the menu where he can choose to leave or play again###
        else:
            show cartecandice at upleft
            show croupier
            croupier "Candice Card ! It's a multiplier."
            $ game_points *= 2 
            hide cartecandice with dissolve
            jump choice

    elif rng == 2 and if kaeli_card_in_inventory == True:
        if taureaugamekaeli == True:
            show cartekaeli at upleft
            show croupier
            croupier "Kaeli Windleaf."
            croupier "I think she lives in Fissa ? She often hangs around anyway."
            player "I met her on my way here. Her cat is cute !"
            croupier "Anyway, 10 more points."
            $ game_points += 10
            $ taureaugamekaeli = False          
            hide cartekaeli with dissolve
            jump choice
        else:
            show cartekaeli at upleft
            show croupier
            croupier "Kaeli and Cassiopée card ! 10 additionnal point." 
            hide cartekaeli with dissolve
            jump choice
            
            ...
I know this solution basically does what I'm asking it to do, but that's not what I had in mind. Most playthroughs are now "choice menu => choice menu => choice menu => card => choice menu => ..." and I don't know how to fix it. :(

I saw in the documentation that you can also code a random.randint to be written like renpy.random.randing(candicecard, kaelicard,...) so I could use that instead of numbers but I don't know how to add more values to the random.randint variable after triggering an event.

So if everyone feels like sharing some knowledge, it would help me a lot. Thank you. :oops:

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

Re: Changing the random.randint values with variables ?

#2 Post by Ocelot »

Populate array only with cards (or card numbers) you want to be able to pull and use renpy.random.choice or, alternatively, use renpy.random.shuffle to shuffle your deck:

Code: Select all

default card_list = ["bad card", "good 1", "good 2", "good 3"]
# . . .

# can only choose from 4 basic cards
$ current_card = renpy.random.choice(card_list)
"and the card is... [current_card]!"

if "secret card 1" not in card_list:
    card_list.append("secret card 1")

# can now select secret card 1
$ current_card = renpy.random.choice(card_list)
"and the card is... [current_card]!"

# randomizes order of cards in list
$ renpy.random.shuffle(card_list)
$ card_n = 0
while card_n < len(card_list):
    $ current_card = card_list[card_n]
    "Card [card_n] is [current_card]!"
    $ card_n = card_n + 1
< < insert Rick Cook quote here > >

Gigan
Newbie
Posts: 21
Joined: Mon Jan 10, 2022 7:02 pm
Contact:

Re: Changing the random.randint values with variables ?

#3 Post by Gigan »

You make it sounds so simple ! x)
Thank you so, so much ! Will try it when I'm back home ! :)

Post Reply

Who is online

Users browsing this forum: Google [Bot], Rhapsy