Function ?

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
iDweadith
Regular
Posts: 63
Joined: Sun Mar 01, 2020 4:15 pm
Contact:

Function ?

#1 Post by iDweadith » Sat May 23, 2020 5:29 am

Guys can you explain what is Function() used for?

action(Function())

drKlauz
Veteran
Posts: 237
Joined: Mon Oct 12, 2015 3:04 pm
Contact:

Re: Function ?

#2 Post by drKlauz » Sat May 23, 2020 7:49 am

Code: Select all

textbutton "Click me" action Function(print,"clicked!")
Used to call existing function in cases where builtin actions are not enough.
First argument is function (any callable should work, tho doubt it will work properly with lambdas), rest are arguments supplied to this function.
https://www.renpy.org/doc/html/screen_a ... l#Function

Common use cases:
- changing lots of variables
- showing/hiding lots of screens
- doing something if result of some calculations is right
I may be available for hire, check my thread: viewtopic.php?f=66&t=51350

User avatar
gas
Miko-Class Veteran
Posts: 838
Joined: Mon Jan 26, 2009 7:21 pm
Contact:

Re: Function ?

#3 Post by gas » Sun May 24, 2020 8:00 am

Above is right.

For the VERY newbie...
A function is a snippet of PYTHON code that process some line, usually getting an input and throwing an output.
The function can then be called at will to do repetitive stuff.

Code: Select all

init python:
    def dice_roll(min,max):
        return renpy.random.randint(min,max)
you've created a function called 'dice_roll'. The function do nothing until you'll use it and pass any value required. Now you can use it at will when needed.
As you can see, it require two parameters, min and max, that you must give when calling the function, and return something (in that case, the random range between them).

So you can do:

Code: Select all

$ dice = dice_roll(1,6)
and now 'dice' is the function return, so a number from 1 to 6.
Python is smart enough to compute the function immediatedly if you'll quote it, so you can do something like...

Code: Select all

if dice_roll(1,6) >3:
    e "You rolled above 3!"
If you don't set any 'return', the function automatically return 'None' on his own.

NOW, a function can also operate on global variables. So for example return nothing but manipulate some value when called.

Code: Select all

default counter = 0
init python:
    def update():
        counter +=1

label start:
    e "Enter a loop!"
label loop:
    $ update()
    e "The value is now [counter]!"
    jump loop
As you can see there's no 'return' and no parameter, the function simply add 1 to an existing variable and return 'None' (no value to store somewhere in the end).

The Function() action work like this. Call a function, operate on some global variable and return 'None'.

Code: Select all

default score = 0

init python:
    def update_score(amount):
        score += renpy.random.randint(1*amount,6*amount)

screen myscreen():
    imagebutton auto "img_%s.png" action Function(update_score, 3)
Clicking on the button add the roll of 3 six faced die to the score. And that's all.

NOW, if you instead of showing you CALLED the screen, the Function action is like a Return(), that close the screen and _return the function output.
So, in the above case, you can do:

Code: Select all

    call screen mydie()
    $ dice = _return
If you think is a mess, IT'S complicated and probably the more complicated action you can use in a screen.
If you want to debate on a reply I gave to your posts, please QUOTE ME or i'll not be notified about. << now red so probably you'll see it.

10 ? "RENPY"
20 GOTO 10

RUN

Post Reply

Who is online

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