Cannot add mesh to my array

So I was going to make a game where you’re a rocket flying through space (inspired by Chapter 5). Here’s some of my code:

https://www.code3dgames.com/3de/#B/pVdbT9tIFH7nVxzlhUkJTtgu7YpLJTYEWi0FlKTblVYrNNgTPMX2eGfGCQbx3/fMxY6dS6UVKCTOnO/cL3Nyci+i8tNJ337snKhQ8lyDkuFpp69jyVjwQ3WQ7giriDwuFf8pIhSZliJR/YukHPrnzQyfdgD6fbg1IkMFimnNsweFp/bohwocTgU0TQWcwm7fPKCw3eMNoIWQj0xaWO5Id+7IMThl05hBR4UsYx3gChYxkwyULmYz4BmIQsIDTRkseJJATPOcZUfIOKcSLBNKz9iiVj0xZ6RrrLHkAH24lHTOdUkMbvp5PBoFf7JQC/mewKBnXr8OoGtZjNRZQjUKfTGfk5hGGIAj0LJgrxUi4Q+x9nqdvLP0nrNMXxkC2V3EXLNdlBz81jCERhGxnN2W5yE6J6l3HTUrxhTo2IegcpWqHE0eU81N1Bc8i8Qi4FnG5Hce6Rj6rbPPzOipzF1YxAauChAz79B2Gc7Kls+3TFqj+JwNLZV8POw1De3BAb4G+Gej0O9vEXQjdSweJM1jHnpJ+9bm/i89qB+ckeZpf/nYUuBEB7lQHLVnwTNqORwM2hlwoHYKJMsiLDrZgUjShc+DSYE31qYE+0a4vITYkXUNVrwtj76z+8ursaeQF5ppThNOla8ja23FaAp0wp8ZWUtPb0M+LGskwiLFcgvMwAhsS0TDmCcRqYVGIh0lzIBqV9/5P5hMz8ZTGN6cf7m+hJtrtHkE16O/pnD15XpUw7x/pvdu5ta/GU0UOwZACkbyNqEZ04rUfUPnVFODQ+KZfXY0R80dHMl//2NO8TUrstAkqiUNXiwLnwGpVHcxVrqQmRM2ExKIkchPB8fATzD/+LG3V3FiHUhGNXMSKxNed9x/U20b59ntXMF0oKGSHA48u7c+yAsVE0NuePZkoaYMu7BfFZwjldtJzy2S+VzSVCFnNLQmDEUi6jguATgHWaviJrkZm5dMpEzLctXEUMxXKvQrU/HvFIf8VwyBxOokL6FRdVQpf13L3eqgtRqNHGLN6Tktns01e4txqXgTg4Mumxe7gjz1oOzBs0cse9hha02t5DRoq/mWJKVPzQKzX11tAT7DO8BoxIGk2HVpFXRPX6OsSfepatSRjWjLe4dBVA+qt7Yay7JJfKOpmpW6VgjD4n5ZBod4t5n/g8H/LIYGvBTFSuaHZcLNmFlLJd54nhGZgru7iEtd3vqMohAz/NboY6HpRvqmOd6sAsR4bauD39SOu9ib9rSmf4MtEeLxTG9YDioRze7zaUJ5m5KUsNlykrh5+NMweEht+BPso6sbq8tuFm8SvbdFdJG/RW65VS7eXNnbJC+DUW0hfnltVW9jqW3mt8IGKZamuQgnOWMRch4cDlp0fEsq2iD42KLhPvAwFVdYIQ1TayottLgQckFlVF+PlaWJCB/bjWlOSH0bn2U8xW6DVNhA4KZrdgtz5bW63qHqMLqxGrFEmxXKKgkemD43B6QeiLWBRR4ZbotvV/G/BVPa2YB6LiTqJV5ZPZD8KuEeiG28HjQibBJeG1j5NS4yyN3Ph6YjxrGJZstS88s5T4vE8zvb4Zu1uZIBHwageYr7l1nD0LEIlFnDcEGrIoc/U1IhdOzEMj1FuCg0qVS6FbH/YVAbvTTGbyL1QoUzYjS3i7zSaJ8knUdWmkLu9FB0Fv3BynP8VvHV7jVohBkB7WsgMjPangfmSyMT5hpydPyRdCalWFzhDNnt+lFyvAU0NuMAUX4sbIN9yxFT5NsBxmCEuFatm7j+Wfgf

