How to access scenes randomly?

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
Rosstin
Veteran
Posts: 368
Joined: Mon Jan 31, 2011 5:43 pm
Completed: Rex Rocket, Kitty Love, King's Ascent
Projects: Road Redemption, Queen At Arms
Organization: Aqualuft Games
Contact:

How to access scenes randomly?

#1 Post by Rosstin » Fri Jan 29, 2016 1:27 pm

I want to build a system where a selection of scenes is randomly polled and then a random scene is served to the player. This will consist of hundreds of scenes, so I don't want to manually code a massive menu that is hundreds of lines long or something.

I also want to put multiple of these random scene possibilities into a menu and have a string that gives a "look ahead" hint. Like-- the west door is icy. You hear the sound of goblins behind the north door. The east door is warm to the touch and gives you a feeling of unease. Etc.

I know how I'd build this in other languages, but not Renpy. In another language, I would have an array of references to Scene objects. I'd manipulate those with some code, populating hint Strings, and setting up the jumps to move to the appropriate scenes.

Any tips on how this could be done in Renpy?
Image

User avatar
Rosstin
Veteran
Posts: 368
Joined: Mon Jan 31, 2011 5:43 pm
Completed: Rex Rocket, Kitty Love, King's Ascent
Projects: Road Redemption, Queen At Arms
Organization: Aqualuft Games
Contact:

Re: How to access scenes randomly?

#2 Post by Rosstin » Fri Jan 29, 2016 2:06 pm

This looks like it might be what I need:
http://www.renpy.org/doc/html/label.html

Maybe I can write the framework for this system in Python and use a function like this one:

"renpy.jump_out_of_context(label)
Causes control to leave the current context, and then to be transferred in the parent context to the given label"
Image

User avatar
Rosstin
Veteran
Posts: 368
Joined: Mon Jan 31, 2011 5:43 pm
Completed: Rex Rocket, Kitty Love, King's Ascent
Projects: Road Redemption, Queen At Arms
Organization: Aqualuft Games
Contact:

Re: How to access scenes randomly?

#3 Post by Rosstin » Fri Jan 29, 2016 2:35 pm

Anyone have an example of renpy.jump_out_of_context?

I can't get this to work.

OK-- $renpy.jump("c01")
works
Image

User avatar
Rosstin
Veteran
Posts: 368
Joined: Mon Jan 31, 2011 5:43 pm
Completed: Rex Rocket, Kitty Love, King's Ascent
Projects: Road Redemption, Queen At Arms
Organization: Aqualuft Games
Contact:

Re: How to access scenes randomly?

#4 Post by Rosstin » Fri Jan 29, 2016 2:40 pm

i can use an array of specially defined objects that have lookahead strings
Image

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: How to access scenes randomly?

#5 Post by xela » Sat Jan 30, 2016 10:38 am

Rosstin wrote:i can use an array of specially defined objects that have lookahead strings
Good answer to your OP :)

You can quite possible do this with less code in Python that in most other languages as well.
Like what we're doing? Support us at:
Image

User avatar
Belgerum
Regular
Posts: 110
Joined: Thu Nov 06, 2014 12:24 am
Skype: belgerum09
Soundcloud: Belgerum
itch: Belgerum
Contact:

Re: How to access scenes randomly?

#6 Post by Belgerum » Mon Feb 01, 2016 7:03 pm

try something like this:

Code: Select all

#this goes above the start label. This imports the python randomizing features.
init:
    $ import random




label start:
    
# Here, we make a list of all the environments you would like to be able to randomly have.

$ label_list = ["ice_cave", "lava_pit", "goblins", "library", "dungeon"]

# This command chooses a random label name from the set, "label_list" and sets "door1" to it.
$ door1 = random.choice(label_list)

# This command removes the chosen label name from label_list, so that we don't randomly choose the same place twice.
$ label_list.remove(door1)

$ door2 = random.choice(label_list)
$ label_list.remove(door2)

$ door3 = random.choice(label_list)
$ label_list.remove(door3)

