Adding Old School Cheat Codes to Your Game! (v4.0.11) [Intermediate Difficulty]

Post Reply
User avatar
MistSonata
Posts: 318
Joined: Fri Feb 16, 2018 7:10 am

Adding Old School Cheat Codes to Your Game! (v4.0.11) [Intermediate Difficulty]

Post by MistSonata » Mon Sep 10, 2018 3:44 pm

Hey, remember when Konami wasn't hot garbage? I do! And in honor of that memory, I made a code that allows you to create a Konami-code-like cheat code system for your title screen. Here's a quick demo I made showing it off!

STEP 1: Set up a file for look up tables if you haven't already.

A lot of my codes use look up tables, and I find it's sometimes easier to use one file for all the smaller tables I have. Create an asm file and put it somewhere in the GameEngineData folder (if you're not sure, just put it into GameEngineData\Routines\UserScripts\), and paste the following code into it:

Code: Select all

CheatCodeInputs: ;; Make sure the number of inputs here is the same number as the one in the beginning of the press start script
.db %00010000, %00010000, %00100000, %00100000, %01000000, %10000000, %01000000, %10000000, %00000010, %00000001
;; by the way, I'd highly recommend doing only one input at a time, the NES reads scripts VERY quickly and unless you have superhuman precision it'll be impossible to activate.

