Choice statements with weights

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
CatNip23
Newbie
Posts: 9
Joined: Wed Jul 29, 2020 12:16 pm
Contact:

Choice statements with weights

#1 Post by CatNip23 »

Hello, I would like to show some specific ATL animations but I'm having some problems.

The choice statement chooses block with equal probabilites, but I would like to have some weights when choosing, like this:

Code: Select all

image anim1:
	choice if renpy.random.randint(1, 10) > 3:
		"animation1.png"
        	0.07
        	"animation2.png"
        	0.07
        	"animation3.png"
        	0.07
        choice else:
        	"animation4.png"
        	0.07
        	"animation5.png"
        	0.07
        	"animation6.png"
        	0.07
        	
        repeat
       	
That way, the first block would happen 70% of the time, and the second 30%. How would I go about something like this?

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

Re: Choice statements with weights

#2 Post by Ocelot »

CatNip23 wrote: Mon Jun 27, 2022 12:14 pm The choice statement chooses block with equal probabilites
The documentation disagrees:
https://www.renpy.org/doc/html/atl.html ... -statement

Code: Select all

atl_choice ::=  "choice" (simple_expression)? ":"
                    atl_block
Choice statements are greedily grouped into a choice set when more than one choice statement appears consecutively in a block. If the simple_expression is supplied, it is a floating-point weight given to that block, otherwise 1.0 is assumed.
< < insert Rick Cook quote here > >

CatNip23
Newbie
Posts: 9
Joined: Wed Jul 29, 2020 12:16 pm
Contact:

Re: Choice statements with weights

#3 Post by CatNip23 »

It's not clear to me how I would use it. I tried using:

Code: Select all

image anim1:
	atl_choice ::=  "choice" (0.7)? ":"
		"animation1.png"
        	0.07
        	"animation2.png"
        	0.07
        	"animation3.png"
        	0.07
        atl_choice ::=  "choice" (0.3)? ":"
        	"animation4.png"
        	0.07
        	"animation5.png"
        	0.07
        	"animation6.png"
        	0.07
        	
        repeat
But it didn't work

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

Re: Choice statements with weights

#4 Post by Ocelot »

It is a Backus–Naur form of defining syntax. It says, that atl_choice statement consist of
* literal word "choice" (without quotes)
* optional simple_expression (which can be a number, variable, etc...)
* literal ":" (semicolon without quotes)
* indented atl_block

So you can write it like:

Code: Select all

choice 7:
    pass
choice 3:
    pass
Notice how the only difference between usual choice statement and this is a single expression between "choice" and ":"
< < insert Rick Cook quote here > >

CatNip23
Newbie
Posts: 9
Joined: Wed Jul 29, 2020 12:16 pm
Contact:

Re: Choice statements with weights

#5 Post by CatNip23 »

Oh, it's a lot simpler than I thought. Thanks!

Post Reply

Who is online

Users browsing this forum: apocolocyntose, DewyNebula