$ door4 = random.choice(label_list)


label four_doors:

"I am in a room with four doors."

menu:
    "What will I do?"
    
    "Open North door":
        "I open the door and walk through..."
        $ renpy.jump(door1)
    "Check North Door":
        "I check the northern door..."
        call check_door(door1, "north")
        jump four_doors
        
    "Open South door":
        "I open the door and walk through..."
        $ renpy.jump(door2)
    "Check South Door":
        "I check the southern door..."
        call check_door(door2,"south")
        jump four_doors
        
    "Open East door":
        "I open the door and walk through..."
        $ renpy.jump(door3)
    "Check East Door":
        "I check the eastern door..."
        call check_door(door3, "east")
        jump four_doors
        
    "Open West door":
        "I open the door and walk through..."
        $ renpy.jump(door4)
    "Check West Door":
        "I check the western door..."
        call check_door(door4, "west")
        jump four_doors


# define each of the labels in label_list. this can be done in a different .rpy file, or just below the script.

label ice_cave:
"I find myself in an icy cavern."
return

label lava_pit:
"I find myself in a volcano, full of lava."
return

label goblins:
"There are goblins everywhere!"
return

label library:
"I find a library with lots of books."
return

label dungeon:
"I find myself in a stone prison, with many locked cells."
return


# define a label for checking the doors, with a different outcome depending on the door's contents.

label check_door(door_checked, direction):
if door_checked == "ice_cave":
    "The [direction] door is freezing cold!"
elif door_checked == "lava_pit":
    "The [direction] door is burning hot!"
elif door_checked == "goblins":
    "There is a bad smell coming from the [direction] door, and the sound of lots of tiny feet."
elif door_checked == "library":
    "The [direction] door gives off dust, as if it hasn't been opened in decades."
elif door_checked == "dungeon":
    "The [direction] door is made of stone, and seems oddly sturdy."
return
Or if that's not what you're after...

Code: Select all

#this goes above the start label. This imports the python randomizing features.
init:
    $ import random




label start:

# Here, we make a list of all the labels you would like to be able to randomly jump to.

$ label_list = ["store", "walk", "movies", "book"]

"I leave home."

# This command makes sure the player cannot just rollback if the scene randomly granted is not their preferred choice.
$ renpy.block_rollback()

# This command chooses a random label name from the set, "label_list" and sets "random_jump_target" to it.
$ random_jump_target = random.choice(label_list)

# This command removes the chosen label name from label_list, so that we don't randomly choose the same scene twice.
$ label_list.remove(random_jump_target)

# This command calls the label named after the string taken from label_list.
$ renpy.call(random_jump_target)

"After that..."

$ renpy.block_rollback()
$ random_jump_target = random.choice(label_list)
$ label_list.remove(random_jump_target)
$ renpy.call(random_jump_target)

"Yet afterword..."

$ renpy.block_rollback()
$ random_jump_target = random.choice(label_list)
$ label_list.remove(random_jump_target)
$ renpy.call(random_jump_target)

"And finally..."

$ renpy.block_rollback()
$ random_jump_target = random.choice(label_list)
$ label_list.remove(random_jump_target)
$ renpy.call(random_jump_target)

"I return home, since it's getting late."

# if you want a different list of scenes form the first, make another list above, or just make a short list with in-line code, like such.

$ label_list2 = ["sleep", "video_game", "writing"]

$ renpy.block_rollback()
$ random_jump_target = random.choice(label_list2)
$ renpy.call(random_jump_target)

"What an exciting day!"

return




# define each of the labels in label_list. this can be done in a different .rpy file, or just below the script.

label store:
"I go to the store."
return

label walk:
"I go for a walk."
return

label movies:
"I go to the movies."
return

label book:
"I read a book."
return

label sleep:
"I sleep all night."
return

label video_game:
"I play a lot of video games, then go to bed."
return

label writing:
"I write a visual novel, staying up all night to finish it."
return


Post Reply

Who is online

Users browsing this forum: enaielei