How to include Ren'Py statements inside Python statement?

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
hell_oh_world
Miko-Class Veteran
Posts: 777
Joined: Fri Jul 12, 2019 5:21 am
Projects: The Button Man
Organization: NILA
Github: hell-oh-world
Location: Philippines
Contact:

How to include Ren'Py statements inside Python statement?

#1 Post by hell_oh_world » Fri Jul 12, 2019 10:15 am

I'm actually very new to this game engine, but I have a basic foundation in programming (mostly in Java). So please bear with me If I will unknowingly mention some mistakes.

So today I've read most of the Ren'Py documentation to learn about the program. I found out that you can incorporate the Python scripts onto Ren'Py. But I'm quite curious if it is also applicable the other way around. I mean, I used the

Code: Select all

python
statement in my code, but I also want to run some Ren'Py statements inside the first one. Is it possible? I've read about the Ren'Py statement equivalents, but there's only a plenty of equivalent statements showed there. Am I missing something?

I'm actually trying to run the

Code: Select all

while
statement onto a

Code: Select all

screen
statement, but It produces error. And based from what I understood, it was because the

Code: Select all

while
statement was not applicable as a child of the

Code: Select all

screen
property that I tried to use. So I was forced to script in Python... Also, If you guys figured out what I'm trying to do, I'm very okay to alternative suggestions that would achieve the thing that I'm trying to do, but I'm still hoping to get answers based on my way.

Any suggestions and feedback are welcome. Thanks in advance.

P.S.: Please try to be specific and reply with terminologies that you think noob users like me will understand. I've read about some of the terminologies in the documentation and I'm quite familiar already with some of the syntax. But don't go too deep because I know I won't be able to understand it very well. Thanks again guys :)

Code: Select all

screen choice(items):
    style_prefix "choice"

    grid 3 3:
        python:
            inc = 1
            while inc <= 9:
                if inc%2 == 0:
                    textbutton (items[inc]).caption action i.action
                else:
                    null
                inc += 1
Last edited by hell_oh_world on Fri Jul 12, 2019 6:09 pm, edited 2 times in total.

User avatar
Alex
Lemma-Class Veteran
Posts: 2981
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: How to include Ren'Py statements inside Python statement?

#2 Post by Alex » Fri Jul 12, 2019 4:51 pm

The issue is that while loop is not used in screens - use for loop instead. You can use while loop in ordinar Ren'Py script. Also, you can use while and for loops insyde a python block.

As for choice menu, try it like

Code: Select all

screen choice(items):
    #style_prefix "choice"

    grid 3 3:
        align (0.5, 0.5)
        for i in items:
            textbutton i.caption action i.action
        for n in range(9-len(items)):
            null

label start:
    "..."
    menu:
        "Choice":
            "1"
    menu:
        "Choice 1":
            pass
        "Choice 2":
            pass
        "Choice 3":
            pass
        "Choice 4":
            pass
    "2"
    menu:
        "Choice 1":
            pass
        "Choice 2":
            pass
        "Choice 3":
            pass
        "Choice 4":
            pass
        "Choice 5":
            pass
        "Choice 6":
            pass
        "Choice 7":
            pass
        "Choice 8":
            pass  
    "?!"
And yes, you can use Ren'Py statements in a python block - their python equivalents, but see this first - https://www.renpy.org/doc/html/save_load_rollback.html

Also, check this (if not yet) - viewtopic.php?f=51&t=39572#p422964

User avatar
hell_oh_world
Miko-Class Veteran
Posts: 777
Joined: Fri Jul 12, 2019 5:21 am
Projects: The Button Man
Organization: NILA
Github: hell-oh-world
Location: Philippines
Contact:

Re: How to include Ren'Py statements inside Python statement?

#3 Post by hell_oh_world » Fri Jul 12, 2019 6:02 pm

Alex wrote:
Fri Jul 12, 2019 4:51 pm
The issue is that while loop is not used in screens - use for loop instead. You can use while loop in ordinar Ren'Py script. Also, you can use while and for loops insyde a python block.

As for choice menu, try it like

Code: Select all

screen choice(items):
    #style_prefix "choice"

    grid 3 3:
        align (0.5, 0.5)
        for i in items:
            textbutton i.caption action i.action
        for n in range(9-len(items)):
            null

label start:
    "..."
    menu:
        "Choice":
            "1"
    menu:
        "Choice 1":
            pass
        "Choice 2":
            pass
        "Choice 3":
            pass
        "Choice 4":
            pass
    "2"
    menu:
        "Choice 1":
            pass
        "Choice 2":
            pass
        "Choice 3":
            pass
        "Choice 4":
            pass
        "Choice 5":
            pass
        "Choice 6":
            pass
        "Choice 7":
            pass
        "Choice 8":
            pass  
    "?!"
And yes, you can use Ren'Py statements in a python block - their python equivalents, but see this first - https://www.renpy.org/doc/html/save_load_rollback.html

Also, check this (if not yet) - viewtopic.php?f=51&t=39572#p422964
In your for loop, I'm quite confused... Because from what I can understand the first for loop will fill all the grids already with textbutton displayables. So does having the second loop there will cause error? Because the grid is full already with just the first loop.

To clarify, what I'm actually really trying to achieve is to fill the even-numbered grids with textbutton while the remaining grids should be filled will null displayables. I saw that you used the range function, I didn't know about it so I tried to search about it. I guess I could achieve what I want through the use of it. Guess my code should be like this, based on what i want to achieve.

Code: Select all

grid 3 3:
    for i in range(1, 10, 1):
        if i%2 == 0:
            textbutton i.caption action i.action
        else:
            null
