BabylonJS/Babylon.js · error · Error
Sounds length does not equal weights length
Error message
Sounds length does not equal weights length
What it means
WeightedSound picks randomly among a set of Sounds using per-sound weights. Its constructor requires the sounds and weights arrays to be the same length so each Sound gets exactly one weight; otherwise it throws this error immediately.
Source
Thrown at packages/dev/core/src/Audio/weightedsound.ts:30
private _volume: number = 1;
/** A Sound is currently playing. */
public isPlaying: boolean = false;
/** A Sound is currently paused. */
public isPaused: boolean = false;
private _sounds: Sound[] = [];
private _weights: number[] = [];
private _currentIndex?: number;
/**
* Creates a new WeightedSound from the list of sounds given.
* @param loop When true a Sound will be selected and played when the current playing Sound completes.
* @param sounds Array of Sounds that will be selected from.
* @param weights Array of number values for selection weights; length must equal sounds, values will be normalized to 1
*/
constructor(loop: boolean, sounds: Sound[], weights: number[]) {
if (sounds.length !== weights.length) {
throw new Error("Sounds length does not equal weights length");
}
this.loop = loop;
this._weights = weights;
// Normalize the weights
let weightSum = 0;
for (const weight of weights) {
weightSum += weight;
}
const invWeightSum = weightSum > 0 ? 1 / weightSum : 0;
for (let i = 0; i < this._weights.length; i++) {
this._weights[i] *= invWeightSum;
}
this._sounds = sounds;
for (const sound of this._sounds) {
sound.onEndedObservable.add(() => {
this._onended();
});
View on GitHub (pinned to 0592b347b8)
Solutions
- Ensure weights.length === sounds.length before constructing
- Generate weights programmatically: sounds.map(() => 1)
- Add a missing weight for every newly added Sound
- Validate config-derived arrays at load time
Example fix
// before new WeightedSound(false, sounds, [1, 1]); // sounds has 3 items // after const weights = sounds.map(() => 1); // always matches new WeightedSound(false, sounds, weights);
Defensive patterns
Strategy: validation
Validate before calling
if (sounds.length !== weights.length) {
throw new Error(`sounds (${sounds.length}) and weights (${weights.length}) must match`);
}
const ws = new WeightedSound(loop, sounds, weights); Try / catch
try {
const ws = new WeightedSound(loop, sounds, weights);
} catch (e) {
if (e.message.includes("weights length")) {
const ws = new WeightedSound(loop, sounds, sounds.map(() => 1));
}
} Prevention
- Derive weights from the sounds array itself (map to 1) so lengths never diverge
- Validate sound/weight config arrays at load time, before constructing
- When appending a Sound at runtime, append its weight in the same code path
When it happens
Trigger: new WeightedSound(loop, sounds, weights) where sounds.length !== weights.length, e.g. 3 sounds with 2 weights or an empty weights array.
Common situations: Building weights dynamically (e.g. from config) that drifts out of sync with the sounds array; forgetting to add a weight when appending a sound; loading sound lists from JSON where some entries are skipped.
Related errors
- The value passed to [Scene.audioListenerPositionProvider] mu
- The value passed to [Scene.audioListenerRotationProvider] mu
- ${extensionContext}: Direction or Distance properties are no
- Sample2DRgbaToRef: widthPx and heightPx must be positive.
- At least one mesh is needed to create the nav mesh.
AI-assisted analysis of BabylonJS/Babylon.js@0592b347b8 (2026-08-30).
Data as JSON: /api/errors/feb7fce28d265057.
Report an issue: GitHub.