[Solved] Persistent variables change text in a screen

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
mjshi
Regular
Posts: 179
Joined: Wed Mar 13, 2013 9:55 pm
Completed: MazeSite01, Ponderings of Time
Contact:

[Solved] Persistent variables change text in a screen

#1 Post by mjshi » Thu Dec 25, 2014 4:41 pm

I was thinking it might be accomplished by
1. If statements inside dialogue blocks (probably impossible)
2. Screen language (somehow)
3. Actual python coding (???)

What I'm trying to do is create an "endings" screen, where certain words in a block of text changes and/or changes color depending if the ending was achieved or not.

I looked it up on this forum, and got this thread:

Code: Select all

screen endings:
    vbox: # Or viewport or anything else you like
        for ending in endings_list:
            if ending in persistant.ending_set:
                text("{color=[red]}%s"%ending) # assumes that you have colors as variables and strings representing the endings.
            else:
                text("color=[green]%s"%ending) # Not yet achieved, same conditions as above apply...
.. except the entire thread discussion was way over my head xD
What is required to make a list again? Something like $ endings_list = [item1, item2, item3]?

So tl;dr Endings list in a text block, style properties able to be affected by persistent variables.
Last edited by mjshi on Fri Dec 26, 2014 12:52 pm, edited 1 time in total.

Shaples
Regular
Posts: 84
Joined: Sat Sep 27, 2014 2:08 pm
Completed: Christmas Sweaters
Projects: White Lie, Tropichu!
Contact:

Re: (endings list) Persistent variables change text in a scr

#2 Post by Shaples » Thu Dec 25, 2014 11:18 pm

I actually just spent a while beating my head against this exact same problem. I'm no coding pro, and someone else will probably have a better answer for you, but I made it work using ArachneJericho's solution in this thread.

To make it work, in your script file you need

Code: Select all

init python:

    # If persistent.endings is None (the first pass through the game), then make it a set.
    if persistent.endings is None:
        persistent.endings = set()
which, to my understanding, basically gives your end flags a place to go. Then, for each of your endings, you need to add a persistent flag alike-so:

Code: Select all

        $ persistent.endings.add("Name Of Ending")
Then, in your screens.rpy, you need to add this block:

Code: Select all

screen endings:
    tag menu
    use navigation

    $ all_endings = [(1, "Ending 1 Name"), (2, "Ending 2 Name")]
### you can put in as many endings as you need to, just keep this same format (#, "Name"),

    frame:
        style_group "pref"

    vbox:
##I added these three lines of spacing and a title so the text would be centered and spaced out, mostly because my game had a high resolution and not a ton of endings, so it looked weird all squished in the upper left corner
        spacing 10
        xalign 0.5
        yalign 0.5
        text("Unlocked Endings")
###The really important part is down here vv
        for ending in all_endings:
            $ ending_number = ending[0]
            $ ending_name = ending[1]
            $ ending_string = "% 2d. ---------------------------------" % (ending_number,)
            
            if ending_name in persistent.endings:
                $ ending_string = "% 2d. %s" % (ending_number, ending_name)
            text ending_string
The bit at the bottom basically takes all your information (the endings the player has gotten, the ending number, and the ending name) and puts them all in the proper order in the proper place. It's a really basic, simple text formatted list (with a #---------------- for endings the player hasn't gotten yet), but it gets the job done. Oh, and you also need to add a button to navigate to this screen in the first place. I just added an extra button to the main menu:

Code: Select all

        textbutton _("Start Game") action Start()
        textbutton _("Load Game") action ShowMenu("load")
        textbutton _("Preferences") action ShowMenu("preferences")
        textbutton _("Endings") action ShowMenu("endings")
##^^ Like this. It will direct to "screen endings:"
        textbutton _("Help") action Help()
        textbutton _("Quit") action Quit(confirm=False)
Hope this helps!

User avatar
mjshi
Regular
Posts: 179
Joined: Wed Mar 13, 2013 9:55 pm
Completed: MazeSite01, Ponderings of Time
Contact:

Re: (endings list) Persistent variables change text in a scr

#3 Post by mjshi » Fri Dec 26, 2014 12:38 pm

Works like a charm! Thank you ^_^

One last question-- do you know of any way to detect if a certain ending has been achieved? (i.e. if a certain ending exists in the all_endings array)
I'd like this feature as an added bonus for the player-- but if


Nevermind xD Found out how to do it.

Code: Select all

  $ persistent.endings.add("Ending 1")
  if "Ending 1" in persistent.endings:
    "Hurrah!"
Again, thank you so much!

Shaples
Regular
Posts: 84
Joined: Sat Sep 27, 2014 2:08 pm
Completed: Christmas Sweaters
Projects: White Lie, Tropichu!
Contact:

Re: [Solved] Persistent variables change text in a screen

#4 Post by Shaples » Fri Dec 26, 2014 5:24 pm

No problem! Glad it worked for you :D

Post Reply

Who is online

Users browsing this forum: No registered users