Sounds in Nifty-GUI don't work

Hello monkeys,
my problem is, that I can’t play sounds in my nifty gui, (I don’t hear any sounds except ingame). There are no errors, no warnings etc. I tried to use Stereo as well as mono sounds. Nothing is played.

Here a Piece of my XML-file:

<nifty xmlns="http://nifty-gui.sourceforge.net/nifty-1.3.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://nifty-gui.sourceforge.net/nifty-1.3.xsd http://nifty-gui.sourceforge.net/nifty-1.3.xsd">

<registerSound id="myclick" filename="Interface/Sounds/tetsMono.WAV" />  

<useStyles filename="nifty-default-styles.xml" />
<useControls filename="nifty-default-controls.xml" />



<!-- **********  START SCREEN *********** -->

<screen id="start" controller="monk.NiftyScreenController"> 
    <layer id="foreground" align="center" valign="center" childLayout="vertical">
       ...
        <panel id="panel_start" height="5%" width="45%" align="center" valign="center" childLayout="center" visibleToMouse="true">
            <control name="button" width="50%" height="90%" label="Start Adventure" id="startButton" align="center" valign="center" visible="true">
                <effect>
                    <onClick name="playSound" sound="myclick"/>
                </effect>
                <interact onClick="startGame()"/>
            </control>
        </panel>    

You see, that equals with the documentation.
I would be very happy, if someone had this Problem too and could help me.

Good evening,
Domenic

EDIT:
I tried to use instead of “onClick” → “onStartHover”. That works! The Sound was played successfully. Could it be, that the onClick effect doesn’t work at Buttons, because they have already an “onClick-method” in use ?

1 Like

I had this problem at one point and i’m pretty sure it had to do with the fact that you aren’t setting the on click effect on the root component that receives the mouse click which calls “startGame”. So your game transitions to a new screen and the mouse event never propagates to where you are setting playSound.

I have mine set in a button style which can effect the root panel that you are actually clicking on. For example:

<style id="menu-button#panel">
    <attributes backgroundImage="Textures/button.png"
                paddingLeft="7px" paddingRight="7px" width="400px" height="56px" childLayout="center"
                visibleToMouse="true"/>
    <effect>
        <!-- <onHover name="border" color="#822f" post="true"/> -->
        <onHover name="imageOverlay" filename="Textures/button_over.png" post="true"/>
        <onEnabled name="renderQuad" startColor="#2228" endColor="#2220" post="true" length="150"/>
        <onDisabled name="renderQuad" startColor="#2220" endColor="#2228" post="true" length="150"/>
        <onHover name="playSound" sound="menuOver" />
        <onClick name="playSound" sound="buttonClick" />
    </effect>
</style>
2 Likes

I tried it again, but still no results. Is the code correct and as you meant?

<panel id="panel_start" height="5%" width="45%" align="center" valign="center" childLayout="center" visibleToMouse="true">
    <control name="button" width="50%" height="90%" label="Start Adventure" id="startButton" align="center" valign="center" visible="true">
        <interact onClick="startGame()"/>
    </control>
    <effect>
        <onClick name="playSound" sound="myclick"/>
    </effect>
</panel> 

Sorry, but I don’t know, how to solve this :confused:

If I insert the effect tag into the Control tag it does work. Then it looks like this:

           <control name="button" width="50%" height="90%" label="Start Adventure" id="startButton" align="center" valign="center" visible="true">
                <!-- <interact onClick="startGame()"/>  -->
                <effect>
                    <onClick name="playSound" sound="myclick"/>
                </effect>
            </control>

But both together don’t work, why ???

No, that’s not what I meant. All controls in Nifty are a bunch of nested components. So I button has a couple different panels and a label for example. So for your use case where you are transitioning to a different screen or starting your game you need to apply it directly to the root panel that gets the click event. That root panel lives INSIDE the button control itself.

Have I not done that before ?

<control name="button" width="50%" height="90%" label="Start Adventure" id="startButton" align="center" valign="center" visible="true">
    <effect>
        <onClick name="playSound" sound="myclick"/>
    </effect>
    <interact onClick="startGame()"/>
</control>

The <effect> tag containing the <onClick ... /> tag is inside the Control-tag.
As I said, without the <interact onClick="startGame()"/> it does work. But both together don’t. Could you please tell me what the rootPanel of the button exactly is and where I have to insert my

<effect>
    <onClick name="playSound" sound="myclick"/>
</effect>

correctly?

Your startGame() propably changes the screen? The sound plays with luck in that case. And it might play if you return to the start screen.

Even with neverStopRendering=true.

Okay, you’re right. If I click the button and the “start”-Screen isn’t changing the sound will be played. But normally I change to a “loading”-screen and it that case it doesn’t work! Is there no way to simply make them work?

That’s what I said earlier. You need to create a style and apply it to the button in order to target the root element and get the sound to play when you change screens. I have the style I use in the post above.

Oh yes :blush: I understood your comment also just now, I’ve also had this problem for quite awhile but haven’t tried to solve it yet.

So, do I have to wrote a controlDefinition as well ? I am new at Nifty GUI development, sorry.

Okay, it is still not working :frowning:
I have to different XML-files.
The relevant part from the first one:

<registerSound id="myclick" filename="Interface/Sounds/tetsMono.WAV" /> 
<useStyles filename="Interface/Screens/Styles/myButtonStyle.xml" />
...
<panel id="panel_start" height="5%" width="45%" align="center" valign="center" childLayout="vertical" visibleToMouse="true" neverStopRendering="true">
   <control name="button" style="myButtonStyle" font="Interface/Fonts/Constantia.fnt" width="50%" height="90%" label="Start Adventure" id="startButton" align="center" valign="center" neverStopRendering="true" visible="true">
      <interact onClick="startGame()"/> 
   </control>
</panel>

The “Style”-XML-file:

<?xml version="1.0" encoding="UTF-8"?>
<nifty-styles>
<style id="myButtonStyle#panel">
    <attributes backgroundImage="Interface/Fonts/Constantia.png"
                paddingLeft="7px" paddingRight="7px" width="400px" height="56px" childLayout="center"
                visibleToMouse="true" font="Interface/Fonts/Constantia.fnt" />
    <effect>
        <onClick name="playSound" sound="myClick" />
    </effect>
</style>
</nifty-styles>

I get the following exception, but the game doesn’t crashs. However, it doesn’t work at all. I’m very frustrating :frowning:

Warnung: missing font in renderText! could it be that you're using <text> elements without a font or style attribute? in case you've replaced <label> with <text> you're probably missing style='nifty-label' :)

EDIT (My alternative solution):
I found another solution: In my case the sound should play when I click on the button. After the click the Screen is changed immediately. So the Sound cannot be played, because the Screen is already “gone”.
But if I insert this line for example:

<onEndScreen name="fade" ... length="1" />

You see it will work!
I think it is not the cleanest solution, but better than nothing.

You are using “myClick” in your style definition and “myclick” for the sound definition. Not sure if that is it but i’m pretty sure the case matters.

I fixed that a few minutes later after I have uploaded this post. But it still not works. However. Maybe I will use this in a couple of months, but until then I will use the solution I wrote above.

Thank you, monkeys, for your help :grinning: