Functions with parameters

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.
Message
Author
monele
Lemma-Class Veteran
Posts: 4101
Joined: Sat Oct 08, 2005 7:57 am
Location: France
Contact:

Functions with parameters

#1 Post by monele »

As a way to speed up coding and ease readability, I was wondering if there was a way to use functions including parameters that would execute RenPy code (such as "scene", "show", "hide" and such).

I have this code :

Code: Select all

show sp_ship w at ship_pos
    $renpy.pause(0.01)
    hide sp_ship
    show sp_ship at ship_pos
and might want to use the same "animation" for another sprite... or for the same sprite but at another position.
(in that last case I'd edit ship_pos, but in the first case...?)

Can a python block do that ? I know a "call" won't do since it's just for procedures :/...

EDIT : btw, how does ImageReference work? ô_o;

User avatar
PyTom
Ren'Py Creator
Posts: 16096
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

#2 Post by PyTom »

The functions renpy.scene, renpy.show, and renpy.hide correspond to the scene, show, and hide statements, respectively. The big issue with them is they take an image name as a tuple of strings.

So

Code: Select all

show sp_ship w at ship_pos 
Becomes:

Code: Select all

$ renpy.show(('sp_ship', 'w'), at_list=[ship_pos])
This can be placed into a python function, with code like:

Code: Select all

init:
    python:
        def showship():
             renpy.show(('sp_ship', 'w'), at_list=[ship_pos])
I'm sort of interested in what you're trying to do, since in your example, the hide statement doesn't accomplish anything, as there's no interaction taking place between when the ship is hidden and when the ship is shown again.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

monele
Lemma-Class Veteran
Posts: 4101
Joined: Sat Oct 08, 2005 7:57 am
Location: France
Contact:

#3 Post by monele »

I realize I should just tell what I'm trying to do XD.. Will be easier to get help too :). So here it goes :

The show/hide stuff is just to make a one time blink animation. The sequence is :
greenversion, hidden, greenversion, normalversion, greenversion, normalversion.

(well that's one example at least).

It then stays on normalversion of course. Would SM.animation or any of this kind be better for this ? (I suppose it would but I just went the rough way for tests...)


And the use of functions would be to create a template animation or transitions sequence and be able to use it with different base images.

In the present case I have an overlay grid appearing over whatever background is already there and *then* the map appearing underneat

Code: Select all

show grid with dissolve
scene map
show grid
with dissolve
and then I make the map disappear :

Code: Select all

scene previousbackground with dissolve
It's that "previousbackground" that I'm having a problem with. Prevents me from using a "call" because I'll have to define what was the previous background in it... and it depends on the previous context ^^;... A solution would be to store the previous background name in a variable and use it to display it again later. I thought ImageReference could help but I don't understand how it works :/

User avatar
PyTom
Ren'Py Creator
Posts: 16096
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

#4 Post by PyTom »

ImageReference isn't what you want. (In fact, it really shouldn't be documented at all.)

For a blink like that, I'd just use straight up Animation. If the argument list ends with an image, then the animation will not loop. (Actually, will take a year to loop, IIRC.)

As to the scenes, Ren'Py doesn't allow variable interpolation into script statements. So you'll have to write it in python, using something like:

Code: Select all

$ renpy.with(None)
$ renpy.scene()
$ renpy.show(previousbackground.split())
$ renpy.with(dissolve)
(This assumes that previousbackground is a space-separated string, like "bg caprica city night".

That being said, it's not obvious that this is worthwhile to save no lines of script. (-1 line scene, +1 line to set previousbackground.)
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

monele
Lemma-Class Veteran
Posts: 4101
Joined: Sat Oct 08, 2005 7:57 am
Location: France
Contact:

#5 Post by monele »

I suppose that if I have 15 different sprites I want to blink in the exact same way, I'll still have to create 15 animation statements even though only the image reference changes ? :/


I'll try the python code tonight, thanks. And the idea is not to gain 1 line but to avoid repeating a set of commands all over my scripts for dozens of pictures. If I happen to change my mind and use a different transition, for example, I'd have to go through each copy of this sequence to update the scripts. If they all use one single function, I only change this function.

Oh btw, for the map+grid thing, I decided not to use "scene" but just "show" instead. This way the whole thing is displayed above the background and I just hide the map and grid when I'm done with them, revealing the previous background underneath. This lets me use a simple call so I'll try to stick to this whenever possible but I know I won't be able to avoid the use of real functions at times ^^

Watercolorheart
Eileen-Class Veteran
Posts: 1314
Joined: Mon Sep 19, 2005 2:15 am
Completed: Controlled Chaos / Sum of the Parts / "that" Midna game with ZONEsama
Projects: Sparse Series/Oddments Shop original cartoon in Pevrea; Cybernetic Duels (fighting game); Good Vibin'
Organization: Watercolorheart Studios
IRC Nick: BCS
Tumblr: adminwatercolor
Deviantart: itsmywatercolorheart
Github: Watercolordevdev
Skype: heartnotes
Soundcloud: Watercollider
itch: watercolorheart
Location: Florida
Contact:

#6 Post by Watercolorheart »

Sweetie, have you discovered composite images and sm.Anim functions yet?

I wish I still had my old in-progress copy of Limitation - I was using composite images and animation functions left and right to eliminate work, and create customized expressions. It was pretty fun.

Pytom, can you do me the honor of explaining these somewhat difficult concepts? Like how you can make ONE blinking animation and layer it using composites on every other sprite? I finally figured out the coding by brute force after an hour of tinkering with it, but I've lost it since ...

User avatar
PyTom
Ren'Py Creator
Posts: 16096
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

#7 Post by PyTom »

Monele wrote: I suppose that if I have 15 different sprites I want to blink in the exact same way, I'll still have to create 15 animation statements even though only the image reference changes ? :/
Yes, but you can use a function to make it easier.

Code: Select all

init:
    python:
         def flicker(im):
                return Animation(im, 0.25, Null(), 0.25, im)

    image flicker foo = flicker("foo.png")
    image flicker bar = flicker("bar.png")
And so on.

For performance reasons, you should really hide the bottom image when covered... even if it's fully covered, it still gets drawn, which takes some time.
BCS wrote: Pytom, can you do me the honor of explaining these somewhat difficult concepts? Like how you can make ONE blinking animation and layer it using composites on every other sprite? I finally figured out the coding by brute force after an hour of tinkering with it, but I've lost it since ...
You can just store the blinking animation in a python variable, and let other people use it that way. You then need to use
LiveComposite to composite the animation against other bits.

For example:

Code: Select all

init:
     $ eyes = Animation(
          "eyes_open.png", 10.0, 
          "eyes_half.png", 0.10, 
          "eyes_closed.png", 0.10, 
          "eyes_half.png", 0.10)

     image foo happy = LiveComposite(
         (400, 600),
         (0, 0), "base.png",
         (100, 100), "face_happy.png",
         (100, 100), eyes)

     image foo sad = LiveComposite(
         (400, 600),
         (0, 0), "base.png",
         (100, 100), "face_happy.png",
         (100, 100), eyes)
Last edited by PyTom on Wed May 24, 2006 10:06 am, edited 1 time in total.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

monele
Lemma-Class Veteran
Posts: 4101
Joined: Sat Oct 08, 2005 7:57 am
Location: France
Contact:

#8 Post by monele »

Mmm okie. Sounds good ^^. I'll try this out later, thanks!

monele
Lemma-Class Veteran
Posts: 4101
Joined: Sat Oct 08, 2005 7:57 am
Location: France
Contact:

#9 Post by monele »

Hmm, is there a class similar to Animation that allows displayables (I'd need "image" at least) instead of needing filenames ? The thing is I'm tinting a source picture in full green and doing this with many pictures. As usual, I like the computer to do it on the fly rather than making colored clones of all the sprites as files ^^;

Edit : might SMAnimation be the solution ?

Edit : SMAnimation doesn't seem to let me use image variables either :/... The doc says it wants displayables though, so I don't get it ^^;

User avatar
PyTom
Ren'Py Creator
Posts: 16096
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

#10 Post by PyTom »

Both Animation and SMAnimation should take displayables. They special case strings to images, but should take arbitrary displayables as well.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

monele
Lemma-Class Veteran
Posts: 4101
Joined: Sat Oct 08, 2005 7:57 am
Location: France
Contact:

#11 Post by monele »

Gyyuh, might it be that this doesn't apply to 5.3.3 ?
Otherwise, what am I doing wrong :

Code: Select all

image sp_urbania = "sp_urbania.png"

image urb_appear = Animation(sp_urbania, 0.01, None, 0.01)
Gets me this :

Code: Select all

-- Full Traceback ------------------------------------------------------------

  File "renpy\bootstrap.pyo", line 76, in bootstrap
  File "renpy\main.pyo", line 226, in main
  File "renpy\main.pyo", line 92, in run
  File "renpy\execution.pyo", line 76, in run
  File "renpy\ast.pyo", line 347, in execute
  File "renpy\python.pyo", line 768, in py_eval_bytecode
  File "game/Intro.rpy", line 24, in <expression>
NameError: name 'sp_urbania' is not defined

The last script statement executed was on line 24 of game/Intro.rpy.

Ren'Py Version: Ren'Py 5.3.3

ShiraiJunichi
Miko-Class Veteran
Posts: 651
Joined: Sat May 21, 2005 12:28 pm
Location: University of Utah
Contact:

#12 Post by ShiraiJunichi »

I think the animation function takes a string filename, and not an image... I've run into this sort of problem a lot myself...

User avatar
PyTom
Ren'Py Creator
Posts: 16096
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

#13 Post by PyTom »

Yeah, you want to use a string filename or displayable with Animation:

Code: Select all

$ sp_urbania = "sp_urbania.png"
image urb_appear = Animation(sp_urbania, 0.01, None, 0.01)
If I had Ren'Py to do over again, I probably would have reduced the number of namespaces it has. Right now, there's an image namespace, a label namespace, and a python namespace, and they can interact in odd ways.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

monele
Lemma-Class Veteran
Posts: 4101
Joined: Sat Oct 08, 2005 7:57 am
Location: France
Contact:

#14 Post by monele »

I think I really don't understand what a Displayable is then ô_o... Let's see :

Code: Select all

image name = "filename.jpg"
Not a displayable ?

Code: Select all

$ name = Image("filename.jpg")
Displayable ?

I'm kinda lost :/... But let's just say that I'd like to define picture I have once, maybe a few versions with filters (tint, alpha, whatever) and then use these in animations and such. If I'm right on that second code part, I'll be able to do it... otherwise I'll have to redefine things a lot of times it seems :/

EDIT : Quickly adding that

Code: Select all

Animation(sp_urbania, 0.01, None, 0.01)
returns an "AttributeError: 'NoneType' object has no attribute 'predict'"

EDIT2 :

Code: Select all

Animation(sp_urbania, 0.01, Null(), 0.01)
works though ^^
Last edited by monele on Thu May 25, 2006 2:54 pm, edited 1 time in total.

ShiraiJunichi
Miko-Class Veteran
Posts: 651
Joined: Sat May 21, 2005 12:28 pm
Location: University of Utah
Contact:

#15 Post by ShiraiJunichi »

I think this is what you want...

Code: Select all

$ sp_urbania_filename = "sp_urbania.png"
image urb_appear = Animation(sp_urbania_filename, 0.01, None, 0.01) 

image sp_urbania = sp_urbania_filename


Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], DewyNebula