Complex data structures of Python with Ren'py

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
GlacyR24
Newbie
Posts: 1
Joined: Thu Apr 18, 2013 7:46 pm
Contact:

Complex data structures of Python with Ren'py

#1 Post by GlacyR24 » Sun May 12, 2013 3:43 am

Hi guys:

I'm planning to make a game with Ren'py. I already have some experience with python so I was wondering if it is possible to use data structures such as lists, dictionaries and classes to store values in my Ren'py game, if so how would I use these data structures?

I was also wondering how would you implement loops such as for loops or while loops with Ren'py? If someone could provide simple examples to demonstrate and hence answer my questions that would be lovely and I would really appreciate it.

Thank you

User avatar
TrickWithAKnife
Eileen-Class Veteran
Posts: 1261
Joined: Fri Mar 16, 2012 11:38 am
Projects: Rika
Organization: Solo (for now)
IRC Nick: Trick
Location: Tokyo, Japan
Contact:

Re: Complex data structures of Python with Ren'py

#2 Post by TrickWithAKnife » Sun May 12, 2013 4:14 am

I'm not even a beginner at Python, but I've managed to import some Python code (including dictionaries) into my renpy projects.

So the answer is yes, you can import some Python code, but as for how, I couldn't make any recommendations. I just hammer it in, and hope I can figure out the error messages long enough to get it working.

Just make sure you precede the python code with:

Code: Select all

init python:
    ## Whatever your code is here"
And import whatever you need (which may be common for python anyway):

Code: Select all

    import string
Regarding using while, check out this link:
http://www.renpy.org/wiki/renpy/doc/ref ... _Statement

Using for, I *think* it's something like this:

Code: Select all

        for i in whatever_your_maximum_variable_is:
            # Do whatever you want
"We must teach them through the tools with which they are comfortable."
The #renpy IRC channel is a great place to chat with other devs. Due to the nature of IRC and timezone differences, people probably won't reply right away.

If you'd like to view or use any code from my VN PM me. All code is freely available without restriction, but also without warranty or (much) support.

User avatar
kankan
Regular
Posts: 80
Joined: Tue Mar 06, 2012 1:47 am
Contact:

Re: Complex data structures of Python with Ren'py

#3 Post by kankan » Sun May 12, 2013 9:21 am

Yup, you can import everything (or almost everything? I don't know if there are limitations) the same way you normally would under that "init python:" TrickWithAKnife mentioned. Lists, dictionaries, and classes are all included, so you don't have to import those.

User avatar
PyTom
Ren'Py Creator
Posts: 15893
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: Complex data structures of Python with Ren'py

#4 Post by PyTom » Sun May 12, 2013 11:54 am

GlacyR24 wrote:I was also wondering how would you implement loops such as for loops or while loops with Ren'py? If someone could provide simple examples to demonstrate and hence answer my questions that would be lovely and I would really appreciate it.
The while loop is just in Ren'Py:

Code: Select all

while python_expression:
    "Going through the loop."
    $ python_statement
The for loop isn't part of Ren'Py, since it requires the creation of an iterator which can't be saved. You have to choices. If you don't need to save or run any Ren'Py statements in the middle, you can use a python block:

Code: Select all

python:
     for i in my_list:
         do_something_with(i)
otherwise, you have to fake it using a while statement:

Code: Select all

$ j = 0
while j < len(my_list):
    $ i = my_list[j]
    "The list contains [i]."
    $ j += 1
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
"Silly and fun things are important." - Elon Musk
Software > Drama • https://www.patreon.com/renpytom

Post Reply

Who is online

Users browsing this forum: Bing [Bot]