How do I question: Random results from a menu selection

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
Evildumdum
Regular
Posts: 191
Joined: Sun Jan 18, 2015 8:49 am
Projects: ApoclypseZ
Contact:

How do I question: Random results from a menu selection

#1 Post by Evildumdum »

So i've just started using Ren'Py. Not doing too badly, though i've never really done any programming so its all completely new to me. I can do basic dialogue, image insertion and movement as well as menu's and such. The very basics. The next thing i want to try and achieve is a menu with options that result in randomly selected outcomes.

Example:

Situation is the character gets confronted.

Normal menu would be along the lines of:

Walk away - leads to walking away
Punch - leads to fight

What I would like to do is more along the lines of:

Walk away - 50% chance of being followed and 50% chance of getting away.
punch - 50% chance of knocking the other guy out and 50% chance of getting knocked out.


I've looked for a cookbook recepie to help me out and googled it, but i've not found anything I can make sense of. I'm hoping someone here might be able to help me. Please bear in mind i'm a complete beginner though. Thanks for any help :)

User avatar
xavimat
Eileen-Class Veteran
Posts: 1461
Joined: Sat Feb 25, 2012 8:45 pm
Completed: Yeshua, Jesus Life, Cops&Robbers
Projects: Fear&Love
Organization: Pilgrim Creations
Github: xavi-mat
itch: pilgrimcreations
Location: Spain
Discord: xavimat
Contact:

Re: How do I question: Random results from a menu selection

#2 Post by xavimat »

Let's separate the two questions here.
1. You say you can do a normal menu. Perfect.
2. The random question has nothing to do with menus. You can use a random function and the "if" statement.

Code: Select all

label try_to_walk_away:
    $ r = renpy.random.randint(1,2)  # This will set "r" to 1 or 2 randomly
    if r == 1:
        jump walk_away
    else:
        jump can_not_walk_away
Of course you can add this code INSIDE the menu (as you can add any other block of code there) but my point is that the random question has its own solution. Doesn't matter if you're using it with or without menus.
Last edited by xavimat on Sun Jan 18, 2015 10:07 am, edited 1 time in total.
Comunidad Ren'Py en español: ¡Únete a nuestro Discord!
Rhaier Kingdom A Ren'Py Multiplayer Adventure Visual Novel.
Cops&Robbers A two-player experiment | Fear&Love Why can't we say I love you?
Honest Critique (Avatar made with Chibi Maker by ~gen8)

User avatar
Evildumdum
Regular
Posts: 191
Joined: Sun Jan 18, 2015 8:49 am
Projects: ApoclypseZ
Contact:

Re: How do I question: Random results from a menu selection

#3 Post by Evildumdum »

Thank you, that little piece of code really helped me out.

User avatar
Evildumdum
Regular
Posts: 191
Joined: Sun Jan 18, 2015 8:49 am
Projects: ApoclypseZ
Contact:

Re: How do I question: Random results from a menu selection

#4 Post by Evildumdum »

I've experimented a little with it. Its seems that it can only make a choice between 1 and 2 each having an equal chance of being chosen. It's possible to use multiple layers of 50/50 chances to create a scenario where one outcome is more likely than another, but it's a bit of a pain and makes 1 in 10 chances or 3 or more outcomes needlessly complicated. You'll probably see what i was trying to do if i show you a bit of code i experimented with and failed. You'll have to pardon the way it's pasted. all the indentations are correct in the code, but i haven't worked out how to paste it like you have.

label skip1:
"Right, time for experimenting."
menu:
"Lets go ride a pig":
jump pig1

"Lets go pet a bunny rabbit":
jump bunny1

"Boring. Skip":
jump carry_on2

label pig1:
$ r = renpy.random.randint(1,2,3,4,5,6)
if r == 1,2,3:
"The pig ride is super fun."
jump carry_on2

if r == 4,5:
"The pig throws you to the ground and you get muddy"
jump dead_end

if r == 6:
"The pig kills you on the spot with its super nuzzle."
jump generic_bad_end


Needless to say that didn't work. I came up with a way to work it, but it's not ideal as you'll see below.

label skip1:
"Right, time for experimenting."
menu:
"Lets go ride a pig":
jump pig1

"Lets go pet a bunny rabbit":
jump bunny1

"Boring. Skip":
jump carry_on2

label pig1:
$ r = renpy.random.randint(1,2)
if r == 1:
"The pig ride is super fun."
jump carry_on2

if r == 2:
"The pig throws you to the ground and you get muddy"
$ r = renpy.random.randint(1,2)
if r == 1:
"The pig kills you on the spot with its super nuzzle."
jump dead_end

if r == 2:
"you're muddy and cold. Go home."
jump generic_bad_end

User avatar
PyTom
Ren'Py Creator
Posts: 16096
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: How do I question: Random results from a menu selection

#5 Post by PyTom »

I like renpy.random.random(), which returns a random number between 0 and 1. so you can have:

Code: Select all

$ r = renpy.random.random()
if r < 3.0 / 6.0:
    "The pig ride is super fun."
    jump carry_on2

elif r < 5.0 / 6.0:
    "The pig throws you to the ground and you get muddy"
    jump dead_end

else:
    "The pig kills you on the spot with its super nuzzle."
    jump generic_bad_end
I'm not sure VN players are a fan of randomness in general, especially random bad ends.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

User avatar
Evildumdum
Regular
Posts: 191
Joined: Sun Jan 18, 2015 8:49 am
Projects: ApoclypseZ
Contact:

Re: How do I question: Random results from a menu selection

#6 Post by Evildumdum »

Thank you, I'll try this one :)

As for random bad endings in VN's, it was only a random scenario to test the mechanics. I'm actually using ren'py because i want to make a game that is more like a visual strategy game. I'm still learning the basics, but random events would be a big part of it. The next step is learning how to programme character attached stats and combine them with the event probability or outcomes. Its going to get very complicated i'm sure, but I have to start somewhere lol.

User avatar
xavimat
Eileen-Class Veteran
Posts: 1461
Joined: Sat Feb 25, 2012 8:45 pm
Completed: Yeshua, Jesus Life, Cops&Robbers
Projects: Fear&Love
Organization: Pilgrim Creations
Github: xavi-mat
itch: pilgrimcreations
Location: Spain
Discord: xavimat
Contact:

Re: How do I question: Random results from a menu selection

#7 Post by xavimat »

I forgot to explain the function and to point to the doc: http://www.renpy.org/doc/html/other.html#renpy-random

We have
renpy.random.randint(a, b) -> returns a random (integer) number between 'a' and 'b'.
renpy.random.random()
renpy.random.choice(seq) -> choices an element in 'seq' (for example, from a list)
Comunidad Ren'Py en español: ¡Únete a nuestro Discord!
Rhaier Kingdom A Ren'Py Multiplayer Adventure Visual Novel.
Cops&Robbers A two-player experiment | Fear&Love Why can't we say I love you?
Honest Critique (Avatar made with Chibi Maker by ~gen8)

Post Reply

Who is online

Users browsing this forum: Google [Bot]