Page 1 of 1

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

Posted: Wed Jan 16, 2019 1:07 pm
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"

Re: How to randomly pick phrases, then change them

Posted: Wed Jan 16, 2019 2:17 pm
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))

Re: How to randomly pick phrases, then change them

Posted: Thu Jan 17, 2019 12:19 am
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

Re: How to randomly pick phrases, then change them

Posted: Thu Jan 17, 2019 3:04 am
by Imperf3kt
You need to place the $ randomfact part at the top of the screen, before you use it within the screen.

Re: How to randomly pick phrases, then change them

Posted: Thu Jan 17, 2019 4:26 am
by Pink09YT
That fixed it, thank you!