Skip to content

Circumvent the Problem of a Counter Incrementing Twice in the Same Frame

In this example you'll see how to circumvent the problem of a counter incrementing twice in the same frame by combining SubSource and AddHits.

In the Game Boy Advance library there's a Christmas platformer game called Santa Claus Jr. Advance. There are some achievements for collecting all candy canes in each one of the bonus stages.

Such achievements were supposed to be quite simple, like this:

ID Special? Memory Cmp Type Mem/Val Hits
1 0xCANDIES != Delta 0xCANDIES (N)
2 ResetIf 0xSTAGE != Value 0xBONUS

This translates into "check if the candy canes counter changed N times in the bonus stage".

Note: we're using 0xCANDIES != Delta 0xCANDIES (using !=, and not >) cause when the player has 99 candies and collect one more, the counter goes to zero.

However, there is an aspect in this game (and many others) where if the player collect 2 candy canes simultaneously the counter is increased by 2 in the same frame. When it happens the hit counter is increased by one while the candy counter is increased by two. This, obviously, ruins the logic used above.

In order to circumvent this issue we have to use that technique of using SubSource to count specific increments combined with the AddHits flag.

Let's take a look at the achievement logic and then we'll see the explanation:

Addresses

  • 0x80dc: Candy Canes counter.
  • 0x809c: Stage ID.

So now the structure of the achievement is going to be like this:

Conditions

  1. Together with condition 2, is a trick used to check if 0xCANDIES - Delta 0xCANDIES = 2. In other words, the candy counter was incremented by two.

  2. Used together with condition 1 (as explained above) and 3. As explained in its respective page, when the AddHits condition is true, the hit counter in the next condition is incremented. Therefore, in our example here, when condition 2 is true, the hitcounter in condition 3 is incremented.

  3. Count when the candy counter changes.

  4. Reset the counter if not in the Bonus stage.

All these conditions could be translated as "while in the bonus stage, trigger the achievement if the candy counter changes 99 times, and if the candy counter is incremented by 2 in the same frame, count an extra change in the candy counter."

Changeloglink

Last 10 changes on this page:

  • [2019-03-16 22:33] meleu: trimming down
  • [2019-02-28 21:06] Thoooreau: text
  • [2019-02-24 12:51] meleu: grammar
  • [2019-02-19 19:31] meleu: not every bonus have 99 candy canes
  • [2019-02-18 17:43] meleu: cosmetic
  • [2019-02-18 17:43] meleu: a little bit of more explanation
  • [2019-02-17 23:33] meleu: trimmed down version
  • [2019-02-17 21:20] Thoooreau: minor stuff
  • [2019-02-17 21:18] Thoooreau: minor stuff
  • [2019-02-16 20:04] meleu: WIP