dbawel Posted January 29, 2018 Share Posted January 29, 2018 Hello, I'm able to attach spatial audio to an object in my scene. But as I'm now creating many objects and pushing them to an array, I cannot assign the audio to the mesh in the array. Here's the basic code: for (let b = 0; b <= 20; b++) { boxRotArray = BABYLON.MeshBuilder.CreateBox("box_rot" + b, {size:0.1}, scene); } const bgm = new BABYLON.Sound('backgroundMusic', './Demos/sounds/WubbaWubbaSound.mp3', scene, null, { spatial: true, maxDistance: 20, loop: true, autoplay: true }); bgm.attachToMesh(boxRotArray[0]); I've tried using the name of the object in the array, and I've also tried to assign a new variable to equal boxRotArray[0]. when I console.log(boxRotArray[0]); it is a mesh and I'm using this in many different other assignments. Any thoughts? Thanks, DB Quote Link to comment Share on other sites More sharing options...
Pryme8 Posted January 29, 2018 Share Posted January 29, 2018 boxRotArray = needs to be boxRotArray[b] = because as is your not defining an array, so bgm.attachToMesh(boxRotArray[0]); is pointing to an invalid var. I've never done any of the sound stuff, but Id assume this is your problem if everything else is correct. Quote Link to comment Share on other sites More sharing options...
dbawel Posted January 29, 2018 Author Share Posted January 29, 2018 Hey Andy, I wrote this too fast. The actual code is: for (let b = 0; b <= 20; b++) { boxRotArray = BABYLON.MeshBuilder.CreateBox("box_rot" + b, {size:0.1}, scene); } And I'm using boxRotArray as variables in many different functions without issue. It simply won't allow me to assign audio to any of the objects in boxRotArray. Sorry for providing incomplete code - I should have copied and pasted as the var assignment is correct - but not the code in the original post - again, writing too fast on the forum. Thanks, DB Quote Link to comment Share on other sites More sharing options...
Pryme8 Posted January 29, 2018 Share Posted January 29, 2018 boxRotArray = BABYLON.MeshBuil [] der.CreateBox("box_rot" + b, {size:0.1}, scene); you are just overwriting the variable. There is no array there. you could do boxRotArray.push(BABYLON.MeshBuilder.CreateBox("box_rot" + b, {size:0.1}, scene);) or if you index already exsists : boxRotArray[b] = BABYLON.MeshBuilder.CreateBox("box_rot" + b, {size:0.1}, scene); Otherwise your variable of boxRotArray is just a single entity of what BABYLON.Meshbuilder returned and any time you reference it after that point as an array you will drop an error. with how you are doing it bgm.attachToMesh(boxRotArray); will "work" but will only attach that object to the last box that was created in the prior loop. bgm.attachToMesh(boxRotArray[0]); wont ever work with the current setup. Is the forum striping the [b] part out of your code or something? If so then I would need to look at the scene. Quote Link to comment Share on other sites More sharing options...
dbawel Posted January 29, 2018 Author Share Posted January 29, 2018 Hey, My bad again - not paying attention as I'm working while on the forum - I copied the code from the original post, and once again forgot to add the REAL code I'm using - so here is REALLY the code: for (let b = 0; b <= 20; b++) { boxRotArray = BABYLON.MeshBuilder.CreateBox("box_rot" + b, {size:0.1}, scene); } But I did learn more about the scene and perhaps a limitation for audio? If I run the following loop, I hear no audio: for (let se = 0; se <= 20; se++) { bgm.attachToMesh(boxRotArray[se]); } And the mp3 is not assigned to a single mesh. However, if I use a single mesh in the array, then it plays for the one mesh such as: bgm.attachToMesh(boxRotArray[0]); So this works; is this an absolute limitation of the audio assignment? I don't want to load 20 of the same audio files or make 20 separate assignments. I'll need to look further into solutions, but if anyone knows how to assign the same mp3 to many objects in an array, I'd be grateful as I hate adding unnecessary lines of code. And Andy - thanks for catching me twice - but I'm not that stupid to leave out the var in the loop. Cheers, DB Quote Link to comment Share on other sites More sharing options...
Pryme8 Posted January 29, 2018 Share Posted January 29, 2018 I have to assume the forum is stripping the: [b] out of the sample code. for (let b = 0; b <= 20; b++) { boxRotArray = BABYLON.MeshBuilder.CreateBox("box_rot" + b, {size:0.1}, scene); } cause it should be: for (let b = 0; b <= 20; b++) { boxRotArray[b] = BABYLON.MeshBuilder.CreateBox("box_rot" + b, {size:0.1}, scene); } but I cant assume that. So Im just trying to make sure this is not the issue before I move on. Sorry >_<. Quote Link to comment Share on other sites More sharing options...
Pryme8 Posted January 29, 2018 Share Posted January 29, 2018 send me the link on the server. If its NDA I think you should still have mine on file. Quote Link to comment Share on other sites More sharing options...
dbawel Posted January 30, 2018 Author Share Posted January 30, 2018 @Pryme8 There is no access to our private repo here at Sony. But I doubt there's anything to do as it appears you can only assign a single audio file to one mesh. I've tried to work around this, but no luck. I appreciate the help though. I'm sure you'll be proud when you see the scope of what I'm building (20 players) and the efficiency of the code. I've come a long way this last year - and you were the first to really push me. Now I'm all E6 compliant. I can show the scene after we release in late March. Perhaps @Deltakosh or someone on his team has a workaround? Is there a connect() function I can index as there is in using AudioNode.connect()? DB Pryme8 1 Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted January 30, 2018 Share Posted January 30, 2018 Pinging @davrous our audio mastermind Quote Link to comment Share on other sites More sharing options...
davrous Posted January 30, 2018 Share Posted January 30, 2018 Hi, Can you summarize what you'd like to do? Do you have a PG sample to share to illustrate that? Thanks, David Quote Link to comment Share on other sites More sharing options...
davrous Posted January 30, 2018 Share Posted January 30, 2018 I think I've understood what you're trying to code. If you'd like to use the same sound attached to several different meshes, you need first to clone the sound as many times as need such as in this sample: https://playground.babylonjs.com/#Q2A8MU It will avoid downloading several times the same sound file. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.