;;   7 6 5 4 3 2 1 0 <-- Gamepad Bits
;;   | | | | | | | |
;;   | | | | | | | +--- RIGHT
;;   | | | | | | +----- LEFT
;;   | | | | | +------- DOWN
;;   | | | | +--------- UP
;;   | | | +----------- START
;;   | | +------------- SELECT
;;   | +--------------- B BUTTON
;;   +----------------- A BUTTON
IMPORTANT!!! This is where you'll define your cheat code inputs. Right now it's set to use the Konami code (which I'm calling the "Komami code" for now), but you can set it to use whatever code inputs you want, just make sure there's a comma between them. I added a little graph with the bits labelled to their corresponding buttons, to make it easier. Also, if you change the number of code inputs, be sure to change the number in the "press start" script to reflect that (it's on line 4). Once you're done, name your asm file whatever you like and make note of where you saved it.

Now that you have that settled, you'll want to go into your MainASM.asm file in GameEngineData and include your script. Make sure you put it down toward the end, after the include for "HUD_DEFINES.dat", but before "columnTableLo". Like this:

Image


STEP 2: Download the scripts and import them into NESmaker

Now you'll want to import your scripts. You can either download the attachment, or copy the codes pasted below and insert them into asm files yourself. Whichever's easier. (I'll be loading the codes up to NESmakerfiles.com soon as well.)
KomamiCode.rar
(1.48 KiB) Downloaded 99 times
KomamiCodeInputs.asm:

Code: Select all

Komami_Code:
    LDA CodeTimer ;; Make sure you create this and the CodeIndex variable in Project Settings (under the User Variables, set both initial values to 0)
    BNE dontResetCodeIndex ;;If the code timer is zero, that means the player ran out of time, and we need to reset the code index, otherwise don't reset it.
    
    LDA #$00
    STA CodeIndex ;;here's where we reset the code index
    
    dontResetCodeIndex:
    
    LDX CodeIndex
    LDA CheatCodeInputs,x ;;This takes the code index (now in the x register) and loads the value in the CheatCodeInputs look up table
    CMP gamepad ;; This takes the value we just loaded and compares it to the gamepad bits
    BNE WrongInputWasMade ;;If they aren't equal, that means the wrong input was made, otherwise move on.
    
    INC CodeIndex ;;CodeIndex increases by 1
    LDA #40 ;; IMPORTANT! Change this number if you want to raise or lower the amount of time the player has to make the next input correctly. 
    ;; 40 will allow the player about 40 frames (given that the NES runs at around 60fps for ntsc games, that's about 2/3rds of a second)
    STA CodeTimer ;;We reset the code timer so now it will tick down by 1 every frame.
    
    ;PlaySound #SFX_LOCKED_DOOR ;; You can put a sound here so that it will play whenever you enter a correct input.
    
    RTS
    
    WrongInputWasMade:
    ;; You could also play a discouraging or "error" sound here if you want to let the player know that they made an incorrect input
    
    LDA #$00
    STA CodeIndex
    RTS
Komami_StartScreen.asm

Code: Select all

Press_StartonStartScreen:

    LDA CodeIndex ;;Now that the player has pressed start, we'll check the code index to see if the player input all the codes correctly
    CMP #10 ;;IMPORTANT! If you changed the amount of inputs in your code, you will want to change this to match.
    BCC cheatCodeNotComplete ;;BCC branches when the carry flag is cleared, or in the case of a CMP, when the value in A is less than the value it's being compared to.
    
    ;; INSERT CODE YOU WANT TO RUN WHEN SUCCESSFULLY ENTERING CHEAT CODE HERE
    
    JMP StartTheGame
    
    cheatCodeNotComplete:
    
    ;; INSERT CODE YOU WANT TO RUN WHEN CHEAT CODE IS ENTERED INCORRECTLY (OPTIONAL)
    
    StartTheGame:
    
    LDA #$00
    STA CodeIndex ;; These codes were added as a redundancy, just in case someone decided to try turbo mashing the start button after completing the code. 
    STA CodeTimer ;; It also resets the index in case the player goes back to the start screen after this.
    
    LDA #STATE_START_GAME
    STA change_state
  
    LDA #%10000000
    STA gameHandler ;; turn sprites on
    RTS
IMPORTANT!!! This is the part you want to pay attention to. See those lines with the ";; INSERT CODE" etc etc? That's where you're going to define what happens when you input the code correctly and what happens when you don't. This is why I labelled this intermediate difficulty, as you'll have to define what effects the code has yourself. After the press start script is done checking your code input, it'll start your game like normal.

Once you import those codes into your input scripts, add them into your editor. With the start screen, you can simply set it up to when the player presses start. The KomamiCodeInputs script is a bit more... involved.

Image

You can see here that I highlighted all the inputs that my code uses (Up, down, left, right, b, and a). I wouldn't recommend using the same button for the start screen and the input script, though, as you might run into trouble there.

STEP 3: Set up some custom variables!

Normally you'd have to dive into the code for this, but thankfully the newest version of NESmaker allows you to define your own custom variables. Just go to Project Settings>User Variables, and create the following:

Image

Easy peasy! Now that we have that over with, it's time for the last step...

STEP 4: Modifying HandleGameTimer.asm

You can find this script by going into Project Settings>Script Settings, highlighting the "Handle Game Timer" script, and clicking the "Edit" button. From there, you'll want to enter the following code right after the line "dontUpdateGameTimer:"...

Code: Select all

	LDA CodeTimer
	BEQ SkipCodeTimerCooldown
	DEC CodeTimer
	SkipCodeTimerCooldown:
Like so:

Image

Once that's done, you should have a fully functioning cheat code script!
User avatar
dale_coop
Posts: 5624
Joined: Fri Feb 16, 2018 7:05 am
Location: France

Re: Adding Old School Cheat Codes to Your Game! (v4.0.11) [Intermediate Difficulty]

Post by dale_coop » Mon Sep 10, 2018 3:59 pm

Wow...!!!! need to test that asap. Thank you, MistSonata <3
-----
Sorry about my poor english
All I need: A Damn Fine Cup of Coffee
My games: PRESS START GAME / UNDERGROUND ADVENTURE / UNDERGROUND ADVENTURE (Arcade version - Byte-Off-2019)
My son's game: KUBO 3
User avatar
WolfMerrik
Posts: 199
Joined: Sat Aug 11, 2018 4:59 pm
Location: Maine
Contact:

Re: Adding Old School Cheat Codes to Your Game! (v4.0.11) [Intermediate Difficulty]

Post by WolfMerrik » Mon Sep 10, 2018 6:20 pm

This is SOOOO RAD!
I wanted to do something like this eventually, but didn't even know where to start... you are great!
ImageWant to download & share Image NESMaker Resources? Check out My Files on www.NESMakerFiles.com Image
Post Reply