Processing Resets

Posted August 24, 2020

NOTE:
This tutorial is most likely not compatible with versions of SGDK above 1.70. Unfortunately, I simply do not have the capacity or ability to update them right now. Read more about it here. Sorry.

Hold up!

If you like 90s platformers, check out my new game Kid Bubblegum!

Remember when consoles had reset buttons? Those were the days. No menus, no closing applications… just slam that button and the game resets. Simple and effective. But is it actually that simple? It turns out that when it comes to the Mega Drive, there’s a little feature that could turn into a trap if you’re not aware of it.,

I’m talking about the fact that there are two types of reset: A soft reset and a hard reset. A soft reset is the reset type you’re probably thinking about: It occurs when you press the reset button on your console. A hard reset is basically the same as turning the console off and on again. But why should we care about the difference?

Well, create a new SGDK project and let me show you!

Let’s demonstrate with a very simple program that simply displays an increasing number on screen. Define two variables outside of main():

int ticker = 0;
char tickerString[4] = "0000";

And then draw the value of ticker on the screen by adding this code to the while(1) game loop inside main():

ticker++;
sprintf(tickerString,"%d",ticker);
VDP_clearText(12,12,4);
VDP_drawText(tickerString,12,12);

Compile and run the game and you’ll see a number that increases every frame. Alright, now reset your emulator (in BlastEm you do that by pressing Tab, in Gens you need to go to CPU -> Reset 68000).

Notice anything? Even though you reset the console, the number doesn’t reset back to zero! Seems a bit strange, doesn’t it?

Well, that’s actually intentional. SGDK doesn’t clear the entire RAM on a soft reset, so that you can keep certain data like high scores and settings alive even after a reset. It would be a bit overkill to use SRAM (saving using a battery in the cartridge) just for that. Plus, you could do even more interesting things with it… Have you ever played the original X-Men game on the Mega Drive? That game had you reset your console to gain access to the final level. That obviously wouldn’t work if a soft reset deleted everything! And it also meant that you couldn’t do it when playing on the Sega Nomad, because that handheld didn’t have a reset button. Whoops.

Anyway, that’s why our number just keeps going up even after a soft reset. Obviously this can cause issues if we’re not careful. If we don’t clear the high score on a soft reset, players could rack up an infinite score by replaying the first level over and over, for example. Or our game could just outright break down because of memory issues. So how do we manage our data when it comes to resets? There are actually two ways.

1. Initializing

The simplest way is to initialize everything we need reset in main() before our game loop starts. The main() function is called every time we reset the console (using a soft or hard reset), so in our case, adding a ticker = 0; before while(1) would solve our problem and set the ticker back to 0 after each reset of the emulator.

2. Checking the Reset Type

But if you want more control over what gets reset and when, there’s actually a trick that lets you check for resets. If you add a parameter to main(), you can see whether the console has been reset and how:

int main(int resetType){
    //...
}

When the console has just been turned on, resetType will equal 1, indicating a hard reset. But if the reset button has just been pressed, resetType will equal 0! (You can call the parameter anything you want, by the way).

Let’s check if it works. Add the following chunk of code at the beginning of main(int resetType):

char typeString[14] = "";
sprintf(typeString,"Reset Type: %d",resetType);
VDP_drawText(typeString,0,0);

If you compile and run the game now, you should see that the reset type is 1. Press tab (or whatever you do to soft-reset) and you’ll see that now it’s 0! So using an if-statement like if(resetType == 0){ ... } you can now control how your game should behave on a soft reset!

If you've got problems or questions, join the official SGDK Discord! It's full of people a lot smarter and skilled than me. Of course you're also welcome to just hang out and have fun!

Join my Discord Server!

Hang out, get news, be excellent!

Come hang out!

Want To Buy Me a Coffee?

Coffee rules, and it keeps me going! I'll take beer too, though.

Check out the rest of this tutorial series!

  • Creating Graphics for the Mega Drive
  • How to Quickly Generate C Prototype Functions in VSCode
  • Color Swapping
  • 4 Programs For Creating Mega Drive Graphics
  • Editing the Rom Header
  • Simple Game States
  • Creating a Simple Menu
  • Changing The Text Color in SGDK
  • Playing Music in SGDK
  • Converting VGZ to VGM
  • Processing Resets
  • Drawing Tiles From Code
  • Make a Text Crawl, Streets of Rage Style
  • Scrolling Maps
  • Placing Tiles
  • Simple Animated Tiles in SGDK
  • Simple Password System
  • Checking the Region
  • Playing Multiple Music Tracks
  • By using the Disqus service you confirm that you have read and agreed to the privacy policy.

    comments powered by Disqus