Is this even right? I just found out that it can take three parameters.

On the other hand, I just took a quick peek of the links you sent. The first one, I don't think if that documentation has anything to do with my issue. But I'm still gonna check it later. The latter is definitely worth checking. I've been trying to find this kind of introduction in Ren`Py in the cookbook but no luck, I'm thankful for this 😂.

I'm still hoping though if what I want is possible in my way through while loop and with Ren'Py statements like textbutton inside a python block. I'm quite expecting that there's a statement like
renpy:
#Ren'Py's block of codes....
inside a python statement. But I bet there's no such thing 😂. But I guess its possible with equivalent statements which is I don't know. I don't know the equivalent statements of textbutton or other displayables in Python language.

User avatar
Alex
Lemma-Class Veteran
Posts: 2981
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: How to include Ren'Py statements inside Python statement?

#4 Post by Alex » Fri Jul 12, 2019 6:31 pm

hell_oh_world wrote:
Fri Jul 12, 2019 6:02 pm
... In your for loop, I'm quite confused... Because from what I can understand the first for loop will fill all the grids already with textbutton displayables. So does having the second loop there will cause error? Because the grid is full already with just the first loop. ...
Well, this was an example of 9 cells grid, so it will work for up to 9 choices (more will cause an error). You see, "items" is passed to this screen from your menu code - it's a list that has all choices and their results, so you can iterate through the "items" to create buttons and set their actions.
https://www.renpy.org/doc/html/screen_s ... tml#choice

If you want to layout any number of choices, you need either to put them inside vpgrid (that calculates its own size (but can be tricky to customize)), or made some calculations to get the number of columns and rows for the grid.
https://www.renpy.org/doc/html/screens.html#vpgrid

Code: Select all

screen choice(items):
    # let's make a grid of 3 columns
    $ rows, reminder = divmod(len(items), 3)
    if reminder > 0:
        $ rows += 1

    grid 3 rows:
        align (0.5, 0.5)
        for i in items:
            textbutton i.caption action i.action
        for n in range(3*rows-len(items)):
            null


The first link mostly was about the unability to save game state in the middle of the python block of code.


All the Ren'Py statements are python functions in first place, so you can use this functions (but Ren'Py script is more readable for human).

As for buttons etc. - screens are used to visualize things to player, so use them as they are (see screen language - https://www.renpy.org/doc/html/screens.html https://www.renpy.org/doc/html/screen_actions.html) or via python - https://www.renpy.org/doc/html/screen_python.html

User avatar
hell_oh_world
Miko-Class Veteran
Posts: 777
Joined: Fri Jul 12, 2019 5:21 am
Projects: The Button Man
Organization: NILA
Github: hell-oh-world
Location: Philippines
Contact:

Re: How to include Ren'Py statements inside Python statement?

#5 Post by hell_oh_world » Fri Jul 12, 2019 7:16 pm

Alex wrote:
Fri Jul 12, 2019 6:31 pm
hell_oh_world wrote:
Fri Jul 12, 2019 6:02 pm
... In your for loop, I'm quite confused... Because from what I can understand the first for loop will fill all the grids already with textbutton displayables. So does having the second loop there will cause error? Because the grid is full already with just the first loop. ...
Well, this was an example of 9 cells grid, so it will work for up to 9 choices (more will cause an error). You see, "items" is passed to this screen from your menu code - it's a list that has all choices and their results, so you can iterate through the "items" to create buttons and set their actions.
https://www.renpy.org/doc/html/screen_s ... tml#choice

If you want to layout any number of choices, you need either to put them inside vpgrid (that calculates its own size (but can be tricky to customize)), or made some calculations to get the number of columns and rows for the grid.
https://www.renpy.org/doc/html/screens.html#vpgrid

Code: Select all

screen choice(items):
    # let's make a grid of 3 columns
    $ rows, reminder = divmod(len(items), 3)
    if reminder > 0:
        $ rows += 1

    grid 3 rows:
        align (0.5, 0.5)
        for i in items:
            textbutton i.caption action i.action
        for n in range(3*rows-len(items)):
            null


The first link mostly was about the unability to save game state in the middle of the python block of code.


All the Ren'Py statements are python functions in first place, so you can use this functions (but Ren'Py script is more readable for human).

As for buttons etc. - screens are used to visualize things to player, so use them as they are (see screen language - https://www.renpy.org/doc/html/screens.html https://www.renpy.org/doc/html/screen_actions.html) or via python - https://www.renpy.org/doc/html/screen_python.html
My bad... I haven't thought about what you said. I barely know anything about Python's functions, got accustomed much in Java's methods. Thanks for the clarifications :) Now I've got more links to check on to. Thanks for this alternative suggestion, this is worth trying for. I'll try this later.

I'm still open for any suggestions guys :)

User avatar
hell_oh_world
Miko-Class Veteran
Posts: 777
Joined: Fri Jul 12, 2019 5:21 am
Projects: The Button Man
Organization: NILA
Github: hell-oh-world
Location: Philippines
Contact:

Re: How to include Ren'Py statements inside Python statement?

#6 Post by hell_oh_world » Fri Jul 12, 2019 7:28 pm

I just tried something right now... And it yielded what I want...

Code: Select all

screen choice(items):
    style_prefix "choice"

    grid 3 3:
        align (0.5, 0.5)
        for i in items:
            null
            textbutton i.caption action i.action xsize 250
        null
This is of course for a fixed 9x9 grid size and I guess with a specific 4 number of choices.
This will do I guess :) But my question still on going...

Post Reply

Who is online

Users browsing this forum: No registered users