screen: how to run a function conditionally?

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
unregistered
Newbie
Posts: 17
Joined: Tue Apr 21, 2020 5:18 am
Contact:

screen: how to run a function conditionally?

#1 Post by unregistered »

I have a little issues getting a function to run properly in a screen after an if statement.

There is an if statement in the screen and if it's true, it should show some screen elements and run a function in the background that does some calculations for me.

The function takes one argument and this is the code snippet:

Code: Select all

screen name():
    modal True    

                if variable1:                
                    $ functionname(arg)
                    hbox:                    
(...)
Now, if variable1 is false, all the screen elements below it are invisible, as it should be.

However, the function is always run, no matter the boole of the variable. I know that this might be true for functions without arguments, but is it also true for functions with arguments?

I also tried using the on statement replacing the function with

Code: Select all

                    on "show" action [Function(functionname,arg)]
But in this case, the function is just never run for some reason.

Any ideas, how I should proceed? Thanks!


Edit:

It seems a timer does work:

Code: Select all

                    timer 0.01 action [Function(functionname,arg)]
Obviosuly, I want the function to run immediately, so this doesn't feel really clean, though. Is there a better way?

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2428
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: screen: how to run a function conditionally?

#2 Post by Ocelot »

Any statement in screen body can be executed at any time, even before or after screen is shown, as part of screen prediction. If that makes a difference for you, then you are doing it wring: documentation explicitely states that screen code should not have any side effects.

If you want function to run only when screen is show, your option are action of short-lived time. Alternatively you can use on "show" and/or "replace" events.
< < insert Rick Cook quote here > >

unregistered
Newbie
Posts: 17
Joined: Tue Apr 21, 2020 5:18 am
Contact:

Re: screen: how to run a function conditionally?

#3 Post by unregistered »

But not having something changing on the screen when it's shown is exactly what I want (see my first code example). The function should not only run when the screen is shown. It can and should be executed anytime the conditional is true and should be part of the screen prediction. The problem is, the function is executed no matter the conditional I give.

That's the reason why I want to avoid the timer, as it does the opposite and only does things when the screen is shown (giving me risks of the screen changing, flickerings, whatever...). :-)

And how do I use an action command without a short timer?

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2428
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: screen: how to run a function conditionally?

#4 Post by Ocelot »

RenPy ignores the condition when executing screen prediction to check for resources which might be nessessry in the future (and it could be executed anytime, even when screen is not even mentioned anywhere in the script).

You can add condition check to your function instead. If that function is used somewhere else, where that check would brreaak thing, whap that function in another one, which does the check and conditionally executes another function.
< < insert Rick Cook quote here > >

Post Reply

Who is online

Users browsing this forum: Google [Bot]