Page 1 of 1

Can RenPy read the players data? (Name)

Posted: Fri Dec 15, 2017 8:30 am
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?

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

Posted: Fri Dec 15, 2017 8:45 am
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.

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

Posted: Mon Dec 18, 2017 10:18 am
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]."

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

Posted: Mon Dec 18, 2017 2:11 pm
by xavimat
Tested on Linux (Ubuntu 16.04), and it shows "None" as the user.

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

Posted: Mon Dec 18, 2017 2:29 pm
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'

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

Posted: Mon Jan 22, 2018 5:19 am
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?

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

Posted: Mon Jan 22, 2018 8:13 am
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] )

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

Posted: Mon Jan 22, 2018 11:25 am
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.

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

Posted: Wed Jan 24, 2018 5:25 am
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.

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

Posted: Fri Aug 17, 2018 7:20 pm
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]"