How to use screen language's 'If'? [answered&solved]

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
fluxus
Regular
Posts: 133
Joined: Thu Jun 19, 2014 8:06 am
Projects: Animal Anaesthesia (a teaching game)
Contact:

How to use screen language's 'If'? [answered&solved]

#1 Post by fluxus »

Okay so... for various reasons, where various means I-want-a-screen-that-calls-a-screen-to-update-my-data,
I'd like to run an action (specifically, a function) depending on whether or not I provide the object to update.

Happily, I found the If function.

So I'm trying this:

Code: Select all

If(False, true=update.selfadjust, false=None)
...and if complains that object of type None has no function called 'selfadjust'. And rightly so, because the parameter 'update' was None in this case, since that is the default value.

But.

If ought to evaluate to false.
So..
There's no lazy evaluation here, 'If' evaluates both statements regardless of the test condition?
Last edited by fluxus on Wed Sep 17, 2014 1:01 pm, edited 1 time in total.

User avatar
fluxus
Regular
Posts: 133
Joined: Thu Jun 19, 2014 8:06 am
Projects: Animal Anaesthesia (a teaching game)
Contact:

Re: How to use screen language's 'If'?

#2 Post by fluxus »

And.. well, under the assumption that I'm right, I wrote a work-around class which solves my problem and makes things work, but, well.. it also kind of renders the If redundant, so I'm still interested in answers / better solutions.

Code: Select all

    class NoUpdate:
        def __init__(self):
            pass
        
        def selfadjust(self):
            pass

User avatar
PyTom
Ren'Py Creator
Posts: 16096
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: How to use screen language's 'If'?

#3 Post by PyTom »

What exactly are you trying to do? It seems like you could just use general python code.

Code: Select all

screen blah(update):

    python:
        if update is not None:
            action = update.selfadjust
        else:
            action = None

    textbutton "Self-Adjust" action action
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

User avatar
fluxus
Regular
Posts: 133
Joined: Thu Jun 19, 2014 8:06 am
Projects: Animal Anaesthesia (a teaching game)
Contact:

Re: How to use screen language's 'If'?

#4 Post by fluxus »

Eh..?
You can use python code directly while inside a screen?
Damn.

Could you pen that down for inclusion in the documentation effort for 6.18?
I have been jumping a significant amount of hoops back and forth between SL, script and classes in order to get things functioning - looks like life just got a lot easier.

Anyway, this was the entire code for the screen in question:

Code: Select all

screen verify_move(container, from_inventory, to_inventory, text, update=NoUpdate()):
    zorder 2
    frame:
        xsize .5
        ysize .3
        xalign .5
        yalign .8
        vbox:
            text text.format(container.name) style "selection_caption" xalign 0.5
            button:
                style "selection_choice_button"
                text "Ja" style "selection_choice"
                action [    renpy.curry(to_inventory.add)(container), 
                            renpy.curry(from_inventory.remove)(container), 
                            If(update, true=update.selfadjust, false=None),
                            Return("Done") ]
            button:
                style "selection_choice_button"
                text "Nej" style "selection_choice"
                action Return("Done")
...Aaaand in case anyone is wondering, what this does is to move one item from one inventory to another inventory. If the player chooses Yes/Ja, that is.

User avatar
PyTom
Ren'Py Creator
Posts: 16096
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: How to use screen language's 'If'?

#5 Post by PyTom »

It's in the documentation.

The one restriction is the Python code can't have side effects, since it's run as part of the screen prediction process.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

User avatar
fluxus
Regular
Posts: 133
Joined: Thu Jun 19, 2014 8:06 am
Projects: Animal Anaesthesia (a teaching game)
Contact:

Re: How to use screen language's 'If'?

#6 Post by fluxus »

You're right, it is, apologies. While I've read a good portion of it in order to know the various possibilities at the beginning, later on I've generally read the documentation on a need-to-know basis. I think this was the biggest surprise thus far, though.

About the side effects, I could still sort a list into smaller lists by item type through in-screen python; that alone means I can slaughter a class I specifically wrote for overcoming that problem.
Though arguably the whole data structure ought to be changed from using a list to using a dict.
No time, though :]

Thanks for letting me know - my next project is going to be so much easier what with all I've learned while doing this one :>

Post Reply

Who is online

Users browsing this forum: Semrush [Bot]