Salut! Lizard.
Issues solved!
First , our code should not be exposed to the general game loop, otherwise the sound(s) playing will be started over and over virtually infinitely.
However, we CANNOT use this code, either:
If (scene.IsFirstLoop){}
(Perhaps , it has been made broken by some newer update. The wiki needs update with this point)
I, thus, first created an event in the Scene Editor, then added the present C++ code as sub-event, like so:
[code]// To show the code event can work
scene.SetBackgroundColor(100,100,240);
// Generate random file
int random_number = rand()%5 + 1;
gd::String random_file = “sentence” + gd::String::From(random_number) + “.ogg”;
// Play it
scene.game->GetSoundManager().PlaySoundOnChannel(random_file,0,false,100,1.0);[/code]
(Yes, we now can use Code Event as sub-event in the Event Editor.)
Of course, the include list in the Code Event editor:
<iostream>
<stdlib.h>
"GDCpp/Runtime/String.h"
"GDCpp/Runtime/CommonTools.h"
"GDCpp/Runtime/RuntimeGame.h"
"GDCpp/Runtime/SoundManager.h"
I have included the project, including the sound files (ogg) for you all’s reference and test.
See the scene: PlayByCode.
P.S.
Lizard,
I don’t quite comprehend C++ Pointer anyway, and I found your relevant seemed not working (idk how):
// Get the sound manager
RuntimeGame* game = scene.game;
SoundManager sound_manager = game->GetSoundManager();
So, ignoring possible drawbacks (or bugs) I used: scene.game->GetSoundManager().PlaySoundOnChannel…
Please enlighten me as to the reason and how I can use pointer properly in the current case.
CodePlaySound.zip (90 KB)