SOLVED - Displaying a Dictionary as a String

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
Enchant00
Regular
Posts: 68
Joined: Tue Jan 12, 2016 1:17 am
Contact:

SOLVED - Displaying a Dictionary as a String

#1 Post by Enchant00 » Mon May 21, 2018 4:29 pm

Hello. As you can see below, I simply just want to display the items of a python dictionary as a string. I could iterate throughout the entire dictionary and display them as separate strings but I was wondering if it's possible to just use the str to convert the dictionary into a string in one go.

I tried the following:

Test 1:

Code: Select all

default random_dictionary = {'Test' : 1}

init python:
    random_dictionary = str('random_dictionary')  # for some reason it wants me to put in in quotes? Else I would receive a NameError: random_dictionary not defined
    
start:
    '[random_dictionary]'
    
    return
Test 2:

Code: Select all

default random_dictionary = {'Test' : 1}

start:
    $ random_dictionary = str(random_dictionary)
    '[random_dictionary]'
	
    return
This is the error:
I'm sorry, but an uncaught exception occurred.

While running game code:
File "game/script.rpy", line 36, in script
'[random_dictionary]'
Exception: Unknown text tag u"u'Test': 1"
Also, I thought I could use python string interpolation such as your %s or format with a text after the start but no dice apparently.
Last edited by Enchant00 on Mon May 21, 2018 11:21 pm, edited 1 time in total.

User avatar
Ocelot
Miko-Class Veteran
Posts: 849
Joined: Tue Aug 23, 2016 10:35 am
Skype: miinipaa
Contact:

Re: Displaying a Dictionary as a String

#2 Post by Ocelot » Mon May 21, 2018 4:36 pm

Code: Select all

'[random_dictionary!q]'
!q is needed so Renpy would not try to parse {Data: 1} as a tag.

Also: remove init python part. It literally does nothing. And after game start it will be overwrittent by default statement execution.
< < insert Rick Cook quote here > >

User avatar
Enchant00
Regular
Posts: 68
Joined: Tue Jan 12, 2016 1:17 am
Contact:

Re: Displaying a Dictionary as a String

#3 Post by Enchant00 » Mon May 21, 2018 11:20 pm

Oh I see now thank :D

Post Reply

Who is online

Users browsing this forum: trooper6