Run Function Multiple Times? Over and Over again, in a loop?

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
Discostar
Regular
Posts: 40
Joined: Mon Mar 10, 2014 10:29 am
Contact:

Run Function Multiple Times? Over and Over again, in a loop?

#1 Post by Discostar »

Is there a way to run a Function multiple times?

Let's say I have a little function

Code: Select all

def MyFunc():
	x = 0
	if x > 5:
	    y = 5
	return
Let's just say, hypothetically, the above code works.

I want to trigger MyFunc() 10 times in a row. Just... over and over and over again. Like a madman!
In a loop, if you will.

Any ideas how I might do that?

User avatar
ComputerArt.Club
Veteran
Posts: 427
Joined: Mon May 22, 2017 8:12 am
Completed: Famous Fables, BoPoMoFo: Learn Chinese, Santa's workshop, Cat's Bath, Computer Art Club
Location: Taiwan
Contact:

Re: Run Function Multiple Times? Over and Over again, in a loop?

#2 Post by ComputerArt.Club »

try

Code: Select all

repeat 10
instead of return

or

when you call it

Code: Select all

show monster:
   jumpupanddown
   repeat 10
Last edited by ComputerArt.Club on Tue Feb 19, 2019 1:58 am, edited 1 time in total.

philat
Eileen-Class Veteran
Posts: 1900
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: Run Function Multiple Times? Over and Over again, in a loop?

#3 Post by philat »

You can also use a while loop.

User avatar
Discostar
Regular
Posts: 40
Joined: Mon Mar 10, 2014 10:29 am
Contact:

Re: Run Function Multiple Times? Over and Over again, in a loop?

#4 Post by Discostar »

I saw some things about the "while loop", but I didn't understand how to properly use it.

I ended up experimenting with repeat.

Thank you both!

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Run Function Multiple Times? Over and Over again, in a loop?

#5 Post by Remix »

$ [ MyFunc() for k in range(10) ]

... and a little snippet to show it works as expected

Code: Select all

init python:

    num_calls = 0

    def test_func():

        global num_calls

        num_calls += 1

        renpy.notify('called {} times'.format(num_calls))


label start:

    $ [test_func() for k in range(10)]

    "..."
Frameworks & Scriptlets:

Post Reply

Who is online

Users browsing this forum: No registered users