0

For introduction: https://mytechservinginternetmeals.blogspot.com/2021/10/how-to-start-making-browser-video-game.html

Could anyone tell how to fix the error in code that try's to check errors in it?

THIS CAUSES TypeError: Cannot read properties of undefined (reading 'resourcePath')

const novietojumi = [
  "0.1221 0.1221",
  "0.1111 0.1111",
  "0.1111 0.1111",
  "0.1111 0.1111",
  "0.1111 0.1111",
  "0.1111 0.1111",
  "0.1111 0.1111",
  "0.1111 0.1111",
];
let arrayLength = novietojumi.length;

console.log(arrayLength);

for (let i = 0, x = 0, y = 0; i < arrayLength; i++, y++, x++) {
  //console.log(novietojumi[i]);

  const NOV = novietojumi[i].split(" ");
  console.log(NOV);
  console.log("KOORD1", NOV[0]);
  console.log("KOORD2", NOV[1]);
   _P.set(NOV[x], 0.0, NOV[x]);
  _P.add(center);
  _P.multiplyScalar(50.0);

  const key = '__scenery__[' + _P.x + '][' + _P.z + ']';
  if (this.FindEntity(key)) {
    //continue;
  }

  _V.copy(_P);

  _P.x += (this.noise_.Get(_P.x, 0.0, _P.z) * 2.0 - 1.0) * 25.0;
  _P.z += (this.noise_.Get(_P.x, 1.0, _P.z) * 2.0 - 1.0) * 25.0;
  _P.y = terrain.GetHeight(_P)[0];

  const biome = this.FindBiome_(terrain, _P);

  const roll = this.noise_.Get(_V.x, 2.0, _V.z);
  if (roll > _BIOMES[biome]) {
    //continue;
  }

  const e = this.SpawnAt_(biome, _P);

  //console.log(e);
  e.SetPosition(_P);

  //console.log(e, key);
  this.Manager.Add(e, key);

  e.SetActive(false);
  this.crap_.push(e);
}

THIS DOES NOT:

for (let x = -10; x <= 10; ++x) {
  for (let y = -10; y <= 10; ++y) {
    _P.set(x, 0.0, y);
    _P.add(center);
    _P.multiplyScalar(50.0);

    const key = '__scenery__[' + _P.x + '][' + _P.z + ']';
    if (this.FindEntity(key)) {
      continue;
    }

    _V.copy(_P);

    _P.x += (this.noise_.Get(_P.x, 0.0, _P.z) * 2.0 - 1.0) * 25.0;
    _P.z += (this.noise_.Get(_P.x, 1.0, _P.z) * 2.0 - 1.0) * 25.0;
    _P.y = terrain.GetHeight(_P)[0];

    const biome = this.FindBiome_(terrain, _P);

    const roll = this.noise_.Get(_V.x, 2.0, _V.z);
    if (roll > _BIOMES[biome]) {
      //continue;
    }

    const e = this.SpawnAt_(biome, _P);

    e.SetPosition(_P);

    this.Manager.Add(e, key);

    e.SetActive(false);
    this.crap_.push(e);
  }
}

THIS CODE sample is where error gets found :

const _SCENERY = {
  birch1: {
    base: 'Birch_1.fbx',
    resourcePath: './resources/trees/FBX/',
    names: {
      Bark: 'Birch_Bark.png',
      Leaves: 'Birch_Leaves_Yellow.png'
    },
    scale: 0.075,
    biomes: ['forest'],
    collision: true,
  },
  tree1: {
    base: 'Tree_1.fbx',
    resourcePath: './resources/trees/FBX/',
    names: {
      Bark: 'Tree_Bark.jpg',
      Leaves: 'Leaves_Blue.png'
    },
    scale: 0.1,
    biomes: ['forest'],
    collision: true,
  },
  rock1: {
    base: 'Rock_1.fbx',
    resourcePath: './resources/nature/FBX/',
    names: {},
    scale: 0.025,
    biomes: ['arid', 'desert'],
  },
  rockMoss1: {
    base: 'Rock_Moss_1.fbx',
    resourcePath: './resources/nature/FBX/',
    names: {},
    scale: 0.025,
    biomes: ['forest'],
  },
  plant1: {
    base: 'Plant_1.fbx',
    resourcePath: './resources/nature/FBX/',
    names: {},
    scale: 0.05,
    biomes: ['forest', 'arid'],
  },
  grass1: {
    base: 'Grass_1.fbx',
    resourcePath: './resources/nature/FBX/',
    names: {},
    scale: 0.05,
    biomes: ['forest', 'arid'],
  },
  flowers1: {
    base: 'Flowers.fbx',
    resourcePath: './resources/nature/FBX/',
    names: {},
    scale: 0.05,
    biomes: ['forest'],
  },
};

const _BIOMES = {
  desert: 0.1,
  forest: 0.8,
  arid: 0.6,
};

const multiples = {
  birch1: {
    name: 'Birch_',
    key: 'birch',
    num: 10
  },
  tree1: {
    name: 'Tree_',
    key: 'tree',
    num: 10
  },
  rock1: {
    name: 'Rock_',
    key: 'rock',
    num: 7
  },
  rockMoss1: {
    name: 'Rock_Moss_',
    key: 'rockMoss',
    num: 7
  },
  plant1: {
    name: 'Plant_',
    key: 'plant',
    num: 5
  },
  grass1: {
    name: 'Grass_',
    key: 'grass',
    num: 2
  },
};


