Working with Python code in 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
User avatar
rabcor
Regular
Posts: 81
Joined: Sun Mar 17, 2013 3:07 pm
Projects: None (Need a programmer?)
Organization: Cestarian Games
Contact:

Working with Python code in Ren'Py

#1 Post by rabcor » Sun Dec 22, 2013 4:27 pm

I've been writing some (very) heavy python code over the last few days. And it's so complicated i only barely understand what i've written :roll:. I've been writing it in python from the start rather than Ren'Py even if i've intended it for Ren'Py from the start simply because i'm pretty familiar with Python already, but i'm a total noob on Ren'Py and often take a while to figure out why Ren'Py rejects my code when it does. :oops:

Not long from now i am going to implement this code into a Ren'Py game (for prototyping purposes in this case) and i was wondering how best to do that. My code consists mostly of classes, it's only got one global variable in it (which is a short tuple). It's also only got one function. Everything else is a class usually linked together in a chain eventually leading to one class which will be used as a variable.

Short Example:

Code: Select all

class class1():
    ...
class class2(class1): #Class2 picks up Class1 and continues it
    variable5 = 50
    ...
class class3(class2): #Class3 picks up Class2 and Class1 and continues them
    ...
class class4(class3): #Class4 picks up Class3, Class2 and Class1 and ends the chain of classes.
    ...

finalvar = class4()
finalvar.variable5 = 90
I figured i'll want to use an "init python:" block for this but is there any guide or tutorial for how to mix together Ren'Py and Python code and work with them together?

The only thing i know is "init python:" blocks and "$ variables = ..."

Is that really all there is to know? I'd like a few pointers, is there anything i should be wary of? should i keep an eye out for code i could implement as part of the Ren'Py code instead of being part of a Python block?

I've seen some code by others that does rely reasonably heavily on direct python scripts, but sometimes i see very python~y things like try/except and loops in the Ren'Py code. Overall i'm just pretty confused :oops:

Any pointers are appreciated, and i don't mind doing a bit of reading so if there is some good guide or documentation on this i should read then please link it.

User avatar
KimiYoriBaka
Miko-Class Veteran
Posts: 636
Joined: Thu May 14, 2009 8:15 pm
Projects: Castle of Arhannia
Contact:

Re: Working with Python code in Ren'Py

#2 Post by KimiYoriBaka » Sun Dec 22, 2013 9:20 pm

okay, firstly, init python is a combination of two blocks into one so that you don't need to indent as much. the init block is so that the code will run before the actual game begins (right before the menus I think). if you want to run complex python code anywhere else you can put it in a python block rather than using $.

also, it's important to note that any renpy statement that expects an expression is expecting a python expression. this includes if and while statements which are described here: http://www.renpy.org/doc/html/conditional.html , but are pretty much the same as in python. for loops still need to be in python blocks, though, because they're harder to for the save functions and rollback to deal with.

other than that, you could skim through the sections in the documentation listed under "python and renpy", which covers the functions you can use to imitate renpy statements.

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: Working with Python code in Ren'Py

#3 Post by PyTom » Mon Dec 23, 2013 1:07 am

Basically, some rules you know are:

* Define classes and functions in init python blocks, call them from python blocks.

* All classes should inherit from object. Ren'Py might not work with old-style classes. (Or it might silently turn them into new-style classes, I haven't checked.)

* Ren'Py can only save/load/rollback at statements in Ren'Py script code. So lots of python code could be problematic. You often want code like:

Code: Select all

$ mygame = Game()

while mygame.step():
    pass


Also, in general, you should consider if you really need such deep inheritance hierarchies. In general, you want to use composition (has-a) in preferences to inheritance (is-a).
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

User avatar
rabcor
Regular
Posts: 81
Joined: Sun Mar 17, 2013 3:07 pm
Projects: None (Need a programmer?)
Organization: Cestarian Games
Contact:

Re: Working with Python code in Ren'Py

#4 Post by rabcor » Mon Dec 23, 2013 10:36 am

So in the above example could Ren'Py save/load the "mygame" variable and it's contents?

The hierarchy system i've got is mostly there for readability (or else i'd just have one, unwieldy, humongous class, or i'd need to make another class which combines all the other classes, which was actually what i originally intended to do, but found that this way was more "direct") but in some cases they are needed. I have one class that uses composition in the hierarchy (similar to below).

Code: Select all

class thisisclass(object):
    value = 5
    ...
class class1(object):
    ...
class class2(class1):
    variable = thisisclass()
    ...
var = class2()
var.variable.value = 0
This is what you mean by inherit from object right?

I am using a hierarchy system because in general i will never want to use only one of the classes, all the classes that are in the hierarchy are worthless without the others anyways so i think it's ok to use it in this code. Thanks for the warning though, i'll keep it at the back of my head. I hadn't given it much thought before, but i think i understand the differences and why in general it's a good idea to avoid inheritance.

Post Reply

Who is online

Users browsing this forum: No registered users