Doss Academy
Skip to learning content

Lesson Notes — Changing the Rules: Rewriting a Formula in a Mod

Course 2: Game Design & Development Principles / Unit 6: Game Design Mechanics, Rules & Documentation / Lesson 11: Changing the Rules: Rewriting a Formula in a Mod

The golden rule: Keep working inside your own expansion folder in the expansions folder, in the expansion.js file you made in Lesson 10. Do not edit index.html.

Lesson overview

Last lesson you changed values: how many points an event is worth. Rules are the other half of a mechanic. A rule is a formula that takes numbers in and gives a decision back. Today you replace two of One Life’s formulas: the one that turns popularity into friend slots, and the one that decides who wins homecoming.

Functions can be replaced

In the game’s code, a rule lives in a function. A function takes in values (parameters) and gives back an answer (a return value). For example, the original friend slot rule is:

function friendLimit() {
  return Math.max(0, Math.floor(popularityScore() / 10));
}

Because your expansion loads after the game, it can give friendLimit a new body. Replacing a function this way is called an override.

Rule mod 1: friend slots

Add this to Part 2 of your expansion file, inside the wrapper and above the console.log line:

friendLimit = function () {
  return Math.floor(popularityScore() / 8);   // was 10
};

Now every 8 points of popularity opens a friend slot instead of every 10. Open the console and type friendLimit() in a new game to check the number.

Rule mod 2: homecoming

The original rule is an if/then: if it is senior year and popularity is at least 300, the chance is 40 plus 1 for every 10 points above 300, up to 95. Otherwise it is 0. Here it is rewritten to need only 150:

const originalHomecoming = homecomingWinPercent;

homecomingWinPercent = function (year, pop) {
  if (year === 4 && pop >= 150) {
    return Math.min(95, 40 + Math.floor((pop - 150) / 10));
  }
  return 0;
};

The first line saves the original function in a variable. You do not have to use it, but keeping the original lets you compare, call it, or undo your change. Test your formula in the console with homecomingWinPercent(4, 150), which should return 40.

Read the if/then

  • year === 4 checks that it is senior year.
  • && means and: both sides must be true.
  • pop >= 150 checks that popularity is at least 150.
  • Math.min(95, ...) makes sure the chance never goes above 95.
  • Math.floor(...) rounds down to a whole number.

Design check: who does your rule help?

Every rule change helps some players and hurts others. Lowering the homecoming requirement makes homecoming reachable for quieter players. It also makes it less special. Write down both effects on your planner.

Debug challenge: one number, two formulas

Temporarily add this to Part 2 and start a new game:

const originalPopularity = popularityScore;
popularityScore = function () {
  return originalPopularity() * 2;
};

Check the friend slots with friendLimit() and look at the Popularity on the dashboard. One changed and one did not. Search index.html for updatePlayerStats and find out why. (You may read the game’s files. You just may not change them.) Designers call it a bug risk when the same formula is written in two places. It is better to have a single source of truth that everything else calls.

Your turn

  1. Add rule mod 1 and rule mod 2 to Part 2 of your file. Change the numbers to your own version.
  2. Fill in the test table on your planner using your formulas, then confirm two of the values in the console.
  3. Complete the debug challenge and explain the cause on your handout.
  4. Remove the debug challenge code when you are done so it does not affect your later testing.

Check your understanding

  1. What is the difference between changing a value and changing a rule?
  2. Why does the expansion save the original function in a variable?
  3. Why is having a formula in two places a problem?

Use this lesson’s Key Terms page to build your glossary.

Discuss and submit in Google Classroom

Use this site to read, watch, and get shared learning materials. Keep completed work, questions, and comments in your class on Google Classroom.

Open Google Classroom

Choose your class and the assignment named by your teacher.