How to set your player's health higher than 9, and actually update correctly in the HUD when you take damage [4.1.5]

JFranco

New member
Good afternoon,

I'm having a slight issue with my player's health:

I can set the Game Object's Health to '99', and I can set the myHealth initial value to '99' (and it appears in the HUD properly - it's presented in numerical format).

However, as soon as I start taking damage from enemies (keep in mind, they are only doing 1 damage per hit), when my player's health drops to '90', the game is over.

I'm guessing the code I need to modify is in here -- HandlePlayerHurt.ASM -- within the first few lines of code. Note: Most comments are mine.

Code:
;;  This works for single digit health. Player dies at when the 1s hit zero.

	LDA Object_health,x         ;load player's new health into accumulator?
	CLC							;clear the carry
	SEC                            ;; clear the carry (again ?)
	SBC #$01                    ;; subtract other's strength
	CMP #$01                    ;; compare to #$01
	BCS notPlayerDeath          ;; branch to notPlayerDeath if X >= #$01
	
	
	JSR HandlePlayerDeath       ;if Player Dead, jump to HandlePlayerDeath subroutine
	JMP doneWithPlayerHurt


What's throwing me off is:
Code:
SBC #$01                    ;; subtract other's strength

How do I correct this so that I can have 2 digits (ex: '99') or even 3 digits of health (ex: '200') and have it subtract properly down to zero?

Thanks,
Justin
 

JFranco

New member
I removed everything and inserted this, just to see how the HUD responded:
Code:
SubtractValue #$02, myHealth, #$01, #$00 ;Subtract -1 to a 2 digits HUD variable

The first enemy hit brought me down to '90', and every subsequent hit brought me down by 10 (90, 80, 70, 60, 50,...) until it brought me down to 00 and then restarted at 90 again to resume the countdown. (I removed any commands branching off if myHealth = 00)

I'm wondering if there's a hi and lo byte I need to be checking/updating?
 

JFranco

New member
From Joe:

Health is usually handled as a single hex value.

You need to treat health like score - multiple bytes, one for each digit. Then, use the subtraction macros rather than simple 8 bit subtraction.

Hopefully that helps point you in the right direction!
 

dale_coop

Moderator
Staff member
If you displays your HUD as a number in your HUD... (and if more than 9) you will have a LOT of issues...
You're close... yeah.

When a hud variable is displayed on the HUD AND as a number 2 digits, it uses two bytes (if displayed as number 3 digits, it will use 3 bytes .... if not displayed as number, or not displayed on the HUD, the variable is only 1 byte).
So when a variable is more than 1 byte, all the calculations become more difficult to write.
Usually when you use a 1 byte "myHealth" variable, to load/compare/save the Health you just use :
Code:
	LDA myHealth
For 2 bytes, you have to do double all the code:
Code:
	LDA myHealth
for the units digit of that number
And
Code:
	LDA myHealth+1
For the tenths digit of that number.

The problem with the Health... is that it use in a lot of scripts in NESmaker engine.
So, you will have to modify all those script to do comparisons for all the digits individually... (and do the AddValue and SubtractValue instead of the INC/ADC and the DEC/SBC)

If you are a coder, you can do that, and debug the code, when you have strange reactions (when you missed one or several modifications).
If you are NOT a coder, I 'd suggest to keep a 1 byte variable for myHealth ("1 digit" Number or a "varTiles" HUD element).
 
Top Bottom