How to Use Persistent Variables When it Comes to PC Names
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.
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.
- 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
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!
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!
Re: How to Use Persistent Variables When it Comes to PC Names
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 = ""
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")
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
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
Re: How to Use Persistent Variables When it Comes to PC Names
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
- 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
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?_ticlock_ wrote: ↑Wed Nov 02, 2022 4:50 pmI 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:Screen to select profile:Code: Select all
default persistent.players = [] default persistent.new_profile_id = 0 default player_profile = False default player_name = ""
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")
Re: How to Use Persistent Variables When it Comes to PC Names
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
- 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
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
pro·gram·mer (noun) An organism capable of converting caffeine into code.
Current project: GGD Mentor
Who is online
Users browsing this forum: Google [Bot]