How do you create an output text 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
Silence in Space
Regular
Posts: 31
Joined: Tue Nov 22, 2016 4:48 pm
Projects: Silence in Space
Contact:

How do you create an output text file?

#1 Post by Silence in Space » Tue Aug 28, 2018 12:38 pm

I am trying figure out how to write a simple output text file to be created via python such as:

Code: Select all

f = open('testfile.txt','w')
f.write('\ntext')
f.close()
However, you cannot simply use python or init python to do this since it causes an error to occur if typed as is. Is writing a text file slightly different for Ren'Py than Python? If so, does anyone know what the proper coding format would be or redirect me to somewhere on this forum where this has already been addressed and solved?

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

Re: How do you create an output text file?

#2 Post by SuperbowserX » Tue Aug 28, 2018 1:42 pm

You can run Python statements in Ren'py. To run a Python statement at a specific point in your script, just use a python block. To run it at startup, just use an init python statement.

https://www.renpy.org/doc/html/python.html

User avatar
Silence in Space
Regular
Posts: 31
Joined: Tue Nov 22, 2016 4:48 pm
Projects: Silence in Space
Contact:

Re: How do you create an output text file?

#3 Post by Silence in Space » Tue Aug 28, 2018 5:51 pm

SuperbowserX wrote:
Tue Aug 28, 2018 1:42 pm
You can run Python statements in Ren'py. To run a Python statement at a specific point in your script, just use a python block. To run it at startup, just use an init python statement.

https://www.renpy.org/doc/html/python.html
If the said statement is placed into a python block (or an init python block), the following error occurs:

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/script.rpy", line 3, in script
    python:
  File "game/script.rpy", line 4, in <module>
    file = open('testfile,txt','w')
IOError: [Errno 13] Permission denied: u'testfile.txt'

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "game/script.rpy", line 3, in script
    python:
  File "/Users/user/Desktop/renpy-6.99.12.4-sdk/renpy/ast.py", line 848, in execute
    renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
  File "/Users/user/Desktop/renpy-6.99.12.4-sdk/renpy/python.py", line 1812, in py_exec_bytecode
    exec bytecode in globals, locals
  File "game/script.rpy", line 4, in <module>
    file = open('testfile,txt','w')
IOError: [Errno 13] Permission denied: u'testfile.txt'

Darwin-17.7.0-x86_64-i386-64bit
Ren'Py 6.99.14.1.3218
Text File Output Testing 1.0
Tue Aug 28 17:43:19 2018
Before you or anyone else says it, yes, I am running on an older version of Ren'Py and the reason is due to being unable to get the game I am working on functioning properly with the newer versions. So, updating/upgrading Ren'Py is not an option.

This is the entire script:

Code: Select all

label start:
    "Help"
    python:
        f = open('testfile.txt','w')
        f.write('\ntext')
        f.close()
    "Hello"
    return
Note: This is in no way part of the game that I am working on and is simply for testing text file outputs.

User avatar
Remix
Eileen-Class Veteran
Posts: 1305
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: How do you create an output text file?

#4 Post by Remix » Tue Aug 28, 2018 7:03 pm

Code: Select all

    with open( os.path.join( renpy.config.gamedir, "testfile.txt" ), 'w' ) as f:
        f.write( 'text' )
Might work better
Mad Scientist Todo List:
  • Finish Improved Event Handler
  • Implement DragonBones animation as a Creator Defined Container
  • Develop Cartoon Speech Bubble dialogue
  • Finish Bitmask collision and rebound vector system
  • Develop time based building mechanic
  • Others
  • *Find a superb artist and actually write a game*

User avatar
Silence in Space
Regular
Posts: 31
Joined: Tue Nov 22, 2016 4:48 pm
Projects: Silence in Space
Contact:

Re: How do you create an output text file?

#5 Post by Silence in Space » Tue Aug 28, 2018 10:59 pm

Remix wrote:
Tue Aug 28, 2018 7:03 pm

Code: Select all

    with open( os.path.join( renpy.config.gamedir, "testfile.txt" ), 'w' ) as f:
        f.write( 'text' )
Might work better
Thanks! That actually worked! Now I am curious if there is a way to make it so that the text file is saved to (or created on) the desktop or the document folder. Is that possible? As of right now, the text file would be imbedded within the game file and normally would not be accessible to the player.

User avatar
Remix
Eileen-Class Veteran
Posts: 1305
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: How do you create an output text file?

#6 Post by Remix » Wed Aug 29, 2018 7:57 am

Ren'Py games will only have permission to create or edit files within their directory scope, so I'd imagine trying to target desktop or docs would likely fail as above... You'd also need lots of conditionals to spoof paths for all the different platforms and have to navigate around firewall/av programs etc.

I personally would instant uninstall (or tbh, never install to begin with) a game that tried to play outside its scope.
Mad Scientist Todo List:
  • Finish Improved Event Handler
  • Implement DragonBones animation as a Creator Defined Container
  • Develop Cartoon Speech Bubble dialogue
  • Finish Bitmask collision and rebound vector system
  • Develop time based building mechanic
  • Others
  • *Find a superb artist and actually write a game*

