[solved] Interpolation of text doesn't work

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
Ayutac
Regular
Posts: 150
Joined: Thu Oct 18, 2012 2:23 pm
Projects: Pokémon Dating Sim
Organization: A Breeze Of Science
Deviantart: Ubro
Location: Mayence, Germany
Contact:

[solved] Interpolation of text doesn't work

#1 Post by Ayutac » Thu Jun 05, 2014 10:59 am

Hello, long time not read. So, generally I can do something like

Code: Select all

$ s = "test"
$ narrator("This is a [s].")
Works. But I have a more complicated construction like this:

Code: Select all

init -10 python:
    sNoSuchCheatS = "The cheat code \"[s]\" is invalid!"

...

label start:
    call initializeGameCheats

...

label initializeGameCheats:

    python:
        def encrypt(s, first = True, two = False) ...
        def decrypt(s, first = True, two = False) ...
        def encryptPairs(chars) ...
        def decryptPairs(s) ...
        def usePairCheats(s):
            global sNoSuchCheatS
            if not (isinstance(s, str) or isinstance(s, unicode)):
                raise TypeError("s must be a string!")
            try:
                tempList = decryptPairs(s)
            except ValueError:
                narrator(s)
                narrator(sNoSuchCheatS)
    ...
    return
that gives me an error message like that:

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/navigation.rpy", line 184, in script
    $ cheat()
  File "game/navigation.rpy", line 184, in <module>
    $ cheat()
  File "game/gameCheats.rpy", line 320, in cheat
    usePairCheats(s)
  File "game/gameCheats.rpy", line 178, in usePairCheats
    narrator(sNoSuchCheatS)
KeyError: 's'

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

Full traceback:
  File "game/navigation.rpy", line 184, in script
    $ cheat()
  File "/run/media/nait/nanika/mobile_programs/renpy-6.15.7-sdk/renpy/ast.py", line 756, in execute
    renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
  File "/run/media/nait/nanika/mobile_programs/renpy-6.15.7-sdk/renpy/python.py", line 1382, in py_exec_bytecode
    exec bytecode in globals, locals
  File "game/navigation.rpy", line 184, in <module>
    $ cheat()
  File "game/gameCheats.rpy", line 320, in cheat
    usePairCheats(s)
  File "game/gameCheats.rpy", line 178, in usePairCheats
    narrator(sNoSuchCheatS)
  File "/run/media/nait/nanika/mobile_programs/renpy-6.15.7-sdk/renpy/character.py", line 808, in __call__
    what = what_pattern.replace("[what]", sub(what, translate=translate))
  File "/run/media/nait/nanika/mobile_programs/renpy-6.15.7-sdk/renpy/substitutions.py", line 223, in substitute
    s = formatter.vformat(s, (), kwargs)
  File "/home/tom/ab/x64lucid-deps/install/lib/python2.7/string.py", line 549, in vformat
  File "/home/tom/ab/x64lucid-deps/install/lib/python2.7/string.py", line 571, in _vformat
  File "/home/tom/ab/x64lucid-deps/install/lib/python2.7/string.py", line 632, in get_field
  File "/home/tom/ab/x64lucid-deps/install/lib/python2.7/string.py", line 591, in get_value
KeyError: 's'

Linux-3.13.8-1-ARCH-x86_64-with-glibc2.2.5
Ren'Py 6.17.6.512
I don't really understand the error message but I guess the calling of s from inside a method is somehow bad. Nevertheless, a simple construction like

Code: Select all

label start:
    python:
        s = "nope"
        def test():
            narrator("[s]")
        test()
seems to work just fine...
Last edited by Ayutac on Fri Jun 06, 2014 12:05 pm, edited 1 time in total.
Up next: An original, open source, text-based Dating Sim. Stay tuned ;)

User avatar
Asceai
Eileen-Class Veteran
Posts: 1258
Joined: Fri Sep 21, 2007 7:13 am
Projects: a battle engine
Contact:

Re: Interpolation of text doesn't work

#2 Post by Asceai » Thu Jun 05, 2014 5:05 pm

You never set s to anything in the second code snippet, that's why you get a key error. In the other examples you set s.

User avatar
Ayutac
Regular
Posts: 150
Joined: Thu Oct 18, 2012 2:23 pm
Projects: Pokémon Dating Sim
Organization: A Breeze Of Science
Deviantart: Ubro
Location: Mayence, Germany
Contact:

Re: Interpolation of text doesn't work

#3 Post by Ayutac » Fri Jun 06, 2014 10:29 am

Asceai wrote:You never set s to anything in the second code snippet, that's why you get a key error. In the other examples you set s.
s is given as a parameter. Does this not count?
Up next: An original, open source, text-based Dating Sim. Stay tuned ;)

User avatar
Asceai
Eileen-Class Veteran
Posts: 1258
Joined: Fri Sep 21, 2007 7:13 am
Projects: a battle engine
Contact:

Re: Interpolation of text doesn't work

#4 Post by Asceai » Fri Jun 06, 2014 11:09 am

Nope. It's a matter of scoping, store variables and when interpolation happens.

When you create a variable in top-level python - that is, python (either in a python: block or after a $) in Ren'Py code (as opposed to screen code, which is a bit different) that is on the top-level (that is, not in a function) it goes on the store, which is where variables are kept for rollback etc.

On the other hand, variables declared in or passed as parameters to a function are only known to that function - they don't exist after the function returns etc.

The other thing is that square bracket [] interpolation is Ren'Py interpolation (really, substitution), not Python interpolation. The substitution happens inside narrator(), not in usePairCheats(), but narrator doesn't know about s. Python interpolation will happen in usePairCheats(), so let's use that instead:

sNoSuchCheatS = "The cheat code \"%s\" is invalid!"
narrator(sNoSuchCheatS % s)

User avatar
Ayutac
Regular
Posts: 150
Joined: Thu Oct 18, 2012 2:23 pm
Projects: Pokémon Dating Sim
Organization: A Breeze Of Science
Deviantart: Ubro
Location: Mayence, Germany
Contact:

Re: Interpolation of text doesn't work

#5 Post by Ayutac » Fri Jun 06, 2014 12:04 pm

Asceai wrote:The substitution happens inside narrator(), not in usePairCheats(), but narrator doesn't know about s. Python interpolation will happen in usePairCheats(), so let's use that instead:

sNoSuchCheatS = "The cheat code \"%s\" is invalid!"
narrator(sNoSuchCheatS % s)
I see. That's a pity, I really didn't wanted to split things up. sNo... is a global (almost) constant because I use it at different parts in the code and I didn't want to have it changed everywhere by hand if it changes. So I'll split sNo... up.


But you have my gratitude for the explanation :D

Also I realized that I forgot to use the search in before, won't happen the next two, three months, I promise!
Up next: An original, open source, text-based Dating Sim. Stay tuned ;)

User avatar
Asceai
Eileen-Class Veteran
Posts: 1258
Joined: Fri Sep 21, 2007 7:13 am
Projects: a battle engine
Contact:

Re: [solved] Interpolation of text doesn't work

#6 Post by Asceai » Fri Jun 06, 2014 12:24 pm

You don't have to split anything up, you can use the exact same code you had before, just replace

Code: Select all

sNoSuchCheatS = "The cheat code \"[s]\" is invalid!"
with

Code: Select all

sNoSuchCheatS = "The cheat code \"%s\" is invalid!"
and

Code: Select all

narrator(sNoSuchCheatS)
with

Code: Select all

narrator(sNoSuchCheatS % s)
You get to maintain the exact same separation.

Post Reply

Who is online

Users browsing this forum: Google [Bot], _ticlock_