[SOLVED]Check for file existence, need example

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
GoldenMagician
Newbie
Posts: 19
Joined: Mon Mar 19, 2018 12:44 pm
Contact:

[SOLVED]Check for file existence, need example

#1 Post by GoldenMagician »

Hello, again. I'm really newbie here. So... I saw in documentation this command: renpy.exists(filename). Can you give me an example?

Code: Select all

renpy.exists(name_here.txt)
if true:
secret_route_unlocked = true
if false:
pass
Well, something like that. Thanks for help.
Last edited by GoldenMagician on Sun Mar 25, 2018 11:55 pm, edited 2 times in total.

guiltyGG
Newbie
Posts: 13
Joined: Sat Aug 19, 2017 8:51 am
Contact:

Re: Check for file existence, need example

#2 Post by guiltyGG »

Just my two cents:

Add a 'name_here.txt' file to your '/game/' folder (where 'script.rpy' resides).

Code: Select all

init python:
    ## set-up objects
    secret_route_unlocked = False    

image bg = Solid('#000') ## create black background image
    
label start:
    scene bg ## show background
    
    python:
        ## test if the file path exists, setting the 'secret_route_unlocked' object if file exists
        if renpy.exists('name_here.txt'):
            secret_route_unlocked = True

    ## display result
    "Secret route unlocked: [secret_route_unlocked]"

    # game ends here
    return
HTH :)

GoldenMagician
Newbie
Posts: 19
Joined: Mon Mar 19, 2018 12:44 pm
Contact:

Re: Check for file existence, need example

#3 Post by GoldenMagician »

guiltyGG wrote: Sun Mar 25, 2018 9:07 am Just my two cents:

Add a 'name_here.txt' file to your '/game/' folder (where 'script.rpy' resides).

Code: Select all

init python:
    ## set-up objects
    secret_route_unlocked = False    

image bg = Solid('#000') ## create black background image
    
label start:
    scene bg ## show background
    
    python:
        ## test if the file path exists, setting the 'secret_route_unlocked' object if file exists
        if renpy.exists('name_here.txt'):
            secret_route_unlocked = True

    ## display result
    "Secret route unlocked: [secret_route_unlocked]"

    # game ends here
    return
HTH :)
Thanks! It's helped. But, if you can, say, how to write "else" to this command. When I write it, it just skipped, i guess. Or I must write if secret_route_unlocked = true etc?

guiltyGG
Newbie
Posts: 13
Joined: Sat Aug 19, 2017 8:51 am
Contact:

Re: Check for file existence, need example

#4 Post by guiltyGG »

Code: Select all

image bg = Solid('#000')
    
label start:
    scene bg
    
    python:
        ## test if the file path exists, setting the 'secret_route_unlocked' object if file exists
        if renpy.exists('name_here.txt'):
            secret_route_unlocked = True
        else:
            secret_route_unlocked = False

    ## display result
    if secret_route_unlocked == True:
        "The Secret Route is now unlocked ..."
    else:
        "No Secret Route for you ..."

    # game ends here
    return
That will test for True or False.

To test for multiple conditions:

Code: Select all

python:
    a = 32
    
    if a == 32:
        print "32"
    elif a == 12:
        print "12"
    else:
        print "'a' doesn't match any expected values!"
Watch out for '=' vs '=='.

'=' will assign a value to an object; whereas '==' tests for equality.

GoldenMagician
Newbie
Posts: 19
Joined: Mon Mar 19, 2018 12:44 pm
Contact:

Re: Check for file existence, need example

#5 Post by GoldenMagician »

Okay, thanks

User avatar
neometalero
Regular
Posts: 196
Joined: Sun Oct 23, 2016 3:51 am
Completed: My Dream Sport Dating Simulator, Attack Helicopter Dating Simulator
Projects: My Dream Sport Dating Simulator, Attack Helicopter Dating Simulator
Deviantart: neometalero
Contact:

Re: Check for file existence, need example

#6 Post by neometalero »

guiltyGG wrote: Sun Mar 25, 2018 9:07 am Just my two cents:

Add a 'name_here.txt' file to your '/game/' folder (where 'script.rpy' resides).

Code: Select all

init python:
    ## set-up objects
    secret_route_unlocked = False    

image bg = Solid('#000') ## create black background image
    
label start:
    scene bg ## show background
    
    python:
        ## test if the file path exists, setting the 'secret_route_unlocked' object if file exists
        if renpy.exists('name_here.txt'):
            secret_route_unlocked = True

    ## display result
    "Secret route unlocked: [secret_route_unlocked]"

    # game ends here
    return
HTH :)
Tks a lot! this saved me from a lot of headaches
Working on many weird narrative games at Curse Box Studios
Image
https://www.curseboxstudios.com/

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

Re: [SOLVED]Check for file existence, need example

#7 Post by Imperf3kt »

Just want to mention this feature is unsupported on mobile devices. It will work fine on PC though.
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
zmook
Veteran
Posts: 421
Joined: Wed Aug 26, 2020 6:44 pm
Contact:

Re: Check for file existence, need example

#8 Post by zmook »

neometalero wrote: Wed Jun 22, 2022 2:23 pm Tks a lot! this saved me from a lot of headaches
Current documentation says "You almost certainly want to use renpy.loadable() in preference to this function."
colin r
➔ if you're an artist and need a bit of help coding your game, feel free to send me a PM

Post Reply

Who is online

Users browsing this forum: No registered users