image button opens a directory

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
Dharker
Regular
Posts: 99
Joined: Sun Sep 15, 2013 6:45 am
Contact:

image button opens a directory

#1 Post by Dharker »

Hi,

I am looking for a simple code that will open a directory in the game folder.

E.g. I have an image button where if you click on it, it goes to a website url.

Code: Select all

imagebutton auto "ui/bgselect/moviescene1a_%s.png" action [OpenURL("http://websiteurl.com"), Play("sound", "se/open.ogg")] hovered Play("sound", "se/select.ogg")
But what I would like is for instead of it opening a URL it opens a folder in the game directory.

Is this possible and what would the code be to replace the OPENURL code, and for it to automatically link to the game directory so i do not need to include the c: part as well. Just the folders after the /game/ part.

Thanks.

User avatar
SuperbowserX
Veteran
Posts: 270
Joined: Sat Jan 07, 2017 3:09 pm
Contact:

Re: image button opens a directory

#2 Post by SuperbowserX »

here's what I found :) viewtopic.php?f=8&t=4143

Dharker
Regular
Posts: 99
Joined: Sun Sep 15, 2013 6:45 am
Contact:

Re: image button opens a directory

#3 Post by Dharker »

Thxs, I was hoping to find something a little more 'safe' that might work on all platforms.
Ideally just similar to OPENURL but goes to a local folder, i don't really want it to open a file or exe. I just want it to open a folder within the game directory.

User avatar
Saltome
Veteran
Posts: 244
Joined: Sun Oct 26, 2014 1:07 pm
Deviantart: saltome
Contact:

Re: image button opens a directory

#4 Post by Saltome »

Well, hate to say it but there's no such thing as "work on all platforms".
You can use the above example to open a folder. And I added a small change to make sure it doesn't crash on other operating systems... I hope. Now if it really is important you may consider extending the functionality so it works on other platforms. But if you can avoid it, do so.
You can use this as a normal function in the labels, or as a screen action.

Code: Select all

init python:
    from os import startfile as osstartfile, name as osname
    def opendir(path = "C:"):
        if osname == "nt":
            osstartfile(path)
            
    @renpy.pure
    class OpenDir(Action, DictEquality):

        def __init__(self, path):
            self.path = path
            
        def __call__(self):
            opendir(self.path)
            
screen test:
    textbutton "OPEN" align(.5,.5) action OpenDir("C:\\")
    
label start:
    "start"
    "Regular function."
    $ opendir("C:\\")
    "Screen action."
    show screen test
    "end"
    
Deviant Art: Image
Discord: saltome
Itch: Phoenix Start

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

Re: image button opens a directory

#5 Post by xavimat »

That thread is very old.
I'm using this code and (I think) it works:

Code: Select all

label open_folder:

    python hide:
        import subprocess
        import sys
        import os
        import platform

        bonus = os.path.abspath(os.path.join(config.basedir, "FOLDER_NAME_IN_BASEDIR"))

        if sys.platform == "win32":
            os.startfile(bonus)
        elif platform.mac_ver()[0]:
            subprocess.Popen([ "open", bonus ])
        else:
            subprocess.Popen([ "xdg-open", bonus ])
(I got this here: viewtopic.php?f=8&t=10636 )
You could use also config.gamedir instead of config.basedir
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)

Post Reply

Who is online

Users browsing this forum: BBN_VN, Google [Bot]