simulating a loop in renpy ?

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
jack_norton
Lemma-Class Veteran
Posts: 4084
Joined: Mon Jul 21, 2008 5:41 pm
Completed: Too many! See my homepage
Projects: A lot! See www.winterwolves.com
Tumblr: winterwolvesgames
Contact:

simulating a loop in renpy ?

#1 Post by jack_norton »

Ok I decided to open a thread specific for this. After learning a bit of python and renpy ui, I figured out almost everything except one thing: how to implement a loop in Renpy.
This is what I do with my C code (very stupid example I use to blink every 1,5 secs):

Code: Select all

	if ((KMiscTools::getMilliseconds()-IntroTimer)>1500)
	{
		IntroTimer=KMiscTools::getMilliseconds();
		MiscV.CBlink=!MiscV.CBlink;
	}
basically I would need to be able to assign a timer value to 1 or more variable and do actions (python stuff) every XX interval (which I should be able to choose).
This has several implementation like showing HP bars that smoothly fill up/down based on a variable that changes or other things like doing some real-time stuff behind the scenes (I'm not talking about a standard VN here).

So the basic question is: is that possible somehow ? 8)
follow me on Image Image Image
computer games

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: simulating a loop in renpy ?

#2 Post by PyTom »

There's basically two ways of doing this in Ren'Py. The first would be to use renpy.pause with a small or zero delay. The following code fills up a bar, having frames that nominally last .01 seconds.

Code: Select all

init python:
    import time

python:
    
    # range - the max value of the bar.
    # start - the starting value of the bar.
    # end - the ending value of the bar.
    # delay - the time it takes to show the bar.

    start = time.time()
    while True:
        fraction = (time.time() - start) / delay
        if fraction >= 1.0:
            break


        ui.bar(range, start + fraction * (end - start), ...)
        renpy.pause(.01)
I don't really recommend it, though. since starting a new interaction is a lot of work. Instead, it's best to use DynamicDisplayable, along with a much longer renpy.pause.

Code: Select all

init python:
    def increase_bar(range, start, end, delay, st, at):
        fraction = st / delay
        return Bar(range, start - fraction * (end - start), ...), .01
    
     curried_increase_bar = renpy.curry(increase_bar)

python:
    ui.add(DynamicDisplayable(curried_increase_bar(range, start, end, delay)))
    renpy.pause(delay)
Basically, the function is called at the start of each interaction. It's expected to return a pair containing two things:

* A displayable to show to the screen.
* The time until the displayable should change. The function will be called again in this amount of time, to get a new displayable.

Fundamentally, Ren'Py keeps control of the main loop of the program. This lets it handle things like image preloading, sound/music, and so on.
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
jack_norton
Lemma-Class Veteran
Posts: 4084
Joined: Mon Jul 21, 2008 5:41 pm
Completed: Too many! See my homepage
Projects: A lot! See www.winterwolves.com
Tumblr: winterwolvesgames
Contact:

Re: simulating a loop in renpy ?

#3 Post by jack_norton »

Ah thanks, at least found a solution for my bars :)
I thought about using the dynamicdisplayable but was having wrong approach, I was thinking to redraw everytime everything (the whole frame+ui.texts+buttons etc) and not just the changing part...
A thing I didn't understood about dynamic displayables is if I can build up my custom displayable (like a sequence of ui. functions to display a status screen with buttons, bars, etc) and reference it as a single entity through renpy ?
follow me on Image Image Image
computer games

User avatar
jack_norton
Lemma-Class Veteran
Posts: 4084
Joined: Mon Jul 21, 2008 5:41 pm
Completed: Too many! See my homepage
Projects: A lot! See www.winterwolves.com
Tumblr: winterwolvesgames
Contact:

Re: simulating a loop in renpy ?

#4 Post by jack_norton »

In the end I did it in the "less recommended way", using the while True loop. Tested in the game though looks great, and I see no particular slowdown, and even rollback seems to work fine 8)
follow me on Image Image Image
computer games

Post Reply

Who is online

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