Weird error trying to make a subroutine

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
purplepanties
Newbie
Posts: 3
Joined: Tue Dec 06, 2022 4:39 am
itch: purplepanties
Contact:

Weird error trying to make a subroutine

#1 Post by purplepanties »

I've got a bit of code like this that occurs in quite a few places:

Code: Select all

    play sound "audio/charm.ogg" noloop volume 0.5
    scene s1_enquire
    with flash_purple
...which works fine.

So, I want to move it into a subroutine, so I don't have to keep making multiple updates if I want to mess with the sound.

I make the call like this:

Code: Select all

            call charm("s1_enquire")
#            play sound "audio/charm.ogg" noloop
#            scene s1_enquire
#            with flash_purple
And I code the subroutine like this (in a different file):

Code: Select all

# Display subroutine

label charm(im="black"):

    play sound "audio/charm.ogg" noloop volume 0.5

    scene expression ImageReference(im)
    with flash_purple

    return
...but when I try and load the game I'm getting an end of line expected error:

Code: Select all

File "game/$emotes.rpy", line 61: end of line expected.
    play sound "audio/charm.ogg" ->noloop volume 0.5
If I comment the play in the subroutine out, it runs fine...

If i just delete the noloop and the volume 0.5, it crashes at runtime with a very different error:

Code: Select all

While running game code:
  File "game/scene1.rpy", line 181, in script call
    call charm("s1_enquire")
  File "game/$emotes.rpy", line 64, in script
    play sound "audio/charm.ogg"
Exception: Sayer 'play' is not defined.

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

Full traceback:
  File "game/scene1.rpy", line 181, in script call
    call charm("s1_enquire")
  File "game/$emotes.rpy", line 64, in script
    play sound "audio/charm.ogg"
  File "D:\renpy-7.4.9-sdk\renpy\ast.py", line 881, in execute
    who = eval_who(self.who, self.who_fast)
  File "D:\renpy-7.4.9-sdk\renpy\ast.py", line 774, in eval_who
    raise Exception("Sayer '%s' is not defined." % who)
Exception: Sayer 'play' is not defined.
It looks like it's not recognizing the play instruction as being a play instruction in the subroutine?

If I change it to:

Code: Select all

label charm(im="black"):

#    play sound "audio/charm.ogg" loopno volume 0.5
    $ renpy.sound.play("audio/charm.ogg", "sound", 0, 0, False, False, 0.5)

    scene expression ImageReference(im)
    with flash_purple

    return
It works fine. Only catch is I had to read the source on github to work out the syntax for the renpy.sound.play call :-(

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2398
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: Weird error trying to make a subroutine

#2 Post by Ocelot »

First of all: you didn't have to read source, syntax for music/sound . play is documented: https://www.renpy.org/doc/html/audio.ht ... music.play
Second: use keyword arguments, it lets you not repeat default value over and over.

After that is out of the way to your problem. First of all, im is in the reserved names list, you should not name your variables that: https://www.renpy.org/doc/html/reserved.html
Also you mention loading the game. Changing script in substantional ways might lead to problems. Does this problem persist if you start a fresh game?
< < insert Rick Cook quote here > >

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3791
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: Weird error trying to make a subroutine

#3 Post by Imperf3kt »

Might I also introduce you to the audio namespace
https://www.renpy.org/doc/html/audio.ht ... -namespace
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Ocelot