SpawnAt_(biome, spawnPos) {
  const matchingScenery = [];
  for (let k in _SCENERY) {
    if (_SCENERY[k].biomes.indexOf(biome) >= 0) {
      matchingScenery.push(k);
    }
  }

  const roll = this.noise_.Get(spawnPos.x, 3.0, spawnPos.z);
  const randomProp = _SCENERY[
    matchingScenery[Math.round(roll * (matchingScenery.length - 1))]];

  const e = new entity.Entity();
  e.AddComponent(new render_component.RenderComponent({
    scene: this.params_.scene,
    resourcePath: randomProp.resourcePath,
    resourceName: randomProp.base,
    textures: {
      resourcePath: './resources/trees/Textures/',
      names: randomProp.names,
      wrap: true,
    },
    emissive: new THREE.Color(0x000000),
    specular: new THREE.Color(0x000000),
    scale: randomProp.scale * (0.8 + this.noise_.Get(spawnPos.x, 4.0, spawnPos.z) * 0.4),
    castShadow: true,
    receiveShadow: true,
    onMaterial: (m) => {
      if (m.name.search('Leaves') >= 0) {
        m.alphaTest = 0.5;
      }
    }
  }));

  if (randomProp.collision) {
    console.log("e");
    e.AddComponent(
      new spatial_grid_controller.SpatialGridController({
        grid: this.params_.grid
      }));
  }

  const q = new THREE.Quaternion().setFromAxisAngle(
    new THREE.Vector3(0, 1, 0), this.noise_.Get(spawnPos.x, 5.0, spawnPos.z) * 360);
  e.SetQuaternion(q);

  return e;
}

I am trying now -

  const center = new THREE.Vector3().copy(player.Position);

  center.x = Math.round(center.x / 50.0);
  center.y = 0.0;
  center.z = Math.round(center.z / 50.0);

  if (this.center_ && this.center_.equals(center)) {
    return;
  }

  this.center_ = center;

  const _P = new THREE.Vector3();
  const _V = new THREE.Vector3();
  const terrain = 
  this.FindEntity('terrain').GetComponent('TerrainChunkManager');


 const novietojumi = [
  "0.1221 0.1221",
  "0.1111 0.1111",
  ];
let arrayLength = novietojumi.length;

console.log(arrayLength);

for(let i = 0, x = 0, y = 0; i < arrayLength; i++, y++, x++) {


console.log(i);
    //console.log(novietojumi[i]);
    
const NOV = novietojumi[i].split(" ");
console.log(NOV, "NOV");
//console.log("KOORD1", NOV[0]);
//console.log("KOORD2", NOV[1]);


      _P.set(parseFloat(NOV[0], 0.0, parseFloat(NOV[1])));
      
      console.log(_P, "pozicijaaaa");
      _P.add(center);
      _P.multiplyScalar(50.0);

      console.log(_P, "22pozicijaaaa");

      const key = '__scenery__[' + _P.x + '][' + _P.z + ']';
      if (this.FindEntity(key)) {
        //continue;
      }

      _V.copy(_P);
      
      _P.x += (this.noise_.Get(_P.x, 0.0, _P.z) * 2.0 - 1.0) * 25.0;
      _P.z += (this.noise_.Get(_P.x, 1.0, _P.z) * 2.0 - 1.0) * 25.0;
      _P.y = terrain.GetHeight(_P)[0];

      const biome = this.FindBiome_(terrain, _P);
      
      console.log(biome, "BIOMA");
      
      
        console.log(_V.x, "VX");
      const roll = this.noise_.Get(_V.x, 2.0, _V.z);
      console.log(roll);
      if (roll > _BIOMES[biome]) {
        //continue;
      }

      const e = this.SpawnAt_(biome, _P);

      //console.log(e);
      e.SetPosition(_P);
      
      //console.log(e, key);



      this.Manager.Add(e, key);

      e.SetActive(false);
      this.crap_.push(e);
  }

for some reason for loop repeats more than array length of novietojumi array constant is telling it to..... that means, when player is walking around,this loop keeps spawning random elements...

13
  • The first code block never uses resourcePath. Which line is getting the error? Commented Dec 16, 2021 at 17:55
  • resourcePath: randomProp.resourcePath, Commented Dec 16, 2021 at 17:56
  • That's in the last code block, not the one you said has the error. Commented Dec 16, 2021 at 17:57
  • The problem is that randomProp is being set to undefined, presumably by _SCENERY[matchingScenery[Math.round(roll * (matchingScenery.length - 1))]] Commented Dec 16, 2021 at 17:59
  • randomProp is defined if I use the second code in article (with two for loops that does iteration for x and y) I guess.... Commented Dec 16, 2021 at 18:06

1 Answer 1

1

NOV[0] and NOV[1] are strings, not numbers. Try:

_P.set(parseFloat(NOV[0], 0.0, parseFloat(NOV[1]));
Sign up to request clarification or add additional context in comments.

7 Comments

Oh yeah, but I will need growing value, 0 1 then 2 3 and then 4 5 etc... thus need an X incrementation.
You're incrementing i and then using const NOV = novietojumi[i].split(" ");
_P.set(parseFloat(NOV[i], 0.0, parseFloat(NOV[i]))); this causes same error...
Then I guess it's not because the values are strings.
You said that was the problem in a comment.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.