[SOLVED] How to randomly pick phrases, then change them

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
Pink09YT
Newbie
Posts: 5
Joined: Sat Aug 25, 2018 10:44 pm
Tumblr: Pink09YT
Deviantart: Pink09YT
Github: Pink09YT
Skype: Pink09YT
Contact:

[SOLVED] How to randomly pick phrases, then change them

#1 Post by Pink09YT »

My game is separated into chapters, so I created a fake loading screen to separate them. For the fun facts part of the loading screen, I need the game to randomly pick from the 9 facts and change them every 20 seconds. Screens aren't my strong point, so I don't really know how to go about doing that.

Code: Select all

screen loading():
    add gui.loading_screen_background

    text "Fun fact: [fact]":
        font "gui/fonts/Milkshake.ttf"
        size 48
        xpos 32
        ypos 516

    text "Chapter [CN]":
        font "gui/fonts/Milkshake.ttf"
        size 150
        color "#000000"
        xpos 687
        ypos 145

    imagebutton auto "gui/loading screen/continue_%s.png" action Return:
        xpos 1400
        ypos 945

    $ randomfact = renpy.random.randint(1,9)

    if randomfact == 1:
        python:
            fact = "Fact 1"

    if randomfact == 2:
        python:
            fact = "Fact 2"

    if randomfact == 3:
        python:
            fact = "Fact 3"

    if randomfact == 4:
        python:
            fact = "Fact 4"

    if randomfact == 5:
        python:
            fact = "Fact 5"

    if randomfact == 6:
        python:
            fact = "Fact 6"

    if randomfact == 7:
        python:
            fact = "Fact 7"

    if randomfact == 8:
        python:
            fact = "Fact 8"

    if randomfact == 9:
        python:
            fact = "Face 9"
Last edited by Pink09YT on Thu Jan 17, 2019 4:26 am, edited 3 times in total.

DannX
Regular
Posts: 99
Joined: Mon Mar 12, 2018 11:15 am
Contact:

Re: How to randomly pick phrases, then change them

#2 Post by DannX »

Try adding timer to the screen:

Code: Select all

screen loading():
    timer 20 repeat True action SetScreenVariable('randomfact', renpy.random.randint(1,9))
As a suggestion, you could also rewrite that using a list of facts, and renpy.random.choice, that way you're not limited to 9 facts (you can add all you want) without having to repeat all those python statements:

Code: Select all

default facts =['Fact 1',
                'Fact 2',
                'Fact 3',
                'Fact 4'
                # and so on
                ]

screen loading():
    
    $ randomfact = renpy.random.choice(facts)
    
    text "[randomfact]"
    
    timer 20 repeat True action SetScreenVariable('randomfact', renpy.random.choice(facts))

Pink09YT
Newbie
Posts: 5
Joined: Sat Aug 25, 2018 10:44 pm
Tumblr: Pink09YT
Deviantart: Pink09YT
Github: Pink09YT
Skype: Pink09YT
Contact:

Re: How to randomly pick phrases, then change them

#3 Post by Pink09YT »

I did what you said, but it's giving me an error.

Code: Select all

default facts = ['Fact 1',
                'Fact 2',
                'Fact 3',
                'Fact 4'
                # and so on
                ]

screen loading():
    add gui.loading_screen_background

    text "Fun fact: [randomfact]":
        font "gui/fonts/Milkshake.ttf"
        size 48
        xpos 32
        ypos 516

    text "Chapter [CN]":
        font "gui/fonts/Milkshake.ttf"
        size 150
        color "#000000"
        xpos 687
        ypos 145

    imagebutton auto "gui/loading screen/continue_%s.png" action Return:
        xpos 1400
        ypos 945

    $ randomfact = renpy.random.choice(facts)

    timer 20 repeat True action SetScreenVariable('randomfact', renpy.random.choice(facts))

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/Chapter 1-5.rpy", line 7, in script
    call screen loading with fade
  File "renpy/common/000statements.rpy", line 519, in execute_call_screen
    store._return = renpy.call_screen(name, *args, **kwargs)
  File "game/screens.rpy", line 92, in execute
    screen loading():
  File "game/screens.rpy", line 92, in execute
    screen loading():
  File "game/screens.rpy", line 95, in execute
    text "Fun fact: [randomfact]":
