Saving Player Input

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
chasingthecosmos
Newbie
Posts: 3
Joined: Fri Jul 20, 2018 1:39 pm
Contact:

Saving Player Input

#1 Post by chasingthecosmos »

I have a game where the player is able to input their own name with this code:
$ player_name = renpy.input ("What is your name?")
$ player_name = player_name.strip()
I call the name in the script using %(player_name)s.

I also have a variable in the game where the player can gain "points" or "achievements" or whatever you want to call it. It's defined like this:
$ clues = 0
And then throughout the script I add to it with $ clues += 1.

Everything works great when I run the game up until I close the window to make edits. Then, all variables are lost. Is there a way to keep this from happening? For a while, it was working great. I could close the game and walk away for a few days, and when I came back the "player_name" and "clues" variables would still be set and I wouldn't have to start from the beginning. I don't know what I did, but somewhere along the way that stopped. I'm assuming that it has something to do with saving the game state, but I've been reading renpy tutorials for weeks and haven't been able to find an answer that works.
Does anybody out there have any suggestions?

User avatar
xavimat
Eileen-Class Veteran
Posts: 1460
Joined: Sat Feb 25, 2012 8:45 pm
Completed: Yeshua, Jesus Life, Cops&Robbers
Projects: Fear&Love, unknown
Organization: Pilgrim Creations
Github: xavi-mat
itch: pilgrimcreations
Location: Spain
Contact:

Re: Saving Player Input

#2 Post by xavimat »

define your variables with default outside any label and the saves will remember them.

Code: Select all

default player_name = ""
default clues = 0
In any case, you have to save your game.
Comunidad Ren'Py en español: ¡Únete a nuestro Discord!
Rhaier Kingdom A Ren'Py Multiplayer Adventure Visual Novel.
Cops&Robbers A two-player experiment | Fear&Love Why can't we say I love you?
Honest Critique (Avatar made with Chibi Maker by ~gen8)

chasingthecosmos
Newbie
Posts: 3
Joined: Fri Jul 20, 2018 1:39 pm
Contact:

Re: Saving Player Input

#3 Post by chasingthecosmos »

That works for the clues, but if I set a default player name, then that's the name that renpy displays instead of the player input. Is there a way for renpy to remember the player input name instead of falling back to a set default?

User avatar
xavimat
Eileen-Class Veteran
Posts: 1460
Joined: Sat Feb 25, 2012 8:45 pm
Completed: Yeshua, Jesus Life, Cops&Robbers
Projects: Fear&Love, unknown
Organization: Pilgrim Creations
Github: xavi-mat
itch: pilgrimcreations
Location: Spain
Contact:

Re: Saving Player Input

#4 Post by xavimat »

The problem must be elsewhere. Whatever the player enters, it will overwrite the default value.
When a new game is started, the variable has the default value. The renpy.input must be at the beginning of the game; that value won't be remembered in new games but it will if you save and load that game. Are you loading or clicking on "start"?
Comunidad Ren'Py en español: ¡Únete a nuestro Discord!
Rhaier Kingdom A Ren'Py Multiplayer Adventure Visual Novel.
Cops&Robbers A two-player experiment | Fear&Love Why can't we say I love you?
Honest Critique (Avatar made with Chibi Maker by ~gen8)

chasingthecosmos
Newbie
Posts: 3
Joined: Fri Jul 20, 2018 1:39 pm
Contact:

Re: Saving Player Input

#5 Post by chasingthecosmos »

I'm just clicking on "Start". When I was trying to load saved files earlier it was working, but I've messed with it quite a bit since then and haven't checked to see if it's still working lol.

User avatar
xavimat
Eileen-Class Veteran
Posts: 1460
Joined: Sat Feb 25, 2012 8:45 pm
Completed: Yeshua, Jesus Life, Cops&Robbers
Projects: Fear&Love, unknown
Organization: Pilgrim Creations
Github: xavi-mat
itch: pilgrimcreations
Location: Spain
Contact:

Re: Saving Player Input

#6 Post by xavimat »

If you click start, the game starts from zero; variables have their "default" value; the "player_name" will have a useful value only after you reach the renpy.input line and renpy asks you for a name. Any name you put on former games is irrelevant because the game is starting from zero. The same will happen to your players when they play the game.
If you want to reuse a value among different saves you need to use "persistent", but that's not what you need in this case.
Comunidad Ren'Py en español: ¡Únete a nuestro Discord!
Rhaier Kingdom A Ren'Py Multiplayer Adventure Visual Novel.
Cops&Robbers A two-player experiment | Fear&Love Why can't we say I love you?
Honest Critique (Avatar made with Chibi Maker by ~gen8)

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: Saving Player Input

#7 Post by trooper6 »

Here is working code that explains this. Put it in a fresh project.

Code: Select all

define pc = Character("[pc_name]")

default pc_name = "Unknown"

# The game starts here.

label start:

    scene black
    pc "The game begins. Who is speaking? Unknown! The name is unknown because
    that is the default and we haven't changed it yet. It will always be Unknown
    for any new game."
    pc "But let's change our name."

    $pc_name = renpy.input("What is my name?")
    $pc_name = pc_name.strip()

    pc "So my name is [pc_name]."
    pc "Intersting!"
    pc "Okay this is what I want you do do now. I want you do save the game at
    this point and then quit. After you quit, restart the game and load the save."

    pc "Are you back?"
    pc "You'll notice that my name is still [pc_name]!"
    pc "Because the variable is saved!"
    pc "In this playthrough, it will remain [pc_name] and won't go back to Unknown."

    pc "If you finish the game and want to play a brand new game with a new name
    for me, you can!"

    pc "Okay, the game if finished. Bye!"

    return
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

Post Reply

Who is online

Users browsing this forum: No registered users