Using an 'if' statement to check for a file?

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
User avatar
Techmill
Newbie
Posts: 11
Joined: Sun Jul 21, 2013 10:39 am
Location: England
Contact:

Using an 'if' statement to check for a file?

#1 Post by Techmill »

I have an idea for a visual novel, but it's sort of consists of short episodic installments that I want to release gradually. Then I thought instead of releasing lots of seperate games, it would be a lot easier if these chapters could be an 'add-ons' to the initial release, for ease-of-use for players over anything.

In my head I had this idea that these additional chapters could be released as extra '.rpy' script files, that could simply dragged and dropped into the existing game folder. And then (Since I already know how many chapters it's going to be), in the initial game script, have a basic menu displaying menu options that jump to the chapters, but only 'if' if the corresponding script for it is in the game folder.

It a was perfect idea! Flawless!... Then I realised I don't even know if that's possible! :lol:
... Is it? To add an 'if' statement to check if a file is in the game folder or not?
Or is there some other to achieve this? (Less complex the possible!)

Thanks in advance!

Xipomus
Newbie
Posts: 8
Joined: Sun Jan 20, 2013 7:24 pm
Contact:

Re: Using an 'if' statement to check for a file?

#2 Post by Xipomus »

As far as I know, Renpy reads all rpy files in the game folder automatically.

Probably easiest would be the make a dummy rpy file for the new episodes. And replace those when the new content comes out.

On the dummy rpy you could place a teaser for that episode and link to it from the main menu.

Or even chance the menu buttons when a new release was installed. With change the menu button to a difference pic / text

User avatar
Techmill
Newbie
Posts: 11
Joined: Sun Jul 21, 2013 10:39 am
Location: England
Contact:

Re: Using an 'if' statement to check for a file?

#3 Post by Techmill »

Now there's an idea :D
Your way sounds a lot easier too... God forbid I make things too easy for myself XD
Thanks for the help, sorry about the delayed reply :)

Levrex
Veteran
Posts: 280
Joined: Mon Jun 18, 2012 12:16 pm
Contact:

Re: Using an 'if' statement to check for a file?

#4 Post by Levrex »

You can actually use renpy.loadable function to check for existing files.
http://renpy.org/wiki/renpy/doc/referen ... y.loadable
If your question is solved, please add [Solved] to theme's name by editing its first post, so that the helpful guys out there wouldn't mistakenly think the problem is still unanswered and waste their time.

Elmiwisa
Veteran
Posts: 476
Joined: Sun Jul 21, 2013 8:08 am
Contact:

Re: Using an 'if' statement to check for a file?

#5 Post by Elmiwisa »

Levrex's solution answer your original concern, but may I suggest something else completely? :wink:
Now let's say that for some reason, you do not want to alter the script file already there when updating, only adding new one. Now there are certain problem with your idea:
-You lost flexibility. Like it or not, you will still have to put a code somewhere among the original files (ie. those that you do not want to be modified) that check to see whether the file corresponding to each chapter are there, and present the menu based on what is there, that presumable also show certain basic fact about the chapter, such as its name. This will commit you to: (a) the name of the script file; (b) the name of the chapter; and (c) the name of the label you need to jump to to start that chapter. :cry:
-You lost control over potential problem cause by updating Ren'Py. This is because you probably don't want to publish the script file to the player, so you will send them a built and obfuscated file instead. The file name will at least have different extension, and it is possible that this choice of extension will change in newer version of Ren'Py. While there are way to deal with this issue too, I suggest that this is still a bad idea, since this method is not going to be used by anybody, so if say for example, if future Ren'Py update think that it might need to change the file name for the purpose of easy porting to a new platform for example, it will make that change. :cry:
-You cannot add more chapters than you already planned. :|

Hence I suggest using a different method altogether. Here is how:
In one of the original script file, insert the following:

Code: Select all

init -1000 python:
    class chapter_info:
        def __init__(self,ord,title,lbl):
            self.chapter_ordering=ord
            self.chapter_title=title
            self.chapter_jump_label=lbl
    chapter_list=[]
The purpose here is create a list of chapter which is the variable "chapter_list", and presumable it will contains elements of the type "chapter_info" which contains some basic information about the chapter such as ordering (the order to display the chapter in the menu), title (the name of the chapter), and the name of the label to jump to. Of course, you could always customize that code to add in more information perhaps.

Then whenever you make a new chapter, insert something like this into the script file of that chapter:

Code: Select all

init -1 python:
    chapter_list.append(chapter_info(2,"The Lost Puppy","lost_puppy"))
label lost_puppy:
    "I heard a sound at the door."
    "I come out to check and to my surprise there is a puppy there."
This code add a chapter of ordering 2, titled "The Lost Puppy", and indicate that to start this chapter, you need to jump to "label lost_puppy:". This code will not run if the script file is not there (obviously :D ) but will automatically run if the script file is there, which will add the chapter into chapter_list. Using this, you can then make a menu that will go through chapter_list and see which chapter are there, what are their title, how to order them on the menu, it's all up to you. I am not going to put a menu code here, because I do not know what do you want to do with the menu. However, the process of reading the list usually start like this:

Code: Select all

python:
     for chapter in chapter_list:
         #do what you need here, for example chapter.chapter_title would be the title of the chapter
Of course, to start a chapter, you need to jump to the label with the corresponding name. So say the made your menu so that when the player select a chapter, you will story the label into the variable selected_chapter_jump_label, then you can jump like this:

Code: Select all

$renpy.jump(selected_chapter_jump_label)
Good luck. :D

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: Using an 'if' statement to check for a file?

#6 Post by xavimat »

There is another possibility.
If you own a website, you can use the "Web Update" functionality in renpy:
http://www.renpy.org/doc/html/updater.html
This way, you can add chapters and also solve mistakes in your first distributions.
Another advantage: The user doesn't need to mess with "rpy" files nor "game" directories. The built-in updater screen guides him/her through the process.
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: Google [Bot]