Randomize Scenes

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
Head_Phones
Regular
Posts: 65
Joined: Sun Oct 27, 2013 11:02 am
Projects: Clichéd
Location: Singapore
Contact:

Randomize Scenes

#1 Post by Head_Phones »

Hello!

I would like some help on randomizing scenes in a game of mine. Basically, I might have 5 scenes, a-e, and like at certain points in the story I want to just throw out these scenes, sort of like random events. Does that make sense? But I'm not sure how to go about with that.
'You may be a cunning linguist, but I am a master debater'
~Austin Powers



Profile pic drawn by the wonderful akemicchi

|| Honest Critique ||

User avatar
Milkymalk
Miko-Class Veteran
Posts: 753
Joined: Wed Nov 23, 2011 5:30 pm
Completed: Don't Look (AGS game)
Projects: KANPEKI! ★Perfect Play★
Organization: Crappy White Wings
Location: Germany
Contact:

Re: Randomize Scenes

#2 Post by Milkymalk »

You could check a random value after every scene and if it's a hit (say, a 0 when it's a random value from 0 to 19 for a 5% chance), you call a random label:

Code: Select all

$ check = int(20*renpy.random.random())    # creates a random number between 0 and 19
if check == 0:
    $ destination = int(5*rempy.random.random())    # creates a random number between 0 and 4
    if destination == 0:
        call random_event_0
    elif destination == 1:
        call random_event_1
    elif destination == 2:
        call random_event_2
    elif destination == 3:
        call random_event_3
    elif destination == 4:
        call random_event_4
Insert that code whenever there's a chance that a random event could occur. Better yet, put it in a label block by itself and call it instead. Then in the event, use the return statement to return to the normal game flow.

If you want each event to only be called once ever, you have to do this instead (you have to declare the "uncalled" variables as True first or you will get an error, best to do this is in an init block):

Code: Select all

    if destination == 0 and event_0_uncalled:
        $ event_0_uncalled = False
        call random_event_0
    elif destination == 1 and event_0_uncalled:
    # etc.
There are more elegant ways to do this, but the basic principle should be clear.

Beware, I'm writing this from my head and there MIGHT be errors. Especially with the use of the int() function.
Crappy White Wings (currently quite inactive)
Working on: KANPEKI!
(On Hold: New Eden, Imperial Sea, Pure Light)

User avatar
Head_Phones
Regular
Posts: 65
Joined: Sun Oct 27, 2013 11:02 am
Projects: Clichéd
Location: Singapore
Contact:

Re: Randomize Scenes

#3 Post by Head_Phones »

thank you so much for your help!

I'm very sorry but I don't quite understand the bottom portion.

So say I have ten questions and a couple have been used but I don't want the questions to repeat how would I go about doing that?
'You may be a cunning linguist, but I am a master debater'
~Austin Powers



Profile pic drawn by the wonderful akemicchi

|| Honest Critique ||

User avatar
Milkymalk
Miko-Class Veteran
Posts: 753
Joined: Wed Nov 23, 2011 5:30 pm
Completed: Don't Look (AGS game)
Projects: KANPEKI! ★Perfect Play★
Organization: Crappy White Wings
Location: Germany
Contact:

Re: Randomize Scenes

#4 Post by Milkymalk »

I don't understand what you mean by "questions".

The bottom portion checks a variable whenever a random scene is to be played. The variable stores if the scene has been played yet. If the scene has been played already, don't play it again. If it has not been played yet, change the variable and play the scene.

The problem with this method (that I just remembered) is that the more random scenes have been played, the chance to get a random scene at all becomes smaller.

Another possibility that's probably better:

Make a python list:

Code: Select all

init python:
    nrs = 10                                     # number of random scenes
    random_scenes = range(nrs)          # creates a list containing the numbers 0 to (nrs minus 1)
Put this somewhere inside your script:

Code: Select all

label play_random_scene(prob):
    python:
        pick = -1                                   # so that, after the list is empty, the last scene won't be played over and over
        if len(random_scenes) > 0:             # make sure there are scenes left before picking
            check = int(100*renpy.random.random())    # creates a random number between 0 and 99
            if check < prob:                          # if the number is low enough, play a random scene! (prob)% chance
                pick = random_scenes[renpy.random.random()*len(random_scenes)]    # pick a random one from the list
                del random_scenes[pick]                # delete the picked number from the list so it will not get picked again

    if pick == 0:
        call random_event_0   # adjust these to your own label names
    elif pick == 1:
        call random_event_1
    # and so on
    return
When you want to check if a random scene should be played, you can just:

Code: Select all

call play_random_scene(25)
25 here is the probability in % that a random scene from the list will be played. You can change this number to whatever you like. 100 means a scene will be played no matter what.

This is all written from memory, if there are errors please ask me again.
Crappy White Wings (currently quite inactive)
Working on: KANPEKI!
(On Hold: New Eden, Imperial Sea, Pure Light)

Post Reply

Who is online

Users browsing this forum: Kocker