The JavaScript console has an error:
Uncaught TypeError: Cannot read properties of undefined (reading ‘push’)
at createPlanet (code.html:46:13)
at addPlanets (code.html:40:7)
at code.html:33:3

I really can’t figure out what’s going on here, because I tried the push method in the JavaScript console and it went smoothly without any errors.

Thanks for helping! :grin: :grin:

At the top of the code, you’ve started a code outline, which is good:

  var gameOver = false;  
  addPlanets();

In that code outline, you’ve started the gameOver variable as false, which makes sense since the game probably hasn’t started at this point. Next you start adding planets in the addPlanet() function.

In that function, you create 100 planets using the createPlanet() function:

  function addPlanets() {
    if (gameOver) return;
    for (var i=0; i<100; i++) {
      createPlanet();
    }
  }

And in the createPlanet() function, you set the size to a random number and then… you’ve got a mistake. You add that size to the planets variable with push:

  function createPlanet() {
    var size = r(50);
    planets.push(size);
    // ...
  }

There are two problems here:

  1. You probably want to add planets to the list of items in the planets variable – not the size of the planets
  2. You haven’t defined what planets is.

To fix #2, go back to the code outline at the top and define planets as an empty list:

  var gameOver = false;  
  var planets = [];
  
  addPlanets();

I think you already know how to fix #1 because you already have the right code for this later in your createPlanet() function – it’s just commented out right now. So remove the line that currently does planets.push(size) and un-comment the other two lines at the bottom of the createPlanet() function.

Hopefully that all makes sense. If you have any other questions / problems, please ask :slight_smile:

-Chris

Okay, so I tried that, but the same problems are coming up in the JavaScript console:

Uncaught TypeError: Cannot read properties of
undefined (reading ‘push’)
at createPlanet (code.html:57:13)
at addPlanets (code.html:40:7)
at code.html:33:3
at _.html:10:12

Other than that, there are no other errors, which really puzzles me.
So I made a new file and tried pushing a mesh into a list–and it worked. No errors whatsoever.
Basically, I have no clue what’s wrong here. :frowning:

(And here’s my updated code, just in case you need it)

https://www.code3dgames.com/3de/#B/pVdtb9s2EP7eX3HwlzCtI7vr0g5NWiBz3bRYmgS2uw4YhoKR6IiNJGokZUcJ+t93R1KylNgFhhp+oXnPvfFeeDq+Ukn99njkfp4cm1jL0oLR8ZvByKZaiOibGSDdEx4iyrQ28oeIWBVWq8yM3mf1JKy3M7x9AjAawSWJjA0YYa0srg3uuq1vJvI4E/E8V/AG9ka0QGF7R1tAa6VvhHaw0pO++i3P4JUtUgEDE4tCDEAaWKdCCzC2Wi5BFqAqDdc8F7CWWQYpL0tRvEbGFdfgmFB6Idat6jntsX2yxpEj9OFU85W0NSPc4sNsOo3+FLFV+gWD8ZDev45h37GQ1GXGLQq9p995yhM8gNdgdSW+N4hMXqc26PXyTvIrKQp7RgS2t06lFXsoOfqtYwhPEuY493uex+ic5sF11GyEMGDTcASNq9yUaPKMW0mnvpZFotaRLAqhv8jEpjDq7X0QpKcxd+0QW7gaQCq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kWBnCZV1ng97bDZ2dzIwNejsHKHGcsGrXQsQQMjVo4hDUnh48iuVI29WKFXSBcVZY1Kv0YOHo5bo3eGBPmjXZowh4xXblh3Vi0T7PBjagpkQdDFF0kf4j6Hf5r+Fr3OjQmSED/GkioR7v9iP50IkHXkKfjg9CJ1mp9hj1kbz+0kqMdoBm1A0SFtrAL9rlETFXuBpDBCPGl2hZx++j3Hw==

You’ve run into a quirky JavaScript feature. JavaScript needs variables – like planets – to be defined before they’re used. Instead of this:

  var gameOver = false;  
  addPlanets();
  var avatar = addAvatar();
  var planets = [];

You need to put the planets variable before the call to addPlanets():

  var gameOver = false;  
  var planets = [];
  addPlanets();
  var avatar = addAvatar();

It’s weird because JavaScript thinks the variable is being used as soon as you call the addPlanets() function. Even though planets is defined above addPlanets(). JavaScript can be strange.

-Chris

It’s working now! Thanks!