music volume and persistents

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
Scribbles
Miko-Class Veteran
Posts: 580
Joined: Wed Sep 21, 2016 4:15 pm
Completed: Pinewood Island
Projects: As We Know It
Organization: Scribble Games
Tumblr: doodlesatbreakfast
Deviantart: breakfastdoodles
itch: scribbles
Location: Georgia
Contact:

music volume and persistents

#1 Post by Scribbles » Wed Jan 24, 2018 10:22 am

So I want to have the volume at about 50% when a user opens the game, but then if they change the volume level themselves, not mess with it after. I've used this code to do that, and it's worked in previous games:

Code: Select all

init python:
    if not persistent.set_volumes:
        persistent.set_volumes = True
        _preferences.volumes['music'] *= .50
and this -will- work until I delete persistent data, then I get this error:

I don't understand why it won't work if I delete persistent data and then run the game? It will work if I remove the code, open the game, don't set the volume, then put it in the code. If there is a better way to do this, or a fix I would love to know!

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/Code/variables.rpy", line 698, in script
    init python:
  File "game/Code/variables.rpy", line 704, in <module>
    _preferences.volumes['music'] *= .50
KeyError: u'music'

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

Full traceback:
  File "game/Code/variables.rpy", line 698, in script
    init python:
  File "C:\Users\barnh\Desktop\renpy-6.99.13-sdk\renpy\ast.py", line 848, in execute
    renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
  File "C:\Users\barnh\Desktop\renpy-6.99.13-sdk\renpy\python.py", line 1804, in py_exec_bytecode
    exec bytecode in globals, locals
  File "game/Code/variables.rpy", line 704, in <module>
    _preferences.volumes['music'] *= .50
KeyError: u'music'

Windows-8-6.2.9200
Ren'Py 6.99.14.3135
 
Image - Image

User avatar
DragoonHP
Miko-Class Veteran
Posts: 710
Joined: Tue Jun 22, 2010 12:54 am
Completed: Christmas
IRC Nick: DragoonHP
Location: Zion Island, Solario
Contact:

Re: music volume and persistents

#2 Post by DragoonHP » Wed Jan 24, 2018 11:19 am

So when you click on "Delete Persistent", all of persistent data is deleted. And with _preferences.volumes['music'] *= .50 you are telling Ren'Py to half the value of something that doesn't even exist anymore because we deleted it when we clicked on "Delete Persistent"

Try this:

Code: Select all

init python:
    if not persistent.set_volumes:
        persistent.set_volumes = True
        try:
            _preferences.volumes['music'] *= .50
        except KeyError:
            _preferences.volumes['music'] = .50

User avatar
Scribbles
Miko-Class Veteran
Posts: 580
Joined: Wed Sep 21, 2016 4:15 pm
Completed: Pinewood Island
Projects: As We Know It
Organization: Scribble Games
Tumblr: doodlesatbreakfast
Deviantart: breakfastdoodles
itch: scribbles
Location: Georgia
Contact:

Re: music volume and persistents

#3 Post by Scribbles » Wed Jan 24, 2018 1:05 pm

that keeps the crash from happening! it doesn't adjust the volume though :-/ it's at 100% when I open it fresh. Is there just no way to do it code-wise? since it's trying to adjust something that doesn't exist until after init?
Image - Image

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

Re: music volume and persistents

#4 Post by Imperf3kt » Wed Jan 24, 2018 4:12 pm

What you want is a configuration variable
https://www.renpy.org/doc/html/preferen ... sic_volume
define config.default_music_volume = 1.0
The default volume of the music mixer, which is used for the music and movie audio channels. This should be a number between 0.0 and 1.0, with 1.0 being full volume.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

User avatar
Scribbles
Miko-Class Veteran
Posts: 580
Joined: Wed Sep 21, 2016 4:15 pm
Completed: Pinewood Island
Projects: As We Know It
Organization: Scribble Games
Tumblr: doodlesatbreakfast
Deviantart: breakfastdoodles
itch: scribbles
Location: Georgia
Contact:

Re: music volume and persistents

#5 Post by Scribbles » Wed Jan 24, 2018 10:30 pm

Imperf3kt wrote:
Wed Jan 24, 2018 4:12 pm
What you want is a configuration variable
https://www.renpy.org/doc/html/preferen ... sic_volume
define config.default_music_volume = 1.0
The default volume of the music mixer, which is used for the music and movie audio channels. This should be a number between 0.0 and 1.0, with 1.0 being full volume.
thanks I'll give that a try!
Image - Image

Post Reply

Who is online

Users browsing this forum: No registered users