Take your skills to the next level with these bite-sized tutorials!

Processing Resets

Posted August 24, 2020

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 have any questions, comments or criticism, post them in the comments below or reach out to me on Twitter @ohsat_games! Special thanks to Stephane Dallongeville for creating SGDK and everyone in the SGDK Discord for their help and keeping the dream alive!

Download the source code

All patrons on Patreon get the complete source code for this tutorial, as well as other perks such as early access! Become a Patron!
Just Want to Buy Me a Coffee?

Check out the rest of this tutorial series!

  • Creating Graphics for the Mega Drive
  • 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
  • Get Words in Your Inbox!

    Be oldschool and sign up for my newsletter to get updates! Just enter your email address, prove you're not part of Skynet and you're good to go!



    Powered by CleverReach. I will not send you spam or sell/give your email address to someone else.  You can of course unsubscribe at any time. By clicking the subscribe button above, you confirm that you have read and agreed to our privacy policy.

    By using the Disqus service you confirm that you have read and agreed to the privacy policy.

    comments powered by Disqus

    Related Posts

    Streets of Rage 2 Design Docs

    A few years ago, Yuzo Koshiro posted a pile of old game design documents for Bare Knuckle 2 aka Streets of Rage 2 on the Ancient blog to commemorate the release of Streets of Rage 2 3D on the Nintendo 3DS. These documents gave a deep insight into the game’s inner workings, technical aspects, designs and even some cut content. They were an awesome resource for one of the most awesome games ever created.

    Read More

    Make a Space Shooter for the Mega Drive!

    It’s time for another SGDK tutorial series! After doing a single player Pong game and an endless runner, it’s time to reach for the stars… and make a space shooter! Apart from things like scrolling and animating sprites, this new series will show you how to deal with multiple entities and their collisions, how to randomly generate backgrounds and more! This project builds upon the previous tutorials, so if you’re new to SGDK programming and have not done those yet, I highly recommend starting with Megapong.
    Read More

    Patreon Revamps

    Hey there, what’s up? Things are continuing to evolve, as I’ve now updated my Patreon to give patrons more perks! Apart from early access to new tutorials and posts, the biggest one is probably the ability to peek behind the scenes…and there will be a lot to peek at in the coming months! This year I’m writing and submitting my MA thesis, meaning that I will have to do more stuff to make ends meet.
    Read More