Page 1 of 1

User defined function for code efficiency

Posted: Sat Jul 07, 2018 4:20 pm
by ChroniclerOfLegends
Hello, hopefully this will be a simple question. I tried searching for an answer and found a few things that were close but not exactly what I was looking for.

I have a small block of renpy code that is going to be used a lot in my game, and I was hoping to make it into a function so that I can reduce it down to 1 line whenever I need to use it:

Code: Select all

def RelationshipChange(relationship, amt):
        show screen affection_bar
        with dissolve
        $ relationship_currentValue = AnimatedValue(value=(relationship+amt), range=relationship_max, delay=1.5, old_value=relationship)
        $ relationship += amt
        pause 2.5 
        hide screen affection_bar
        with dissolve
This just simply shows a custom relationship bar that I have made, shows the value changing, and then hides it again.
I have already tried the above code, but I am coming back with this error:

Code: Select all

I'm sorry, but errors were detected in your script. Please correct the
errors listed below, and try again.


File "game/source/scenes/scene_debug.rpy", line 99: expected statement.
    RelationshipChange(temp_value, -25)
                                       ^

File "game/source/screens/screen_affectionbar.rpy", line 20: expected statement.
    def RelationshipChange(relationship, amt):
                          ^

Ren'Py Version: Ren'Py 6.99.14.3.3347
Sat Jul 07 15:18:16 2018
EDIT:
I got it partially working with this code, but the change to relationship_currentValue stays inside the scope of the function and doesn't affect the outer variable:

Code: Select all

init python:
    def RelationshipChange(original, amt):
        renpy.show_screen('affection_bar')
        renpy.transition(dissolve)
        relationship_currentValue = AnimatedValue(value=(original+amt), range=relationship_max, delay=1.5, old_value=original)
        original += amt
        renpy.pause(2.5)
        renpy.hide_screen('affection_bar')
        renpy.transition(dissolve)
        return original

Re: User defined function for code efficiency

Posted: Sat Jul 07, 2018 5:27 pm
by Saithir
Yeah, that's not gonna work. You're trying to mix two things here - take a bunch of screen language commands and wrap them in a python function. You need to stick to python only and for that you need to replace all your "show screen x" with "renpy.show_screen('x')" and so on.

So you would have (just a start without all the fanciness):

Code: Select all

init python:
    def RelationshipChange(amt):
        global relationship
        renpy.show_screen("affection_bar")
        relationship += amt
        renpy.pause(2.5)
        renpy.hide_screen("affection_bar")
And then use it wherever:

Code: Select all

$ RelationshipChange(-25)
You need to go through the docs and see how to do with dissolve and whatever's that AnimatedValue though. Here's a start point: https://www.renpy.org/doc/html/statemen ... lents.html

Re: User defined function for code efficiency

Posted: Sat Jul 07, 2018 5:30 pm
by Saithir
Ah wait that's wrong actually.

You either want to pass a string name of the variable you want to change:

Code: Select all

def Function(var_name):
    real_var = eval(var_name)
    real_var += 50
And you run this with $ Function("my_awesome_variable") where somewhere else you got $ my_awesome_variable = 15 for example.

or use global like I did above:

Code: Select all

def Other(amount):
   global some_global_value
   some_global_value += amount

Re: User defined function for code efficiency

Posted: Sun Jul 08, 2018 12:58 am
by ChroniclerOfLegends
Thanks for the suggestions, making relationship_currentvalue global got it working properly.
It makes sense for it to be global too. It is a temporary variable used by all characters to animate the affection bar when it changes.

Final working version:

Code: Select all

init python:
    def RelationshipChange(original, amt):
        global relationship_currentValue
        renpy.show_screen('affection_bar')
        renpy.transition(dissolve)
        relationship_currentValue = AnimatedValue(value=(original+amt), range=relationship_max, delay=1.5, old_value=original)
        original += amt
        renpy.pause(2.5)
        renpy.hide_screen('affection_bar')
        renpy.transition(dissolve)
        return original
Thanks for the help!
Now I have a really cool affection bar working.