{"record":{"id":"feb7fce28d265057","repo":"BabylonJS/Babylon.js","slug":"sounds-length-does-not-equal-weights-length","errorCode":null,"errorMessage":"Sounds length does not equal weights length","messagePattern":"Sounds length does not equal weights length","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/dev/core/src/Audio/weightedsound.ts","lineNumber":30,"sourceCode":"    private _volume: number = 1;\r\n    /** A Sound is currently playing. */\r\n    public isPlaying: boolean = false;\r\n    /** A Sound is currently paused. */\r\n    public isPaused: boolean = false;\r\n\r\n    private _sounds: Sound[] = [];\r\n    private _weights: number[] = [];\r\n    private _currentIndex?: number;\r\n\r\n    /**\r\n     * Creates a new WeightedSound from the list of sounds given.\r\n     * @param loop When true a Sound will be selected and played when the current playing Sound completes.\r\n     * @param sounds Array of Sounds that will be selected from.\r\n     * @param weights Array of number values for selection weights; length must equal sounds, values will be normalized to 1\r\n     */\r\n    constructor(loop: boolean, sounds: Sound[], weights: number[]) {\r\n        if (sounds.length !== weights.length) {\r\n            throw new Error(\"Sounds length does not equal weights length\");\r\n        }\r\n\r\n        this.loop = loop;\r\n        this._weights = weights;\r\n        // Normalize the weights\r\n        let weightSum = 0;\r\n        for (const weight of weights) {\r\n            weightSum += weight;\r\n        }\r\n        const invWeightSum = weightSum > 0 ? 1 / weightSum : 0;\r\n        for (let i = 0; i < this._weights.length; i++) {\r\n            this._weights[i] *= invWeightSum;\r\n        }\r\n        this._sounds = sounds;\r\n        for (const sound of this._sounds) {\r\n            sound.onEndedObservable.add(() => {\r\n                this._onended();\r\n            });\r","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/BabylonJS/Babylon.js/blob/0592b347b8a4ee0236089ea86a749cacfdb266d8/packages/dev/core/src/Audio/weightedsound.ts#L12-L48","documentation":"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.","triggerScenarios":"new WeightedSound(loop, sounds, weights) where sounds.length !== weights.length, e.g. 3 sounds with 2 weights or an empty weights array.","commonSituations":"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.","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"],"exampleFix":"// before\nnew WeightedSound(false, sounds, [1, 1]); // sounds has 3 items\n// after\nconst weights = sounds.map(() => 1); // always matches\nnew WeightedSound(false, sounds, weights);","handlingStrategy":"validation","validationCode":"if (sounds.length !== weights.length) {\n    throw new Error(`sounds (${sounds.length}) and weights (${weights.length}) must match`);\n}\nconst ws = new WeightedSound(loop, sounds, weights);","typeGuard":null,"tryCatchPattern":"try {\n    const ws = new WeightedSound(loop, sounds, weights);\n} catch (e) {\n    if (e.message.includes(\"weights length\")) {\n        const ws = new WeightedSound(loop, sounds, sounds.map(() => 1));\n    }\n}","preventionTips":["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"],"tags":["audio","sound","validation","arrays"],"backgroundTag":"array-length-mismatch","analyzedSha":"0592b347b8a4ee0236089ea86a749cacfdb266d8","analyzedAt":"2026-08-30T15:11:20.442Z","schemaVersion":2},"datasetVersion":"2026-08-30T18:17:15.746Z"}