Relative adresses in renpy

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
mukashi
Newbie
Posts: 1
Joined: Sat Aug 01, 2015 9:12 am
Contact:

Relative adresses in renpy

#1 Post by mukashi »

I'm a beginner in renpy and i have such a problem:
I try to code like that

Code: Select all

init python:
    import fight
    Instance = fight.Fight("xml/be.xml")
Here the fight is a pythonic module and xml is a folder in the game folder. But in such way this xml is unreachable. I tried os.getcwd() and it shows that default adress is renpy folder. For example, no problems with this code

Code: Select all

init:
    image bg gh = "images/golden_horn.jpg"
My images are in the game folder.

DragoonHP
Miko-Class Veteran
Posts: 758
Joined: Tue Jun 22, 2010 12:54 am
Completed: Christmas
IRC Nick: DragoonHP
Location: Zion Island, Solario
Contact:

Re: Relative adresses in renpy

#2 Post by DragoonHP »

You need to use "renpy.loader.transfn()"
So it will be something like this:

Code: Select all

init python:
    import fight
    Instance = fight.Fight(renpy.loader.transfn("xml/be.xml"))

User avatar
Karl_C
Veteran
Posts: 232
Joined: Sun Mar 31, 2013 6:18 am
Contact:

Re: Relative adresses in renpy

#3 Post by Karl_C »

PyTom wrote:renpy.loader.transfn is not a documented API. Don't use it.
http://lemmasoft.renai.us/forums/viewto ... 7&#p380566

Is there an alternative?

User avatar
PyTom
Ren'Py Creator
Posts: 16096
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Relative adresses in renpy

#4 Post by PyTom »

The right way to open a file is renpy.file.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

User avatar
Karl_C
Veteran
Posts: 232
Joined: Sun Mar 31, 2013 6:18 am
Contact:

Re: Relative adresses in renpy

#5 Post by Karl_C »

PyTom wrote:The right way to open a file is renpy.file.
As mukashi apparently want to pass the relative path of a file to an external python package ("xml/be.xml", "fight") and renpy.file(fn) returns a read-only file-like object:

How could this be handled without modifying the external python package?

User avatar
PyTom
Ren'Py Creator
Posts: 16096
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Relative adresses in renpy

#6 Post by PyTom »

What python package is it? Most take file-like objects instead of or in addition to filenames.

If you really want a real path, you can try os.path.join(config.gamedir, "myfile.xml"). But that will break on iOS and Android, and if you archive the file, etc...
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

User avatar
Karl_C
Veteran
Posts: 232
Joined: Sun Mar 31, 2013 6:18 am
Contact:

Re: Relative adresses in renpy

#7 Post by Karl_C »

Sorry for late reply.
PyTom wrote:What python package is it? Most take file-like objects instead of or in addition to filenames.
In my case it's pyAIML, an interpreter for AIML (the Artificial Intelligence Markup Language), implemented entirely in standard Python. It's normally used for chatterbots or 'semi-intelligent' desktop utilities.
PyTom wrote:If you really want a real path, you can try os.path.join(config.gamedir, "myfile.xml"). But that will break on iOS and Android, and if you archive the file, etc...
I quick test shows that it seems to work from within RenPy, at least on my Linux machine (Eileens response in the screenshot below was generated by pyAIML and a freely available AIML set):
Image Image

BTW: You pointed out already, that renpy.file and file can be used to copy a file out of a .rpa to disk. Is it possible to write a temp file with RenPy that won't break on iOS and Android? What would be the suggested location?

User avatar
Asahel
Newbie
Posts: 24
Joined: Fri Nov 14, 2014 10:38 am
Contact:

Re: Relative adresses in renpy

#8 Post by Asahel »

Karl_C wrote:Sorry for late reply.

I quick test shows that it seems to work from within RenPy, at least on my Linux machine (Eileens response in the screenshot below was generated by pyAIML and a freely available AIML set):
Image Image
Karl_C this is awesome, could you please share a demo for ren'py - pyAIML integration? I really want a chatbot /digital assistant in Ren'py and I've been trying to run pyAIML from within ren'py without success. :cry: :cry: :cry:

User avatar
Karl_C
Veteran
Posts: 232
Joined: Sun Mar 31, 2013 6:18 am
Contact:

