Guys can you explain what is Function() used for?
action(Function())
Function ?
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.
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.
Re: Function ?
Code: Select all
textbutton "Click me" action Function(print,"clicked!")
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
Re: Function ?
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.
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:
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...
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.
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'.
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:
If you think is a mess, IT'S complicated and probably the more complicated action you can use in a screen.
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)
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)
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!"
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
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)
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 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
10 ? "RENPY"
20 GOTO 10
RUN
Who is online
Users browsing this forum: Bing [Bot], Google [Bot]