[SOLVED] Python statement equivalents

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
Queex
Regular
Posts: 46
Joined: Sat Mar 14, 2015 9:47 am
Contact:

[SOLVED] Python statement equivalents

#1 Post by Queex »

I'm having difficulty getting my head around some the proper python statement equivalents. I want the python equivalents in order to avoid a 100-branch ifelse tree.

Code: Select all

init:
    image image1="someimage"
    image image2="otherimage"
    $ image_array=["image1","image2"]

    transform pos1:
        xalign=0.2 yalign=0.5
    transform pos2:
        xalign=0.8 yalign=0.5
    $ pos_array=["pos1","pos2"]

elsewhere:
    python:
        rand=renpy.random.randint(0,1)
        renpy.show(image_array[rand],pos_array[rand])

        renpy.hide(image_array[rand])
(The actual use-case is considerably more complicated)

It seems that the renpy.hide() statement works fine, so it appears that referring to images that way works, but it doesn't seem to work for the transform. Also, I'm not sure how I would go about showing an image with a transition programmatically. There's clearly something I'm missing about how to handle renpy actions from within a python block.


Thanks in advance.
Last edited by Queex on Fri Mar 11, 2016 12:53 pm, edited 1 time in total.

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: Python statement equivalents

#2 Post by xela »

You can do something along these lines (totally untested) but you can just forgo declaring the images with Ren'Py and plainly randomly draw from a list of paths. I always do...

Code: Select all

$ images = []
for n, i in enumerate(["paths to images"]):
    tag = "image" + str(n)
    renpy.image(tag, i)
    images.append(tag)
ATL Functions can be just thrown and a list and renpy.random.choice() from that list same as with images, just don't call them (aka just the names without brackets).
Like what we're doing? Support us at:
Image

User avatar
Queex
Regular
Posts: 46
Joined: Sat Mar 14, 2015 9:47 am
Contact:

Re: Python statement equivalents

#3 Post by Queex »

Well, the random part isn't actually relevant; what I'm having trouble with is duplicating:

Code: Select all

show someimage at sometransform with sometransition
in a python block, as I don't think renpy statements support lists (or, at last, I can't find a way to define such lists in a renpy-friendly manner).

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: Python statement equivalents

#4 Post by xela »

I thought they did... in any case:

renpy.show(tag, what=displayable, at_list=[Transforms or atl funcitons])
Like what we're doing? Support us at:
Image

User avatar
Queex
Regular
Posts: 46
Joined: Sat Mar 14, 2015 9:47 am
Contact:

Re: Python statement equivalents

#5 Post by Queex »

Okay, so to do that:
How can I get a Transform object (which is what the at_list is expecting, based on the error message) when the Transform is defined in a renpy statement?
or
How can I define a Transform object in a python block? It looks like a Transform object is specific to a child displayable, so would have to be initialised afresh whenever called on a different image, making a different beast entirely to a renpy-defined transform.

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: Python statement equivalents

#6 Post by xela »

I am not sure that I follow...

These are some examples from my project:

Code: Select all

$ renpy.show("_tag", what=Text("%d" % wage, style="back_serpent", color=gold, size=40, bold=True), at_list=[found_cash(150, 600, 2)])
Ren'Py statement would be:

Code: Select all

show expression Text("%d" % wage, style="back_serpent", color=gold, size=40, bold=True) at found_cash(150, 600, 2) as _tag
which I prolly should have used here.

Here is one deep within Python showing both, a Transform and an ATL Function with instructions:

Code: Select all

            for index, target in enumerate(targets):
                aimpos = battle.get_cp(target, type="center")
                renpy.show("launch" + str(index), what=missle, at_list=[move_from_to_pos_with_easeout(start_pos=initpos, end_pos=aimpos, t=pause), Transform(anchor=(0.5, 0.5))], zorder=target.besk["zorder"]+50)

Code: Select all

    python:
        n = Character(" ")
        img = get_random_event_image("simple_beach")
        renpy.show("event", what=img, at_list=[center])
        renpy.with_statement(dissolve)
And the function (not safe for android, renpy.list_files should be used instead) (content path is just a func returning os path leading to game/content folder) (ProportionalScale is a class returning resized image no properly scaled to take the largest of two sides and scale the other one preserving the true image ratio):

Code: Select all

init -9 python:
    def get_random_event_image(eventfolder):
        templist = []
        if eventfolder in os.listdir(content_path('events')):
            for file in os.listdir(content_path('events/%s' % eventfolder)):
                    templist.append('content/events/%s/%s' % (eventfolder, file))
            return ProportionalScale(choice(templist), config.screen_width, config.screen_height)
Like what we're doing? Support us at:
Image

User avatar
Queex
Regular
Posts: 46
Joined: Sat Mar 14, 2015 9:47 am
Contact:

Re: Python statement equivalents

#7 Post by Queex »

Ok, many thanks, I think I've got a better handle on what's going on. With some significant experimentation, I've got it behaving. I think my problem was how renpy.show parses the arguments; the image you use can be given as a string that names a renpy image object, but you can't do the same thing for the transform, plus some confusion I had about the difference between the 'name' and 'what' arguments which I think I've got straightened out now.

Cheers!

Post Reply

Who is online

Users browsing this forum: Baidu [Spider], Bing [Bot], Google [Bot]