Re: Relative adresses in renpy

#9 Post by Karl_C »

Asahel wrote: Karl_C this is awesome, could you please share a demo for ren'py - pyAIML integration? I really want a chatbot /digital assistant in Ren'py and I've been trying to run pyAIML from within ren'py without success. :cry: :cry: :cry:
A big, fat warning: As I'm not a Python programmer, I'm quite sure that there are much better ways to do it. But if you only want a working example, here you are:

script.rpy:

Code: Select all

# You can place the script of your game in this file.

# Declare images below this line, using the image statement.
# eg. image eileen happy = "eileen_happy.png"

# Declare characters used by this game.
define e = Character('Eileen', color="#ffffff")

init:
    $ import AIMLBot
    

# The game starts here.
label start:

    e "This is a test of pyAIML from within RenPy"

    $ renpy.free_memory()

    $ user_input =[]
    while True:

        $ user_input = renpy.input("Type some input")
        $ user_input = user_input.strip()
        $ response = AIMLBot.AimlBot().run(user_input)
    
        if user_input == "quit":
            jump End_of_loop
        e "[response]"

label End_of_loop:

    e "End of loop"

    return
AIMLbot.py in the "game" directory:

Code: Select all

import aiml
import os.path
import sys


class AimlBot:
	brainFileName = "standard.brn"
	kernel = aiml.Kernel()
	botName = "Eileen"

	def __init__(self):
		self.kernel.verbose(False)
		self.kernel.setBotPredicate("name", self.botName)

		if os.path.isfile(self.brainFileName):
		    self.kernel.bootstrap(brainFile = self.brainFileName)
		else:
		    self.kernel.bootstrap(learnFiles = "std-startup.xml", commands = "load aiml b")
		    self.kernel.saveBrain(self.brainFileName)

	# runs bot on input and returns answer
	def run(self, inputString):
		answer =  self.kernel.respond(inputString) # second argument is string
		#self.kernel.saveBrain(self.brainFileName)
		return answer
You also need to have:
  • this pyAIML zip file extracted in the "game" directory (you'll have to rename the subdirectory from "pyAIML-master" to "aiml")
  • The standard AIML set, extracted in the in the subdirectory "standard"
  • a "std-startup.xml" in the "game" directory (you'll have to edit it, so that it can find the "standard" directory with your AIML set
Good luck!

User avatar
Asahel
Newbie
Posts: 24
Joined: Fri Nov 14, 2014 10:38 am
Contact:

Re: Relative adresses in renpy

#10 Post by Asahel »

:shock: I can't believe my luck, I seriously thought I necroed the thread. Thank you so much! :D
I have pyAIML and ALICE set running on my linux machine without a hitch, but I couldn't for the life of me make it run with ren'py. I'm not a programmer either (understatement), just want my own linux chatbot and virtual assistant.

I'll give it a try, hope it works for me too!

User avatar
Asahel
Newbie
Posts: 24
Joined: Fri Nov 14, 2014 10:38 am
Contact:

Re: Relative adresses in renpy

#11 Post by Asahel »

It doesn't open, I'm getting an encoding error. It is supposed to be UTF-8, right? :?

Code: Select all

UnicodeDecodeError: 'utf8' codec can't decode byte 0xd4 in position 47219: invalid continuation byte
I don't want to hijack this thread further, I will post a separate thread for help.

jeffster
Veteran
Posts: 409
Joined: Wed Feb 03, 2021 9:55 pm
Contact:

Re: Relative adresses in renpy

#12 Post by jeffster »

PyTom wrote: Sun Aug 02, 2015 11:15 pm The right way to open a file is renpy.file.
It does not find files like 'game/script.rpy':
File "game/screens.rpy", line 1445, in <module>
with renpy.file(fn) as f:
IOError: Couldn't find file 'game/script.rpy'.
So I have to ridiculously strip a part of the filename like this:

Code: Select all

screen this_script():
    default fn = renpy.get_filename_line()[0]
    python:
        if fn[0:5] == "game/":
            fn = fn[5:]
        with renpy.file(fn) as f:
            lines = f.readlines()
Is there a way to do that better?

Post Reply

Who is online

Users browsing this forum: No registered users