Can RenPy read the players data? (Name)

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
FantaFrisk
Newbie
Posts: 1
Joined: Fri Dec 15, 2017 8:22 am
Contact:

Can RenPy read the players data? (Name)

#1 Post by FantaFrisk »

Inspired by OneShot, Niko asks if your name is (PC USERNAME). I'm trying to figure out if I can do the same sort of thing in my game?

User avatar
TheJerminator15
Regular
Posts: 161
Joined: Fri Mar 18, 2016 2:37 pm
Completed: A Sedentary Fist
Projects: Manipulation, Switch Swap, Unnamed Project
itch: jamsandwich
Location: England
Contact:

Re: Can RenPy read the players data? (Name)

#2 Post by TheJerminator15 »

I don't know how to implement it myself, but it definitely is possible. Doki Doki literature Club is a very recent example of a Ren'Py game using that.
My Current Writing Project: viewtopic.php?f=47&t=37699
Manipulation Teaser Demo: https://drive.google.com/open?id=0BzJ4E ... zV6TWVaclk

User avatar
RicharDann
Veteran
Posts: 286
Joined: Thu Aug 31, 2017 11:47 am
Contact:

Re: Can RenPy read the players data? (Name)

#3 Post by RicharDann »

This seems to work on Windows at least:

Code: Select all

label start:

    python:
        import os

        user = os.environ.get('username')

    "Hello there, [user]."
The most important step is always the next one.

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: Can RenPy read the players data? (Name)

#4 Post by xavimat »

Tested on Linux (Ubuntu 16.04), and it shows "None" as the user.
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
Ocelot
Lemma-Class Veteran
Posts: 2384
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: Can RenPy read the players data? (Name)

#5 Post by Ocelot »

On Linux enviroment variable is named "user".

YOu can detect Windows by checking for os.name() == 'nt'. Linux and MacOS would be os.name() == 'posix'
< < insert Rick Cook quote here > >

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

Re: Can RenPy read the players data? (Name)

#6 Post by Imperf3kt »

Ocelot wrote: Mon Dec 18, 2017 2:29 pm On Linux enviroment variable is named "user".

YOu can detect Windows by checking for os.name() == 'nt'. Linux and MacOS would be os.name() == 'posix'
Could you elaborate on this? I only get errors.

I use the following, which works great on Windows, but on Linux (Debian), it still returns "None" as username.

Code: Select all

    ## using init doesn't appear to be a requirement, but it makes no difference if I leave it out.
    init python:
        import os
     
        for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
            player = os.environ.get(name)

    "Hello there, [player]."
    
Image

I found this if its related?
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

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Can RenPy read the players data? (Name)

#7 Post by Remix »

You have no if/else logic to break the loop once found... suggest:

Code: Select all

default player = 'Player'

init python:
    import os
    player = os.environ.get( 'USERNAME', 
                 os.environ.get( 'USER', 
                 os.environ.get( 'LNAME', 
                 os.environ.get( 'LOGNAME', 'Player' ))))

label start:
    "Hello there, [player]."
Python .get on dictionary/object = reference.get( attribute, [optional default if key not found] )
Frameworks & Scriptlets:

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2384
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: Can RenPy read the players data? (Name)

#8 Post by Ocelot »

Imperf3kt wrote: Mon Jan 22, 2018 5:19 am Could you elaborate on this? I only get errors.
Yes. I messed up and added brackets. os.name is a string, not function.
< < insert Rick Cook quote here > >

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

Re: Can RenPy read the players data? (Name)

#9 Post by Imperf3kt »

Remix wrote: Mon Jan 22, 2018 8:13 am You have no if/else logic to break the loop once found... suggest:

Code: Select all

default player = 'Player'

init python:
    import os
    player = os.environ.get( 'USERNAME', 
                 os.environ.get( 'USER', 
                 os.environ.get( 'LNAME', 
                 os.environ.get( 'LOGNAME', 'Player' ))))

label start:
    "Hello there, [player]."
Python .get on dictionary/object = reference.get( attribute, [optional default if key not found] )
Thanks, but that didn't do as expected.

I didn't particularly want to hijack the thread, but for anyone else who may stumble across this in the future (because stackexchange was proving useless) here's what I found works:

Code: Select all

init python:
    if os.name == 'nt':
        import os
        
        for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
            player = os.environ.get(name)
    elif os.name == 'posix':
        import os
        
        for user in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
            player = os.environ.get('USER')
This had me scratching my head for days because I had used lower case. Also, don't add quotes for Windows, it returns the name as None when you do that.
Also do not default the name "player" as that happens after the init takes place, so it overwrites whatever it should show with what you "default"ed it to.

I use this in a screen, but I see no reason it won't work in dialogue as well.
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

Neyunse
Newbie
Posts: 8
Joined: Fri Aug 17, 2018 7:08 pm
Projects: Roses Of Love
Organization: KagariSoft
Github: Neyunse
itch: KagariSoft
Location: Argentina
Contact:

Re: Can RenPy read the players data? (Name)

#10 Post by Neyunse »

to make it detect the name in windows in this way
sorry for my English

Code: Select all

default player = os.environ.get('username')
label start:

python:
    if os.name == 'nt':
        import os

        for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
            player = os.environ.get(name)
    elif os.name == 'posix':
        import os

        for user in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
            player = os.environ.get('USER')
            
 
 a "hello [player]"
 
----------------------------------------------------
My Twitter: @Neyunse
My Discord: ねゆんせ#7916
----------------------------------------------------
KagariSoft Twitter: @KagariSoft
My Discord Server

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot]