Using renpy function names and variables in an external 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
Errilhl
Regular
Posts: 164
Joined: Wed Nov 08, 2017 4:32 pm
Projects: HSS
Deviantart: studioerrilhl
Github: studioerrilhl
Contact:

Using renpy function names and variables in an external function?

#1 Post by Errilhl »

Okay, I'm trying to move logic out of the script.rpy file, and into a functions.rpy file. What I'm having trouble with is using renpy-specific syntax and such in that file - although (in my opinion) it should work, since the syntax / function is only being called inside the label start:

I'm guessing that is a mistake on my part.

So... is there any way to do what I want to do? Keep the functions callable, but in a separate file, while still being able to use things like label label_name: and character shortcuts for say-statements?
Currently working on: Image

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: Using renpy function names and variables in an external function?

#2 Post by trooper6 »

Renpy treats all .rpy files as one big file. So there shouldn't be a problem where you place the functions. Perhaps you are not coding the functions properly? How are you coding them and how are you calling them?
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

Errilhl
Regular
Posts: 164
Joined: Wed Nov 08, 2017 4:32 pm
Projects: HSS
Deviantart: studioerrilhl
Github: studioerrilhl
Contact:

Re: Using renpy function names and variables in an external function?

#3 Post by Errilhl »

The current attempt was this:

Code: Select all

 def w_mc(firstday=False):
        def _work_on_bike():
            if day_week <= 4:
                if mc_b >= 40 and mc_b < 100:
                     c = .3
                elif mc_b >= 100 and mc_b <= 150:
                     c = .5
                else:
                     c = .6
                if renpy.random.random() > c:
                    if mc_b <= 40:
                         mcI = 1
                    elif mc_b <= 75:                        
                         mcI = .5
                    else:
                         mcI = .25
                    statschangeNotify("mc_b",mcI)
            else:
                label work_on_bike:
                    if sat_count < 3:
                        if mc_b >= 40 and mc_b < 100:
                             c = .35
                        elif mc_b >= 100 and mc_b <= 150:
                             c = .55
                        else:
                             c = .65
                        if renpy.random.random() > c:
                            if mc_b <= 50:
                                 mcI = 1.5
                            elif mc_b <= 90:
                                 mcI = 1
                            else:
                                 mcI = .75
                            statschangeNotify("mc_b",mcI)
                        sat_count += 1
                        if sat_count == 1:
                             sc = str(sat_count) + " hour"
                        else:
                             sc = str(sat_count) + " hours"
                        "I've already worked on this bike for [sc] today"
                        if sat_count == 3:
                            fP "It's late, and probably time to call it a day"
                            jump sat_end
                        else:
                            jump work_on_bike
                    else:
                        fP "It's late, and probably time to call it a day"
                        sat_count = 0
This is done in a separate file: functions.rpy
It does have a init -10 python: at the very start, if that matters
Currently working on: Image

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: Using renpy function names and variables in an external function?

#4 Post by trooper6 »

Yeah...that isn't how you make a function.

Also maybe you don't want a function at all but just a label that you call?

Could you explain in plain english what you want to happen with this work on bike label?
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

Errilhl
Regular
Posts: 164
Joined: Wed Nov 08, 2017 4:32 pm
Projects: HSS
Deviantart: studioerrilhl
Github: studioerrilhl
Contact:

Re: Using renpy function names and variables in an external function?

#5 Post by Errilhl »

It's basically just a repeatable event that I want to take out of the script.rpy code, and call that whenever I need to. I've managed to have it work as I want by reducing the amount of code, and just placing it before the label start:
Currently the code is this:

Code: Select all

init python:
    def w_mc():
        c = 0
        mcI = False
        mc_b = getattr(store,"mc_b")
        if mc_b >= 40 and mc_b < 100:
            c = .35
        elif mc_b >= 100 and mc_b <= 150:
            c = .25
        else:
            c = .5
        if renpy.random.random() > c:
            if mc_b <= 40:
                mcI = 1
            elif mc_b <= 75:                        
                mcI = .5
            else:
                mcI = .25
        if mcI:
            statschangeNotify("mc_b",mcI)
right before the label start:, and it works fine. The rest of the dialogue and such is kept out, although it would of course be better if I could put most of it back into the function. You stated that "this is not how you make a function"... then my question becomes... why not? A function, to me, is something containing repeatable code, that you call on instead of having the actual code repeated several times over?
Currently working on: Image

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: Using renpy function names and variables in an external function?

#6 Post by trooper6 »

You can also use labels as repeatable code that you call instead of having the actual code repeated several times over.

1) In your original code you tried to define a function in a function (w_mc and _work_on_bike). That is no good. Define one function at a time.

2) Please note that unless you declare a variable global within your function it is not going to access some larger variable, it creates its own.

So..

Code: Select all

default coins = 0

init python:
    def addCoins():
        coins +=1
    	
label start:
    "How many coins? [coins]" #It will say 0
    $addCoins()
    "How many coins? [coins]" #It will still say 0
Your function here will not increase your coins variable...because by default the function creates its own variable unless you tell it to access a global variable.

Rather to access the larger variables, you'd:

Code: Select all

default coins = 0

init python:
    def addCoins():
    	global coins
        coins +=1
    	
label start:
    "How many coins? [coins]" #It will say 0
    $addCoins()
    "How many coins? [coins]" #It will still say 1
You have a bunch of variables in your function...are any of these variables that live outside of the function? Because you'll want to declare them global or it won't work.

3) You have a bunch of stuff that really looks like label stuff to me rather than function stuff.
You can have a file called functions.rpy or calls.rpy or whatever.rpy and put in a label, that you then call to.

in your script.rpy file

Code: Select all

label start:
    "I'm walking down the street."
    menu choices:
    	"What do I do today?"
    	"Go fishing":
    	    call fishing()
    	"Go shopping":
    	    call shopping()
    	    
    "That was nice!"    	  
In your other file:

Code: Select all

label fishing():
    "I love fishing!"
    "I fish for 10 days."
    "I come back with a fish"
    return
    
label shopping():
    "I love shopping!"
    "I shop for 10 days."
    "I come back with presents"
    return    
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

Errilhl
Regular
Posts: 164
Joined: Wed Nov 08, 2017 4:32 pm
Projects: HSS
Deviantart: studioerrilhl
Github: studioerrilhl
Contact:

Re: Using renpy function names and variables in an external function?

#7 Post by Errilhl »

Hm, okay. I will try it again :)
And yeah, I figured that it might have to do with scope - although, I've seen function declared within functions before, for only internal use - seems to be working fine?

Also, instead of declaring globals (which I really don't like to do), I could be using setattr() and getattr(), couldn't I? Or are those slower / worse than declaring a global? (Normally, I'm used to declaring globals being a no-no) :)
Currently working on: Image

Errilhl
Regular
Posts: 164
Joined: Wed Nov 08, 2017 4:32 pm
Projects: HSS
Deviantart: studioerrilhl
Github: studioerrilhl
Contact:

Re: Using renpy function names and variables in an external function?

#8 Post by Errilhl »

A follow up question:
I was trying to do

Code: Select all

show ingame_menu_screen
from within my function (which is inside a init python call.
That does not work, and returns a "invalid syntax" error
Any way I can load the screen, reload the screen, update the screen in question from within the function?
Currently working on: Image

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: Using renpy function names and variables in an external function?

#9 Post by trooper6 »

global is not a no-no, this isn't Java. You can just use global.

As for your followup question you are trying to use Renpy language in Python and that is a no-no. You need to use Python equivalents:
https://www.renpy.org/doc/html/screen_python.html

Though I do have some concerns that you are trying to do things in inefficient ways.
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

Errilhl
Regular
Posts: 164
Joined: Wed Nov 08, 2017 4:32 pm
Projects: HSS
Deviantart: studioerrilhl
Github: studioerrilhl
Contact:

Re: Using renpy function names and variables in an external function?

#10 Post by Errilhl »

Probably. I ended up doing it via a label, and using call labelname instead.
Current code:

Code: Select all

label w_mc:
        $ c = 0
        $ mcI = False
        if mc_b >= 40 and mc_b < 100:
            $ c = .35
        elif mc_b >= 100 and mc_b <= 150:
            $ c = .25
        else:
            $ c = .5
        if renpy.random.random() > c:
            if mc_b <= 40:
               $ mcI = 1
            elif mc_b <= 75:                        
               $ mcI = .5
            else:
               $ mcI = .25
        if mcI:
            $ statschangeNotify("mc_b",mcI)
        $ hour = getattr(store,"current_hour")
        $ current_hour = str(int(hour[:2])+1)+hour[2:]
        $ current_hour = current_hour.rjust(5,'0')
        show screen ingame_menu_display
Currently working on: Image

Post Reply

Who is online

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