User defined function for code efficiency

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
ChroniclerOfLegends
Regular
Posts: 53
Joined: Thu Feb 16, 2017 5:57 pm
Projects: Scifi Non-Linear Visual Novel/Arcade/Adventure game. (No name yet)
Contact:

User defined function for code efficiency

#1 Post by ChroniclerOfLegends » Sat Jul 07, 2018 4:20 pm

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

Saithir
Newbie
Posts: 11
Joined: Mon Jun 25, 2018 4:27 pm
Contact:

Re: User defined function for code efficiency

#2 Post by Saithir » Sat Jul 07, 2018 5:27 pm

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
Last edited by Saithir on Sat Jul 07, 2018 5:31 pm, edited 3 times in total.

Saithir
Newbie
Posts: 11
Joined: Mon Jun 25, 2018 4:27 pm
Contact:

Re: User defined function for code efficiency

#3 Post by Saithir » Sat Jul 07, 2018 5:30 pm

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

User avatar
ChroniclerOfLegends
Regular
Posts: 53
Joined: Thu Feb 16, 2017 5:57 pm
Projects: Scifi Non-Linear Visual Novel/Arcade/Adventure game. (No name yet)
Contact:

Re: User defined function for code efficiency

#4 Post by ChroniclerOfLegends » Sun Jul 08, 2018 12:58 am

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.

Post Reply

Who is online

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