Calling Random 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
User avatar
Pinecone
Newbie
Posts: 16
Joined: Fri Jan 01, 2016 4:57 am
Projects: Cloaked Knight
itch: iridiangrey
Contact:

Calling Random Variables?

#1 Post by Pinecone »

I feel like I'm spamming the board but, I've been stumped on this for a while.
In this project I have a 'talk' mechanic, which you can click to gain relationship points, but I want it to pull randomly from a pool of topics? How would I go about this?

User avatar
noeinan
Eileen-Class Veteran
Posts: 1153
Joined: Sun Apr 04, 2010 10:10 pm
Projects: Ren'Py QuickStart, Crimson Rue
Organization: Statistically Unlikely Games
Deviantart: noeinan
Github: noeinan
Location: Washington State, USA
Contact:

Re: Calling Random Variables?

#2 Post by noeinan »

Ren'Py has a random number generator built in, which would be pretty useful for something like this. I have used it in one of my games to have several sets of herbs that will randomly generate on the screen.

Code: Select all

label forest_loop:
    $ renpy.pause()
    jump forest_loop
    
# I pause RenPy so that I can have a screen with buttons for players to click without worrying about the textbox.

label forest001_layout:
    $ forest001_spawn = renpy.random.randint(1, 5)
    
# This is the code that spawns the random number. Because this controls the spawn rate of my herbs in the first forest screen, I've called it forest001_spawn. This sets that variable to a random integer (whole number) between 1 and 5. You can change these numbers to suit your needs. (Ex. susan_topic to generate a number to help pick which topic Susan will talk about.)
    
label forest001:
    init python: 
        message = "message"
    $ in_forest001 = True
    
# The message variable connects to my inventory code, and I have a variable here that lets me check if the player is inside the first forest screen or not. This can be useful for events that will only take place inside the first forest screen, for example.
    
    scene bg forest001
    show screen forest001
    show screen basic_overlay
    
# Showing the background forest one, the forest screen which contains all the herbs (as image buttons), and my screen overlay with the calendar date, inventory button, etc. 
    
    "Your random number is [forest001_spawn]."
    jump forest_loop
    
# I have a line of dialogue here for testing purposes. The brackets will print out the random number generated by the variable, that way I can make sure it's working properly. The loop is what keeps RenPy locked on the forest screen until the player clicks on a button. 
After that, you just use simple if statements to determine what happens with different random numbers. Since mine is a screen, this is what I did:

Code: Select all

screen forest001:
    tag menu2
    
    if forest001_spawn == 1:
# If my random number is one then it will show the image button below.
    
        if herb_book3 and forest001_poppy_col: 
#These are other variables I'm using, so herbs only show up if the player has read the book that identifies that plant, and they haven't already collected that plant from this screen. 

            imagebutton:
                idle "gui/inv/herb021_idle.png"
                hover "gui/inv/herb021_hover.png"
                focus_mask True
                clicked [ SetVariable("forest001_poppy_col", False), Jump("forest001_poppy") ]
                xpos 300 ypos 500
                xanchor 0 yanchor 0
I don't know if you want to have a screen with, like, little word bubbles above the person who wants to talk, and each word bubble shows a word describing the topic they want to talk about, or if you just want to make it so that when you talk to a person they discuss a random topic. Generally speaking, it's pretty straightforward either way. You just need to generate a random number, set that to a variable, and then use if statements so that the topic will only be displayed if they have the right random number.

You can also add extra variables, like I did, so that the character will only talk about that topic one time-- or you can have a string of conversations on the same topic, but they will only show up if you've read the conversations before that. (Ex. if the topic is "my cat" and you say "if the random number is 3 jump to the label "my_cat_1" which has dialogue. At the end of the dialogue, set the "my_cat_1_read" variable to True. If they go through the conversations again, you can have it check the random number, then if you get 3 it does another check to see if "my_cat_1_read" is true. If False, it shows that dialogue, if True, it displays "my_cat_2" and so on and so forth.)
Image

Image
Image

Post Reply

Who is online

Users browsing this forum: fufuffiero, Sugar_and_rice