Jump to content

Can't assign audio to object in array


dbawel
 Share

Recommended Posts

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

Link to comment
Share on other sites

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.
 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

 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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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 >_<.
 

Link to comment
Share on other sites

@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 

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...