Where to play sound and music

Monkey Forums/Monkey Beginners/Where to play sound and music

pit(Posted 2014) [#1]
Hi all.

Question from a monkey beginner.
For one reason or other, is it better to place the playsound and playmusic instructions in the "onUpdate" part of the porgram or in the "onRender" part ?

Thanks

Pit


Xyle(Posted 2014) [#2]
Hello!

Personally, I prefer to put all drawing commands in the onRender. Thats basically what that function is for. Anything related to game logic, variables, etc should go in the onUpdate part of the program.

I am definitely no expert, but I'm pretty sure thats the correct idea.

Have fun!


Gerry Quinn(Posted 2014) [#3]
I am sure they will work fine in either, but as Xyle says, anything to do with game logic logically belongs in the OnUpdate().

That said, I could imagine situations where a sound could be started during OnRender(). For example, if the game had balloon objects that floated up and popped at a certain altitude, and the balloon was just an animation that had no further effect on the game, I could imagine creating 'fire and forget' balloon animation objects that get updated during OnRender(), and which play a pop sound when they have reached the appropriate stage.


pit(Posted 2014) [#4]
Hi Gerry and Xyle,

I decided to put hthem in the onUpdate as you suggested.
As you say, it seems more logic to keep the onRender part for the drawing stricto censu.

Thanks for your advices

Pit


ImmutableOctet(SKNG)(Posted 2014) [#5]
DO NOT put any of your game's logic in 'OnRender'. It can and will be called whenever the system wants it to be. For example, a suspended window will be rendered occasionally by Windows's display server. The example Gerry Quinn gave does not at all excuse such an act from being non-standard, and bad behavior. The balloon idea doesn't even work, because the logic backing it should already be in 'OnUpdate'.

In short, load in 'OnCreate' and 'OnUpdate', and draw/render in 'OnRender'. Your game should not care if 'OnRender' has been called, the game-logic should work regardless. Playing sounds is still a part of the game's logic. As a rule of thumb, 'OnUpdate' and 'OnCreate' should handle anything that doesn't fall under a defined category. The term "update" is in some ways vague (What gets update), but the term "render" is not (Render everything you see fit).


pit(Posted 2014) [#6]
Hi immutableOctet !

And thanks for this very clear answer !

Pit


Gerry Quinn(Posted 2014) [#7]
You're probably right about the balloons, Octet - now that I think of it, I'd probably have a pass through the animation objects in OnUpdate() anyway, allowing them to update and clearing out the dead ones. (But it would still work in OnRender(), I'm pretty sure.)