Extra life code question.

Dev Individual

New member
So I want to make an extra life object for my game, and I tried to use some code from the maze game advanced tutorial. As seen below, this is what I put in:

HTML clipboard pickupLabel:
LDA Object_type,x
CMP #$04
BNE +notThisPickup
INC myLives
UpdateHudElement #$01
PlaySound #sfx_OneUp
+notThisPickup:
CMP #$01
BNE +notThisPickup

JMP RESET

+notThisPickup

+endPickups

For some reason, the "INC" isn't highlighted like "LDA", "CMP", "BNE" and "JMP". So whenever I touched my extra life object, it doesn't change the number of lives I have. Why might this be? Please let me know when you can, thank you.
 

dale_coop

Moderator
Staff member
What game object are you using for the "extra life" pickup ? (according to your code, it should be the 5th object in the "Game Objects" list). I am pretty sure it's your mistake here.
For the infirmation, "INC" is correct, it means it will INCrease the value on the myLives variable. And "UpdateHudElement #$01" will update/refresh visually the hud display (the 2nd element oh the HUD Elements).

Also, I see a small error in that code, here a fixed version:
Code:
LDA Object_type,x
CMP #$04
BNE +notThisPickup
  INC myLives
  UpdateHudElement #$01
  PlaySound #sfx_OneUp
  JMP +endPickups  ;; <-- dale_coop here, that line is important
+notThisPickup:
CMP #$01
BNE +notThisPickup
  JMP RESET
+notThisPickup
 

Dev Individual

New member
I chose the 5th object in the game objects because it's called the health pickup, I figured it would be easier for me to remember. I updated the code using what you sent and changed the variables so that the melee object would now be the extra life item.

pickupLabel:
LDA Object_type,x
CMP #$01
BNE +notThisPickup
INC myLives
UpdateHudElement #$01
PlaySound #sfx_OneUp
JMP +endPickups ;; <-- dale_coop here, that line is important
+notThisPickup:
CMP #$02
BNE +notThisPickup
JMP RESET
+notThisPickup

+endPickups

But it still won't add to my life counter. I made sure that "myLives" is set to hud element 1 as well. I have set it so that the "max value" is set to three digits rather than just 1 like in the tutorial, could that be impacting the result.
 

dale_coop

Moderator
Staff member
Oh! I see... several mistakes here.
First, please, make sure to share screenshots when you have issues, to help us to check what's your settings.
If you use the game object labeled "Health pickup", and using the Adventure module, it's the 6th game object, and it's object_type is #$05:
2023-05-24 09_37_18-NES MAKER 4.5.9 - Version_ 0x176 - AdventureTutorial.MST.png

In that case, the code should be:
Code:
LDA Object_type,x
CMP #$05  ;; "health pickup" object
BNE +notThisPickup


Also... 3 digits? Are you sure you want that?
Because it will imply several important modifications in the code:
First, that means you need myLives to be a 3 bytes variable (need to add myLives10 and myLives100 under myLives in the Project Settings > User Variables).
Then, handling a 3 bytes variable is way different than a single one
For example, instead of:
Code:
INC myLives
You will need to use:
Code:
AddValue #$03, myLives, #$01, #$00[/CODE
And when the player dies, instead of
[CODE]DEC myLives
You will need to use:
Code:
SubtractValue #$03, myLives, #$01, #$00[/CODE
And when checking if no more lives (for the game over), instead of just checking myLives, you need to check myLives+2 and myLives+1 and then myLives.
 

Dev Individual

New member
I decided to just go with 1 digit for my lives counter as it seemed simpler for me, but I appreciate the extra code that I could use in a future project.
Anyway, I tried to put the extra life as the 6th object but again it doesn't change the life count. I included some screenshots of the game I'm working on which I hope can help us figure out the issue I'm having. I blurred out the name of the game and some graphics as I'm not ready to show it yet. The one labelled Health Pickup is what I want to use for my extra life item.myLives.pngextralifecode.png
 

dale_coop

Moderator
Staff member
I see, you're not doing that correctly... the pickup script is NOT a input script.
The code to handle what happens when you collect a pickup object needs to be added in the "Pickup script" from the "Projet Settings > Scripts Settings":

2023-05-26 09_08_57-NM_4.5_SummerCamp.png


That script is executed the player collect a pickup script, you need to add your code somewhere in that script.
If it doesn't work for you, don't hesitate to share that script with your modification if you want us to check.
 

Dev Individual

New member
Hi, sorry for the delay. Anyway, I finally managed to get my extra life item to work. I can't believe I didn't think to put it in the script settings. Thank you so much for your help, I really appreciate it.
 
Top Bottom