Intro to 3D Game Development

0 of 193 lessons complete (0%)

Scripting Part 3: Finishing the Game Loop

Randomize the Treasure Spot

You don’t have access to this lesson

Please register or sign in to access the course content.

Now we are getting a new random number each time we run the game, but it is a decimal number (also known as a float), and array indices can only be whole numbers (also known as an int or integer). To fix that, we can use another Math library function, floor, which rounds numbers down to the nearest integer

  1. In the randomizeTreasure function, create a new variable after the randomNumber variable called randomTreasure.
  2. Set randomTreasure to numSpots times randomNumber, rounded down with Math.floor.
  3. Change the last index you had for digSpots to randomTreasure, and let the program send the event to the index that was randomly selected.