Page 1 of 1

[SOLVED]Check for file existence, need example

Posted: Sat Mar 24, 2018 8:32 pm
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.

Re: Check for file existence, need example

Posted: Sun Mar 25, 2018 9:07 am
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 :)

Re: Check for file existence, need example

Posted: Sun Mar 25, 2018 12:55 pm
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?

Re: Check for file existence, need example

Posted: Sun Mar 25, 2018 2:00 pm
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.

Re: Check for file existence, need example

Posted: Sun Mar 25, 2018 11:54 pm
by GoldenMagician
Okay, thanks

Re: Check for file existence, need example

Posted: Wed Jun 22, 2022 2:23 pm
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

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

Posted: Wed Jun 22, 2022 11:43 pm
by Imperf3kt
Just want to mention this feature is unsupported on mobile devices. It will work fine on PC though.

Re: Check for file existence, need example

Posted: Thu Jun 23, 2022 8:57 am
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."