User avatar
Silence in Space
Regular
Posts: 31
Joined: Tue Nov 22, 2016 4:48 pm
Projects: Silence in Space
Contact:

Re: How do you create an output text file?

#7 Post by Silence in Space » Wed Aug 29, 2018 10:18 am

Remix wrote:
Wed Aug 29, 2018 7:57 am
Ren'Py games will only have permission to create or edit files within their directory scope, so I'd imagine trying to target desktop or docs would likely fail as above... You'd also need lots of conditionals to spoof paths for all the different platforms and have to navigate around firewall/av programs etc.

I personally would instant uninstall (or tbh, never install to begin with) a game that tried to play outside its scope.
I was asking for whether or not if you can be done, not if it was a good idea. I already know it is a bad idea. The only reason this topic came to surface is due to the fact that Doki Doki Literature Club! did this with its directory and was curious to find out how one would do so (without scanning through the surprisingly complex scripts for the said game) and if Ren'Py is restricted to its own directory and that seems to be the case. The reason for the generation of text files would be due to the fact that the game that I am working on is a semi-point-and-click adventure psychological horror game with a tale of mystery.

I will not go into details as to what the game is about nor any story-/plot-related details. However, I will say that almost all audio, music, and sound effects are custom made and original; pseudo-animated backgrounds and images; the manipulation of a NVL mode character to simulate a computer terminal; the usage of imagemapping for navigation instead of narration-driven story telling; et cetera.

In short summary (if you simply don't care to ready anything above besides the first paragraph hopefully), I am looking for a way for the user to have easy assess to passcodes that can be found and used on the terminal found throughout the game without having to write them down by hand or memorize them. Perhaps, instead of an output text file that the user has to search and find the games directory, there can be an extra game menu option that allows the player to view passcodes or important information like the Quest Journal in Skyrim or any "Journal"/"Diary" system used in Open-world RPG games.

User avatar
Showsni
Miko-Class Veteran
Posts: 561
Joined: Tue Jul 24, 2007 12:58 pm
Contact:

Re: How do you create an output text file?

#8 Post by Showsni » Wed Aug 29, 2018 11:02 am

How about including a shortcut file that leads to the text file, and asking them to save it somewhere more convenient? But yes, you could probably fairly easily code an in game journal rather than creating an external text document.

User avatar
Remix
Eileen-Class Veteran
Posts: 1305
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: How do you create an output text file?

#9 Post by Remix » Wed Aug 29, 2018 12:29 pm

Ren'Py has full frameworks in place to create, remember, amend, delete as many variable values as you want.
The store module naturally remembers stuff as you go along and integrates into the rollback/forward and save facilities - if a passcode is added to the store then you rollback, the newly acquired passcode can be lost (if chosen)

Ren'Py has systems to remember them per play-through, per entire game or even per game series, so I fail to see what benefits could be achieved by using your own text file instead.

I'm pretty sure you would find more benefits by exploring using variables in Ren'Py than you would gain by effectively creating your own store.
Mad Scientist Todo List:
  • Finish Improved Event Handler
  • Implement DragonBones animation as a Creator Defined Container
  • Develop Cartoon Speech Bubble dialogue
  • Finish Bitmask collision and rebound vector system
  • Develop time based building mechanic
  • Others
  • *Find a superb artist and actually write a game*

User avatar
Silence in Space
Regular
Posts: 31
Joined: Tue Nov 22, 2016 4:48 pm
Projects: Silence in Space
Contact:

Re: How do you create an output text file?

#10 Post by Silence in Space » Wed Aug 29, 2018 1:47 pm

Remix wrote:
Wed Aug 29, 2018 12:29 pm
Ren'Py has full frameworks in place to create, remember, amend, delete as many variable values as you want.
The store module naturally remembers stuff as you go along and integrates into the rollback/forward and save facilities - if a passcode is added to the store then you rollback, the newly acquired passcode can be lost (if chosen)

Ren'Py has systems to remember them per play-through, per entire game or even per game series, so I fail to see what benefits could be achieved by using your own text file instead.

I'm pretty sure you would find more benefits by exploring using variables in Ren'Py than you would gain by effectively creating your own store.
There is a solution for that:

Code: Select all

$ renpy.block_rollback()
This is used MANY times to avoid the prospect of someone cheating or chickening out on his or her decision(s) and there are plenty of implicated warnings before choosing an undesirable decision. This is used before and after (and before) every single "terminal" line in the script since you would be unable to "rollback" a terminal display and/or process in conventional cases. In addition, you can only save at certain instances and quick-saves/autosaves are hidden from the player as to not ruin synchronization between audio (i.e. all) and sequentially looping images.

In addition, I am dropping the text file usage because now it seems like a pointless thing to incorporate unless I am making a meta-game or fourth-wall-breaking game (which I am not and don't plan on doing).

Post Reply

Who is online

Users browsing this forum: Alex, trickster-kaja