How to Use Persistent Variables When it Comes to PC Names

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
nyeowmi
Newbie
Posts: 5
Joined: Thu Oct 27, 2022 11:10 pm
Projects: Never Ending Love
Discord: nyeowmi#6969
Contact:

How to Use Persistent Variables When it Comes to PC Names

#1 Post by nyeowmi »

Hi! So, I'm very new to Ren'py and want to achieve a certain thing but I'm quite unsure how?

I've only seen people use persistent variables when it comes to flagging certain endings (ie, player has received a certain ending, persistent variable is switched to true, game remembers you had achieved that ending.) However, I wanted to know if it's possible to use persistent variables to remember pc names? Something along the lines of player enters a name and the game is able to remember that the player with that name has played before if they enter that same name again? Should I just use persistent variables like I use regular variables?

And, on a more complicated note, is it possible to bind endings or choices to that name? Say, the game would only remember that you've achieved those endings if you enter the same name but will pretend that you haven't if you've entered a different name? I feel this would be more complicated or impossible since I'm not sure you can store multiple names (ie, nicolas has achieved one ending, marie has achieved two endings, a completely new name has achieved zero endings) and have the game remember even if you play out of order (ie, you play as nicolas first and then replay, enter the name marie and then replay again, enter the name nicolas again and the game still remembers nicolas has two endings)

Hopefully I make complete sense? Thank you so much for your time!

User avatar
_ticlock_
Miko-Class Veteran
Posts: 910
Joined: Mon Oct 26, 2020 5:41 pm
Contact:

Re: How to Use Persistent Variables When it Comes to PC Names

#2 Post by _ticlock_ »

nyeowmi wrote: Wed Nov 02, 2022 12:27 pm
I believe you meant MC names, not PC names. You can implement everything you mentioned in your post.

I would suggest instead of remembering just a name, create something like a profile for the player. A player can select the previous profile or a new one.

For example, you can do something like the following:

Code: Select all

default persistent.players = []
default persistent.new_profile_id = 0
default player_profile = False
default player_name = ""
Screen to select profile:

Code: Select all

screen select_profile():
    side ("c r"):
        xfill True yfill True
        viewport id "select_profile":
            draggable True mousewheel True
            vbox:
                xfill True
                label "Select profile" xalign 0.5
                textbutton "Create new profile":
                    action [SetVariable("player_profile", None),
                            Return()]
                    xalign 0.5
                for profile in persistent.players:
                    $ player_name = profile["name"]
                    $ player_id = profile["id"]
                    textbutton "[player_name] - (id: [player_id])":
                        action [SetVariable("player_profile", profile),
                                Return()]
                        xalign 0.5
        vbar value YScrollValue("select_profile")
Possible start to select a profile or create new one:

Code: Select all

label start:
    call screen select_profile
    if player_profile == None:
        jump new_profile
    else:
        jump old_profile

label new_profile:
    $ player_name = renpy.input("What is your name?")
    $ player_name = player_name.strip()
    if player_name == "":
        "I am sorry, but I didn't get your name"
        jump new_profile
    python:
        player_profile = {
            "name" : player_name,
            "id": persistent.new_profile_id,
            "endings": set(),
            "total_games": 1
            }
        persistent.new_profile_id += 1
        persistent.players.append(player_profile)
    "Welcome, [player_name]!"

    jump new_game

label old_profile:
    python:
        player_name = player_profile["name"]
        player_profile["total_games"] = player_profile["total_games"] + 1
    "Welcome back, [player_name]!"
    jump new_game
    
For this example to work, you need to update player_profile variable from the persistent data when loading a save file:

Code: Select all

label after_load:
    python:
        if player_profile:
            flag = True
            for profile in persistent.players:
                if profile["id"] == player_profile["id"]:
                    player_profile = profile
                    flag = False
                    break

            if flag:
                # Restoring profile from save file
                player_name = profile["name"]
                player_id = profile["id"]
                persistent.players.append(player_profile)
    return

User avatar
_ticlock_
Miko-Class Veteran
Posts: 910
Joined: Mon Oct 26, 2020 5:41 pm
Contact:

Re: How to Use Persistent Variables When it Comes to PC Names

#3 Post by _ticlock_ »

