-1

I am making an ai that will learn by its self to get to a specific point in the best possible way.

I am doing this using atom and a life server to google.

The error says that life is undefined but when I look at my code then is life first set to 0 and then every frame it increments by one.

Also: I'm using a library called p5.js

Can someone please help me. I'm getting a little bit depressed about this project. This is the code:

function setup() {
  createCanvas(400, 300);
  population = new Population();
  // countP = createP();
  dna = new Dna();
}

function draw() {
  background(0);
  population.run();
  // lifeP.html(count);
  // count++;
}

//ROCKET OBJECT
function Rocket() {
  let pos = createVector(width / 2, height);
  let vel = createVector();
  let acc = createVector();

  this.applyForce = function(force) {
    acc.add(force);
  }

  this.update = function() {
    let index = 0;

    this.applyForce(dna.genes[index]);
    index++;

    vel.add(acc);
    pos.add(vel);
    acc.mult(0);
  }

  this.show = function() {
    push();
    translate(pos.x, pos.y);
    rectMode(CENTER);
    rotate(vel.heading());
    fill(255, 150);
    noStroke();
    rect(0, 0, 50, 10);
    pop();
  }
}
//POPULATION OBJECT
function Population() {
  let rockets = [];
  let popsize = 25

  for (let i = 0; i < popsize; i++) {
    rockets[i] = new Rocket();
  }

  this.run = function() {
    for (let i = 0; i < popsize; i++) {
      rockets[i].update();
      rockets[i].show();
    }
  }
}
//DNA OBJECT
function Dna() {
  let genes = [];

  for (let i = 0; i < lifespan; i++) {
    genes[i] = p5.Vector.random2D();
  }
}

This is the error: Uncaught TypeError:

Cannot read property '0' of undefined
    at Rocket.update (sketch.js:34)
    at Population.run (sketch.js:64)
    at draw (sketch.js:16)
    at p5.redraw (p5.js:51956)
    at p5.<anonymous> (p5.js:46250)
    at p5.<anonymous> (p5.js:46152)
    at new p5 (p5.js:46434)
    at _globalInit (p5.js:48404)
Rocket.update @ sketch.js:34
Population.run @ sketch.js:64
draw @ sketch.js:16
p5.redraw @ p5.js:51956
(anonymous) @ p5.js:46250
(anonymous) @ p5.js:46152
p5 @ p5.js:46434
_globalInit @ p5.js:48404
load (async)
26.../core/core @ p5.js:48413
s @ p5.js:2
(anonymous) @ p5.js:2
13../color/creating_reading @ p5.js:42088
s @ p5.js:2
e @ p5.js:2
(anonymous) @ p5.js:2
(anonymous) @ p5.js:2
(anonymous) @ p5.js:2
Fabian N.
  • 3,807
  • 2
  • 23
  • 46
  • 1
    Please fix the formatting of your question and copy-paste your code and error into the question as text instead of a screenshot. – Fabian N. Aug 04 '18 at 10:29
  • I hope this is better – ExellentCoin2044 Aug 04 '18 at 10:39
  • 1
    No its not. Its still a screenshot. – Adrian Aug 04 '18 at 10:41
  • 1
    You could add some line breaks ... but the important point is: copy paste the code into the question, select it and then click the {} (see here https://i.stack.imgur.com/Kf9No.png) symbol to format it as code. – Fabian N. Aug 04 '18 at 10:42
  • 3
    [**Do not post images of code or errors!**](https://meta.stackoverflow.com/q/303812/995714) Images and screenshots can be a nice addition to a post, but please make sure the post is still clear and useful without them. If you post images of code or error messages make sure you also copy and paste or type the actual code/message into the post directly. – Rob Aug 04 '18 at 10:44
  • ok this should be it – ExellentCoin2044 Aug 04 '18 at 10:46
  • 1
    After endless editing, I'm pretty confident to say that I managed to track down your actual problem, see below. – Fabian N. Aug 04 '18 at 11:10

1 Answers1

1
Cannot read property '0' of undefined
    at Rocket.update (sketch.js:34)

The line in question is:

this.applyForce(dna.genes[index]);

and there aren't many things there that use properties, it's probably [index] which means that dna.genes is for some reason undefined.

Now let's take a look at where dna.genes was defined.

//DNA OBJECT
function Dna() {
  genes = [];

  for (let i = 0; i < lifespan; i++) {
    genes[i] = p5.Vector.random2D();
  }
}

If you compare this to your other object you can see that you forgot the this. in front of genes:

//DNA OBJECT
function Dna() {
  this.genes = [];

  for (let i = 0; i < lifespan; i++) {
    this.genes[i] = p5.Vector.random2D();
  }
}

Or you could add a getter to expose the genes:

//DNA OBJECT
function Dna() {
  let genes = [];

  for (let i = 0; i < lifespan; i++) {
    genes[i] = p5.Vector.random2D();
  }

  this.getGenes = function(index) {
    return genes[index];
  }
}

And then use dna.getGenes(index) in Rocket.update.

Fabian N.
  • 3,807
  • 2
  • 23
  • 46
  • I don't know what it dit but it seems to fix it when I put "this." in front of genes at the Dna object. Can someone explain me? – ExellentCoin2044 Aug 04 '18 at 11:19
  • It's doing the same as when you defined the functions e.g `this.update` it defines [thingy] as a property of the object instead of local variable (e.g `let index = 0`). I would prefer the second method, using a getter. You should take a look at this https://stackoverflow.com/questions/13418669/javascript-do-i-need-to-put-this-var-for-every-variable-in-an-object to understand the general concept. – Fabian N. Aug 04 '18 at 11:31
  • Also if this post solved your problem please consider accepting it as the answer https://meta.stackexchange.com/a/5235/332983 – Fabian N. Aug 04 '18 at 11:40