[4.5] How can I get my player's health to appear in the HUD? [SOLVED]

JFranco

New member
Note: This presumes you were following and completed the Adventure Game Basics tutorial videos #1 to 7


IN 4 STEPS:

1. Create HUD elements

create a HUD element.
Type: string
Text: 'Health:'

create another HUD element
Type: number
Element Var: playerHealth
Max Value: 1


2. Create user variable called 'playerHealth'

go to Project Settings -> User Variables
create a new Variable. Name it 'playerHealth'. Give it an initial value of whatever you'd like player health to be (don't go higher than 9)


3. Create a 'Hurt' action state for your player

Go to Game Objects -> Player
Click on 'Object Details'
Go to Animations tab. Create a new Animation Type called 'Hurt'
Go to Actions tab. Set Action Step 7 to Animation Type 'Hurt'. Set Timer to 3. Set End Action to 'Go to First'.

4. Assign this script to your Common/Handle Player Hurt routine (via Project Settings -> Script Settings):

Code:
	;;;;;;;;;;;;;;;;;; Presumes there is a variable called myLives defined in user variables.
	;;;;;;;;;;;;;;;;;; You could also create your own variable for this.
	LDA gameHandler
	AND #%10000000
	BEQ +canHurtPlayer
		JMP +skipHurt
+canHurtPlayer:

;;;;;;;;;;;;;;;;;;;;;
;;;; Player takes health damage - custom code
;;;;;;;;;;;;;;;;;;;;;

    LDX player1_object					; load player1 object into x
    TXA									; transfer x to accumulator
    STA temp							; store accumulator value into 'temp' variable
	GetActionStep temp					; run GetActionStep macro, 'temp' contains player1 object (aka "what is player 1's current Action state?")
	CMP #$07                    		; are we in ActionState #7 (aka: 'Hurt' mode). My hurt ActionState might be different than yours. adjust accordingly.
	BEQ +skipHurt						; yes? then exit this script
		
		ChangeActionStep temp, #$07   	; no? then change action state to 'hurt'
		LDA #$80						; I'm not sure why this is important -- i think it's the amount of time to stay hurt?
		STA Object_action_timer,x		; stay hurt for this amount of time
	
	
	DEC playerHealth            ;decrease player health by one
	
	UpdateHudElement #$07       ;update HUD -- myHealth is assigned to HUD element 7 in my game. it may be different in yours. adjust accordingly
	
	LDA playerHealth            ;load player health into accumulator
	BEQ +loseLife               ;if player health equals zero, lose a life
		JMP +skipHurt           ;else, exit this script

+loseLife:

;;;;;;;;;;;;;;;;;;;;;
;;;;; End of custom code
;;;;;;;;;;;;;;;;;;;;;

	
	
	Dec myLives
	LDA myLives
	BNE myLivesNotZero
		JMP RESET ;; game over.
		;;;; also could warp to game over screen here instead.
myLivesNotZero:


;;;;;;;;;;;;;;;;;;;;;;;;;
;;; When Player begins new life, refresh health back to 9 - custom code
;;;;;;;;;;;;;;;;;;;;;;;;;

	LDA #$09					;load '9' into accumulator
	STA playerHealth			;update playerHealth to 9
	UpdateHudElement #$07       ;update HUD -- myHealth is assigned to HUD element 7 in my game. it may be different in yours. adjust accordingly

;;;;;;;;;;;;;;;;;;;;;;;;;
;;; End of custom code
;;;;;;;;;;;;;;;;;;;;;;;;;

	LDA continueMap
	STA warpMap
	
	LDA continueScreen
	STA currentNametable
	
	LDX player1_object
	STA Object_screen,x
	
	LDA #$02 ;; this is continue type warp.
	STA screenTransitionType ;; is of warp type

	
	LDA gameHandler
	ORA #%10000000
	STA gameHandler ;; this will set the next game loop to update the screen.

+skipHurt
------------------------------------------------

KNOWN ISSUES:

-if a monster walks into a player his health decreases by one, but if a player walks into a monster his health decreases by two.
 

offparkway

Active member
thanks for this! I'm new to the code stuff, and have somewhat of a grasp on some of it... how might I adapt this if I just wanted the play to get hurt for a period of time and remove a life (no health)?
 

jataka5000

New member
offparkway said:
thanks for this! I'm new to the code stuff, and have somewhat of a grasp on some of it... how might I adapt this if I just wanted the play to get hurt for a period of time and remove a life (no health)?

I'm not sure exactly what you're looking to accomplish here, but I think you want to have a single hit kill the player? In this case you could set the playerHealth to 1 (Project settings / user variables)... but maybe you are looking to do something else...

Code:
DEC playerHealth            ;decrease player health by one
 

offparkway

Active member
I could have been more clear. I was looking for the opposite, actually and I don't know why I phrased it that way. I was looking to have my player get hurt, but blink for a few seconds as a result (or show some sort of hurt animation), and keep moving. From the tutorials, the player restarts the screen every time he gets hurt. So I was looking to do something like Zelda... if link gets hit, hearts decrease and he dies once hearts are at 0.

Currently my game works in a way that I like: get hit = health meter goes down but player keeps moving and fighting. Once health reaches 0, Lives are decreased and the player starts again from last checkpoint. Once Lives = 0, game is over.

Thanks for the response!
 
Top Bottom