Problem with my Pickup Script

Dactyl

New member
I only have 3 pickups in my game, myLives, myAmmo and myScore.
this is my pickup script nothing is updating my hud for any of my pickups?
;;; Pickup scripts. LDA Object_type,x CMP #$02 BNE +notAmmoPickup ;;; Increase ammo LDA #$05 STA myAmmo UpdateHudElement #$07 JMP +endPickups +notAmmoPickup: CMP #$03 BNE +notScorePickup ;;; Increase score INC myScore LDA myScore CMP #$0A BNE +dontNormalizeScore LDA #$09 STA myScore +dontNormalizeScore UpdateHudElement #$03 JMP +endPickups +notScorePickup: CMP #$04 BNE +notLivesPickup ;;; Increase lives INC myLives LDA myLives CMP #$04 BNE +dontNormalizeLives LDA #$03 STA myLives +dontNormalizeLives UpdateHudElement #$01 JMP +endPickups +notLivesPickup: +endPickups: any help would be super cool i have been struggling with the pickups updating my hud ?
 

dale_coop

Moderator
Staff member
Wow, that post is kinda painful to read .
don't be afraid to use carriage returns , empty lines, ... spaces ;)

Hmmm...
Make sure your pickup objects are the object 02 for the Ammo, the object 03 for Score and the object 04 for lives?
(reminder: in your Game Object list, the Player object is 00, the object just under it is 01, the next one is 02, ...)
 

TheRetroBro

Active member
I only have 3 pickups in my game, myLives, myAmmo and myScore.
this is my pickup script nothing is updating my hud for any of my pickups?
;;; Pickup scripts. LDA Object_type,x CMP #$02 BNE +notAmmoPickup ;;; Increase ammo LDA #$05 STA myAmmo UpdateHudElement #$07 JMP +endPickups +notAmmoPickup: CMP #$03 BNE +notScorePickup ;;; Increase score INC myScore LDA myScore CMP #$0A BNE +dontNormalizeScore LDA #$09 STA myScore +dontNormalizeScore UpdateHudElement #$03 JMP +endPickups +notScorePickup: CMP #$04 BNE +notLivesPickup ;;; Increase lives INC myLives LDA myLives CMP #$04 BNE +dontNormalizeLives LDA #$03 STA myLives +dontNormalizeLives UpdateHudElement #$01 JMP +endPickups +notLivesPickup: +endPickups: any help would be super cool i have been struggling with the pickups updating my hud ?
this hurts my brain

assuming your objects are what they are i made your code alittle more readable and easier to add more variables: Cant test it atm as im at work.


LDA Object_type,x
CMP #2 ;;;;;;check if item is object 2 ammo pickup
BNE + ;;;;branching to check next if not 2
JMP doammopickup ;;; is object 2 so jump to ammo pickup
+
CMP #3 ;;; check if object 3 score pickup
BNE+ ;;; branching to next if if not object 3
JMP doscore ;;; is object 3 so jump to doscore
+
CMP #4 ;;;check if object is 4 lives pickup
BNE + ;;;branching to next if not object 4
JMP dolives ;;; is object 4 so branch to lives pickup
+
JMP +endPickups


doammopickup
LDA #$05
STA myAmmo
UpdateHudElement #$07
JMP +endPickups


doscore
INC myScore
LDA myScore
CMP #$0A
BNE +dontNormalizeScore
LDA #$09
STA myScore

+dontNormalizeScore
UpdateHudElement #$03
JMP +endPickups

dolives
INC myLives
LDA myLives
CMP #$04
BNE +dontNormalizeLives
LDA #$03
STA myLives

+dontNormalizeLives
UpdateHudElement #$01


+endPickups
 

dale_coop

Moderator
Staff member
assuming your objects are what they are i made your code alittle more readable and easier to add more variables: Cant test it atm as im at work.

I am sorry but I disagree, your code is not better, quite the opposite.

We should NOT reverse a direct branch-out with another branch-out and jump if it's not necessary (if there's no "branch-out" compilation error), because that wastes precious cycles! The more you save them, the more optimized your code will be, which means fewer slowdowns!

Here's the original script, indented (easier to read):
Code:
;;; Pickup scripts.

LDA Object_type,x
CMP #$02
BNE +notAmmoPickup
    ;;; Increase ammo
    LDA #$05
    STA myAmmo
    UpdateHudElement #$07
    JMP +endPickups
+notAmmoPickup:

CMP #$03
BNE +notScorePickup
    ;;; Increase score
    INC myScore
   
    LDA myScore
    CMP #$0A
    BNE +dontNormalizeScore
        LDA #$09
        STA myScore
    +dontNormalizeScore:
    UpdateHudElement #$03
    JMP +endPickups
+notScorePickup:

CMP #$04
BNE +notLivesPickup
    ;;; Increase lives
    INC myLives

    LDA myLives
    CMP #$04
    BNE +dontNormalizeLives
        LDA #$03
        STA myLives
    +dontNormalizeLives:
    UpdateHudElement #$01
    JMP +endPickups
+notLivesPickup:

+endPickups:

But yes, that code should work IF:
- The pickup objects used are actually Object 02 for Ammo, Object 03 for Score, and Object 04 for Lives.
- The HUD elements are set accordingly: HUD Element 7 for Ammo, HUD Element 3 for Score, and HUD Element 4 for Lives.
Otherwise, you’ll need to modify the code to match your objects and HUD elements.
 

TheRetroBro

Active member
I am sorry but I disagree, your code is not better, quite the opposite.

We should NOT reverse a direct branch-out with another branch-out and jump if it's not necessary (if there's no "branch-out" compilation error), because that wastes precious cycles! The more you save them, the more optimized your code will be, which means fewer slowdowns!

Here's the original script, indented (easier to read):
Code:
;;; Pickup scripts.

LDA Object_type,x
CMP #$02
BNE +notAmmoPickup
    ;;; Increase ammo
    LDA #$05
    STA myAmmo
    UpdateHudElement #$07
    JMP +endPickups
+notAmmoPickup:

CMP #$03
BNE +notScorePickup
    ;;; Increase score
    INC myScore
  
    LDA myScore
    CMP #$0A
    BNE +dontNormalizeScore
        LDA #$09
        STA myScore
    +dontNormalizeScore:
    UpdateHudElement #$03
    JMP +endPickups
+notScorePickup:

CMP #$04
BNE +notLivesPickup
    ;;; Increase lives
    INC myLives

    LDA myLives
    CMP #$04
    BNE +dontNormalizeLives
        LDA #$03
        STA myLives
    +dontNormalizeLives:
    UpdateHudElement #$01
    JMP +endPickups
+notLivesPickup:

+endPickups:

But yes, that code should work IF:
- The pickup objects used are actually Object 02 for Ammo, Object 03 for Score, and Object 04 for Lives.
- The HUD elements are set accordingly: HUD Element 7 for Ammo, HUD Element 3 for Score, and HUD Element 4 for Lives.
Otherwise, you’ll need to modify the code to match your objects and HUD elements.
Oh OK. I appreciate the lesson. I didn't realize using bne and jumping was wasting cycles as you noted and possibly slowing things down. Still learning here myself and try to help when I can.
 
Top Bottom