Code missing something

I think that this error could happen usually if you don’t have the images in the right place for the script to read them

interesting ok how do i tell

you could try with fewer images, like 3 images maybe, I think that there is a constant in code that specifies the number of images

interesting would there be way to change that constant. because my specific project needs to generate a lot of people so having more combinations would be nice

i changes it so there is only a few pngs in each and still same error and even just put 1 png in and nothing

you could add more lines with console.log to identify where it stops

usually there was a problem with not finding a specific image or file on disk from where it tries to read it

ok i did now it getting caught on this line again thank you for your help still learning.

C:\Users\brans\Documents\2d game\Pixel people\index.js:145
Math.random() * layer.elementIdsForRarity[_rarity].length

i get that its can read it because its undefined but i don’t see how its not

here is where it is in the code

[quote="nosnarb1, post:6, topic:5895"]
// check if the given dna is contained within the given dnaList
// return true if it is, indicating that this dna is already in use and should be recalculated
const isDnaUnique = (_DnaList = [], _dna = []) => {
let foundDna = _DnaList.find((i) => i.join("") === _dna.join(""));
return foundDna == undefined ? true : false;
};

const getRandomRarity = (_rarityOptions) => {
let randomPercent = Math.random() * 100;
let percentCount = 0;

for (let i = 0; i <= _rarityOptions.length; i++) {
percentCount += _rarityOptions[i].percent;
if (percentCount >= randomPercent) {
console.log(`use random rarity ${_rarityOptions[i].id}`);
return _rarityOptions[i].id;
}
}
return _rarityOptions[0].id;
};

// create a dna based on the available layers for the given rarity
// use a random part for each layer
const createDna = (_layers, _rarity) => {
let randNum = [];
let _rarityWeight = rarityWeights.find((rw) => rw.value === _rarity);
_layers.forEach((layer) => {
let num = Math.floor(
Math.random() * layer.elementIdsForRarity[_rarity].length
);
if (_rarityWeight && _rarityWeight.layerPercent[layer.id]) {
// if there is a layerPercent defined, we want to identify which dna to actually use here (instead of only picking from the same rarity)
let _rarityForLayer = getRandomRarity(
_rarityWeight.layerPercent[layer.id]
);
num = Math.floor(
Math.random() * layer.elementIdsForRarity[_rarityForLayer].length
);
randNum.push(layer.elementIdsForRarity[_rarityForLayer][num]);
} else {
randNum.push(layer.elementIdsForRarity[_rarity][num]);
}
});
return randNum;
};
[/quote]

you can see here how to paste formatted code on forum: READ BEFORE POSTING - How to post code in the forum

1 Like

did that help? i tried to edit it

first code looks better now

what does the error say? what variables is undefined?

##################

Generative Art

- Generating your NFT collection

##################


Mutating 0 of 100

  • rarity: undefined
    C:\Users\brans\Documents\2d game\Pixel people\index.js:145
    Math.random() * layer.elementIdsForRarity[_rarity].length
    ^

TypeError: Cannot read properties of undefined (reading ‘length’)
at C:\Users\brans\Documents\2d game\Pixel people\index.js:145:58
at Array.forEach ()
at createDna (C:\Users\brans\Documents\2d game\Pixel people\index.js:143:11)
at saveFile (C:\Users\brans\Documents\2d game\Pixel people\index.js:328:20)
at handleFinal (C:\Users\brans\Documents\2d game\Pixel people\index.js:390:31)
at startCreating (C:\Users\brans\Documents\2d game\Pixel people\index.js:393:11)
at Object. (C:\Users\brans\Documents\2d game\Pixel people\index.js:453:1)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions…js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:17:47

this would mean that this is undefined

right so then how do i define it i guess i thought i did here

const getRandomRarity = (_rarityOptions) => {
  let randomPercent = Math.random() * 100;
  let percentCount = 0

that seems to be an array, dat doesn’t have data for a particular _rarity

i see hmm im not sure how to fix it ill have to play around i guess

I was stuck in the same error and my solution was to add “?” to avoid undefined error

percentCount += _rarityOptions[i]?.percent;