NameError: Name 'randomfact' is not defined.

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "game/Chapter 1-5.rpy", line 7, in script
    call screen loading with fade
  File "C:\Users\Student\Desktop\Programs\renpy-7.1.3-sdk\renpy\ast.py", line 1861, in execute
    self.call("execute")
  File "C:\Users\Student\Desktop\Programs\renpy-7.1.3-sdk\renpy\ast.py", line 1849, in call
    return renpy.statements.call(method, parsed, *args, **kwargs)
  File "C:\Users\Student\Desktop\Programs\renpy-7.1.3-sdk\renpy\statements.py", line 203, in call
    return method(parsed, *args, **kwargs)
  File "renpy/common/000statements.rpy", line 519, in execute_call_screen
    store._return = renpy.call_screen(name, *args, **kwargs)
  File "C:\Users\Student\Desktop\Programs\renpy-7.1.3-sdk\renpy\exports.py", line 2755, in call_screen
    rv = renpy.ui.interact(mouse="screen", type="screen", roll_forward=roll_forward)
  File "C:\Users\Student\Desktop\Programs\renpy-7.1.3-sdk\renpy\ui.py", line 289, in interact
    rv = renpy.game.interface.interact(roll_forward=roll_forward, **kwargs)
  File "C:\Users\Student\Desktop\Programs\renpy-7.1.3-sdk\renpy\display\core.py", line 2672, in interact
    repeat, rv = self.interact_core(preloads=preloads, trans_pause=trans_pause, **kwargs)
  File "C:\Users\Student\Desktop\Programs\renpy-7.1.3-sdk\renpy\display\core.py", line 3059, in interact_core
    root_widget.visit_all(lambda i : i.per_interact())
  File "C:\Users\Student\Desktop\Programs\renpy-7.1.3-sdk\renpy\display\core.py", line 531, in visit_all
    d.visit_all(callback, seen)
  File "C:\Users\Student\Desktop\Programs\renpy-7.1.3-sdk\renpy\display\core.py", line 531, in visit_all
    d.visit_all(callback, seen)
  File "C:\Users\Student\Desktop\Programs\renpy-7.1.3-sdk\renpy\display\core.py", line 531, in visit_all
    d.visit_all(callback, seen)
  File "C:\Users\Student\Desktop\Programs\renpy-7.1.3-sdk\renpy\display\core.py", line 531, in visit_all
    d.visit_all(callback, seen)
  File "C:\Users\Student\Desktop\Programs\renpy-7.1.3-sdk\renpy\display\screen.py", line 424, in visit_all
    callback(self)
  File "C:\Users\Student\Desktop\Programs\renpy-7.1.3-sdk\renpy\display\core.py", line 3059, in <lambda>
    root_widget.visit_all(lambda i : i.per_interact())
  File "C:\Users\Student\Desktop\Programs\renpy-7.1.3-sdk\renpy\display\screen.py", line 434, in per_interact
    self.update()
  File "C:\Users\Student\Desktop\Programs\renpy-7.1.3-sdk\renpy\display\screen.py", line 619, in update
    self.screen.function(**self.scope)
  File "game/screens.rpy", line 92, in execute
    screen loading():
  File "game/screens.rpy", line 92, in execute
    screen loading():
  File "game/screens.rpy", line 95, in execute
    text "Fun fact: [randomfact]":
  File "C:\Users\Student\Desktop\Programs\renpy-7.1.3-sdk\renpy\text\text.py", line 1423, in __init__
    self.set_text(text, scope, substitute)
  File "C:\Users\Student\Desktop\Programs\renpy-7.1.3-sdk\renpy\text\text.py", line 1542, in set_text
    i, did_sub = renpy.substitutions.substitute(i, scope, substitute)
  File "C:\Users\Student\Desktop\Programs\renpy-7.1.3-sdk\renpy\substitutions.py", line 244, in substitute
    s = formatter.vformat(s, (), kwargs)
  File "/home/tom/ab/x64lucid-deps/install/lib/python2.7/string.py", line 563, in vformat
  File "/home/tom/ab/x64lucid-deps/install/lib/python2.7/string.py", line 585, in _vformat
  File "/home/tom/ab/x64lucid-deps/install/lib/python2.7/string.py", line 646, in get_field
  File "/home/tom/ab/x64lucid-deps/install/lib/python2.7/string.py", line 605, in get_value
  File "C:\Users\Student\Desktop\Programs\renpy-7.1.3-sdk\renpy\substitutions.py", line 203, in __getitem__
    raise NameError("Name '{}' is not defined.".format(key))
NameError: Name 'randomfact' is not defined.

Windows-8-6.2.9200
Ren'Py 7.1.3.1092
Diaries - Choose Your Story {font=gui/fonts/playtime.ttf}{size=40}v1.0a{/font}{/size}
Thu Jan 17 14:16:47 2019

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3784
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: How to randomly pick phrases, then change them

#4 Post by Imperf3kt »

You need to place the $ randomfact part at the top of the screen, before you use it within the screen.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

Pink09YT
Newbie
Posts: 5
Joined: Sat Aug 25, 2018 10:44 pm
Tumblr: Pink09YT
Deviantart: Pink09YT
Github: Pink09YT
Skype: Pink09YT
Contact:

Re: How to randomly pick phrases, then change them

#5 Post by Pink09YT »

That fixed it, thank you!

Post Reply

Who is online

Users browsing this forum: No registered users