(4.5.9) Keys in Arcade Plat Issues (SOLVED)

Hello NesMakers!
The key pickup in my game has been causing a multitude of problems:

Whenever I pick up a key item and then kill an enemy, my game crashes.

On top of that, other pickups also don't work if my key script is written first in my Pickup scripts.
Adding the key pickup script anywhere else in the code causes the key to not function.

I've been stumped on this for a while, and I'm not sure how to fix this.

Any help is appreciated!

here is my Pickup Script:
Code:
;;; Pickup scripts.
;;; Right now, we need to look at the type of object that is in X.
;;; Then, we have to do pickup behavior accordingly.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KEY;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
LDA Object_type,x
CMP #$06
    BNE +notThisPickup
        INC myKeys
        LDA myKeys
        ;CMP #$3E7 ;; one more than the max
        ;BNE +dontNormalizeValue
        ;    LDA #$63 ;; normalize the value to 999 if it got bigger than 999
        ;    STA myKeys
        ;+dontNormalizeValue
        UpdateHudElement #$07
TriggerScreen screenType
AddValue #$02, myKeys, #$0, #$00
AddValue #$07, myScore, #$1, #$02
    UpdateHudElement #$06
;JMP +benchmark

+notThisPickup:
        JMP +endPickups
;; Here is an example of how to set variable myAmmo to 5 if the pickup type
;; is gameobject 3.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;MELON;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    LDA Object_type,x
    CMP #$03 ;;What object is your pickup?  00 - 0f
    BNE +notThisPickup
        ;;;;;;;;;;;;;;;;;;;;;;;;; What do you want to do to the value?
        ;;;;;;;;;;;;;;;;;;;;;;;;; Increase?  Decrease?  Set it to a number?
        ;;;;;;;;;;;;;;;;;;;;;;;;; Here, we are setting myAmmo to 5.
        LDA #$05
        STA myAmmo
        ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
        ;;;;;;;;;;;;;;;;;;;;;;;;; Do we need to update the HUD to reflect this?
        ;;;;;;;;;;;;;;;;;;;;;;;;; If so, which element is the above variable represented in?
;DrawSprite #168, #28, #113, #01
;DrawSprite #176, #28, #114, #01
 
        UpdateHudElement #$03
AddValue #$07, myScore, #$1, #$02
    UpdateHudElement #$06
JMP +benchmark


        JMP +endPickups
+notThisPickup:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;FIG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    CMP #$04
    BNE +notThisPickup
        INC myCash
        LDA myCash
        ;CMP #$3E7 ;; one more than the max
        BNE +dontNormalizeValue
            LDA #$63 ;; normalize the value to 999 if it got bigger than 999
            STA myCash
        +dontNormalizeValue
        UpdateHudElement #$02

AddValue #$02, myCash, #$0, #$00
AddValue #$07, myScore, #$1, #$02
    UpdateHudElement #$06
JMP +benchmark
        JMP +endPickups

+notThisPickup:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;HEART;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
LDA Object_type,x
CMP #$07
    BNE +notThisPickup       
        LDA myHealth
INC myHealth
    ;+increaseHealth
        CMP #$03 ;; one more than the max
        BNE +dontNormalizeValue
            LDA #$03 ;; normalize the value to 3 if it got bigger than 3

            STA myHealth
        +dontNormalizeValue
        UpdateHudElement #$04
AddValue #$07, myScore, #$1, #$02
    UpdateHudElement #$06
JMP +benchmark
        JMP +endPickups

+notThisPickup:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;APPLE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
LDA Object_type,x
    CMP #$09 ;;What object is your pickup?  00 - 0f
    BNE +notThisPickup
        LDA #$05
        STA myAmmo2
        UpdateHudElement #$05
AddValue #$07, myScore, #$1, #$02
    UpdateHudElement #$06
JMP +benchmark
+notThisPickup
        JMP +endPickups

    
;;;;;;;;;;;;BENCHMARK;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+benchmark:
LDA myScore_10000        ;;;;Load the 10,000 place
CMP myBenchmark            ;;;;Check if the 10,000 place = benchmark
BNE +giveExtraFigs        ;;;;If so...

        ;;We Will Increase the Benchmark with INC
        ;;INC adds variables just by *tens*
        
    INC myBenchmark        ;;;;Increase the benchmark
    INC myCash_10          ;;;;Add an extra Figs, by tens
    

    ;;;Update HUD Element -- only if you use a tile HUD
    ;;;If you have a Sprite HUD, you can skip this
    UpdateHudElement myLives, #$01 ;<------ or whatever number your lives variable is in the HUD
+giveExtraFigs
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+endPickups
 

kevin81

Well-known member
The key pickup in my game has been causing a multitude of problems:
Whenever I pick up a key item and then kill an enemy, my game crashes.
There are a couple things in your pickup script that probably won't do what you expect them to do. For example, the following at line 17:
Code:
AddValue #$02, myKeys, #$0, #$00
adds zero points to an assumed three-digit variable called myKeys. You can remove that line altogether, but since it doesn't do anything specifically (other than waste clock cycles), it shouldn't be causing the crash though. I think that the main problem actually lies in HUD element #$07. Could you try and comment out line 15 from your script to see if it at least doesn't crash anymore?
Code:
; UpdateHudElement #$07

Something else I've noticed is that in lines 53-59, you're trying to handle myCash as a single-byte variable (0-255) capped at 99. Yet in line 115 you're handling it as a multibyte decimal, where each byte should have a value from 0 to 9. So I think you either have to rewrite the first section to use the AddValue macro, or add 10 to myCash in the second section, depending on which of the two flavors you were trying to use here.
(side note: the CMP #$3E7 line did not work because the NES is an 8-bit system, which only goes up to #$FF. Not CMP'ing myCash to anything prevents the capping to work properly here though.)

On top of that, other pickups also don't work if my key script is written first in my Pickup scripts.
That is caused by the JMP +endPickups in line 23 of your pickup script. If the item is not a key, it always jumps to the end of the script now. I think you either want that JMP statement above the +notThisPickup: label, or you need to restore the JMP at line 20, and remove the JMP at line 23 altogether.
 
There are a couple things in your pickup script that probably won't do what you expect them to do. For example, the following at line 17:
Code:
AddValue #$02, myKeys, #$0, #$00
adds zero points to an assumed three-digit variable called myKeys. You can remove that line altogether, but since it doesn't do anything specifically (other than waste clock cycles), it shouldn't be causing the crash though. I think that the main problem actually lies in HUD element #$07. Could you try and comment out line 15 from your script to see if it at least doesn't crash anymore?
Code:
; UpdateHudElement #$07

Something else I've noticed is that in lines 53-59, you're trying to handle myCash as a single-byte variable (0-255) capped at 99. Yet in line 115 you're handling it as a multibyte decimal, where each byte should have a value from 0 to 9. So I think you either have to rewrite the first section to use the AddValue macro, or add 10 to myCash in the second section, depending on which of the two flavors you were trying to use here.
(side note: the CMP #$3E7 line did not work because the NES is an 8-bit system, which only goes up to #$FF. Not CMP'ing myCash to anything prevents the capping to work properly here though.)


That is caused by the JMP +endPickups in line 23 of your pickup script. If the item is not a key, it always jumps to the end of the script now. I think you either want that JMP statement above the +notThisPickup: label, or you need to restore the JMP at line 20, and remove the JMP at line 23 altogether.
Thank you, Kevin! I was able to get the other items to work now. However, I am still having issues with the crashing after killing enemies. After some digging, it seems Line 8 might be the issue.
Code:
INC myKeys
I'm not certain how to bypass this, as this line is important for keys to work.
 
Top Bottom