Intro to 3D Game Development

0 of 193 lessons complete (0%)

Scripting Part 2: Making Code Respond to Players

Make a Separate Function for the Chest Opening Code

You don’t have access to this lesson

Please register or sign in to access the course content.

In this next step, we’ll make a separate animation function for the Chest_Lid, the same way you did for the chest.

  1. In the ChestLidScript, create a new line below the startOpeningLidAnimation function.
  2. Create a new, empty function called openChest
  3. Copy all the code inside the if(this.startAnimation && !this.stopAnimation) statement, and paste it into the openChest function
  4. Fix indentations using Shift+Tab
  5. Call the openChest function where your code used to be in the tick function, inside the if(this.startAnimation && !this.stopAnimation) statement.

Tip: Matching Pairs of Brackets

One of the most common bugs that happens when moving TypeScript code around is accidentally missing a { or } curly bracket. The errors that this gives can be very confusing to read, because the order of everything gets jumbled up, but often it will be be something like, “Error: Expected … or “Error: Unexpected …

Indenting code is one of the tools we use to visually check that all our pairs of brackets line up.

  • Check that each } closing curly bracket lines up with the beginning of the line with its { opening curly bracket


The code editor gives you another way to check, however. If you click a { or } curly bracket, it will highlight the other bracket that goes with that pair. If the editor is highlighting the wrong bracket that means there is either a misplaced one between them, or one is missing.