nyeowmi wrote: Wed Nov 02, 2022 12:27 pm And, on a more complicated note, is it possible to bind endings or choices to that name? Say, the game would only remember that you've achieved those endings if you enter the same name but will pretend that you haven't if you've entered a different name?
Based on previous example:

Code: Select all

label bad_ending_1:
    if "bad_ending_1" in player_profile["endings"]:
        "You are not learning, aren't you?"
        return
    else:
        "You reached bad ending 1"
        $ player_profile["endings"].add("bad_ending_1")
        return

User avatar
nyeowmi
Newbie
Posts: 5
Joined: Thu Oct 27, 2022 11:10 pm
Projects: Never Ending Love
Discord: nyeowmi#6969
Contact:

Re: How to Use Persistent Variables When it Comes to PC Names

#4 Post by nyeowmi »

_ticlock_ wrote: Wed Nov 02, 2022 4:50 pm
nyeowmi wrote: Wed Nov 02, 2022 12:27 pm
I would suggest instead of remembering just a name, create something like a profile for the player. A player can select the previous profile or a new one.

For example, you can do something like the following:

Code: Select all

default persistent.players = []
default persistent.new_profile_id = 0
default player_profile = False
default player_name = ""
Screen to select profile:

Code: Select all

screen select_profile():
    side ("c r"):
        xfill True yfill True
        viewport id "select_profile":
            draggable True mousewheel True
            vbox:
                xfill True
                label "Select profile" xalign 0.5
                textbutton "Create new profile":
                    action [SetVariable("player_profile", None),
                            Return()]
                    xalign 0.5
                for profile in persistent.players:
                    $ player_name = profile["name"]
                    $ player_id = profile["id"]
                    textbutton "[player_name] - (id: [player_id])":
                        action [SetVariable("player_profile", profile),
                                Return()]
                        xalign 0.5
        vbar value YScrollValue("select_profile")
Thank you so much for your awesome answer! This is exactly what I was looking for. Is it possible to forgo the select profile screen and have the game just remember when the player inputs a name or is the profile select necessary?

User avatar
_ticlock_
Miko-Class Veteran
Posts: 910
Joined: Mon Oct 26, 2020 5:41 pm
Contact:

Re: How to Use Persistent Variables When it Comes to PC Names

#5 Post by _ticlock_ »

nyeowmi wrote: Wed Nov 02, 2022 9:45 pm Thank you so much for your awesome answer! This is exactly what I was looking for. Is it possible to forgo the select profile screen and have the game just remember when the player inputs a name or is the profile select necessary?
Sure. Note, that if two players input the same name the game assumes it is the same player.

In this case you just use name as profile id, and check if the profile with the same name exist. Something like this:

Code: Select all

label start:
    $ player_name = renpy.input("What is your name?")
    $ player_name = player_name.strip()
    if player_name == "":
        "I am sorry, but I didn't get your name"
        jump start
    python:
        #Look for existing name in profiles
        for profile in persistent.players:
            if profile["name"] == player_name:
                player_profile = profile
                break
    if player_profile:
        # Old player
        “Welcome back, [player_name]!"
    else:
        # New player
        python:      
            player_profile = {
                "name" : player_name,
                "endings": set(),
                "total_games": 1
                }
            persistent.players.append(player_profile)
            
        "Welcome, [player_name]!"

    jump new_game

Code: Select all

label after_load:
    python:
        if player_profile:
            flag = True
            for profile in persistent.players:
                if profile["name"] == player_profile["name"]:
                    player_profile = profile
                    flag = False
                    break

            if flag:
                # Restoring profile from save file
                player_name = profile["name"]
                persistent.players.append(player_profile)
    return
I rewrote the code, hopefully did not make any mistakes.

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

Re: How to Use Persistent Variables When it Comes to PC Names

#6 Post by Imperf3kt »

_ticlock_ wrote: Wed Nov 02, 2022 4:50 pm I believe you meant MC names, not PC names.

People call it different things, but there's no standard to work by. Either would work.
MC would mean Main Character while PC would mean Player Character (referring to the character the player is assuming the role of)
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

Post Reply

Who is online

Users browsing this forum: Google [Bot], Ocelot