Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter 4 updating HUD on level changes #28

Open
mrwnphs opened this issue Jul 15, 2024 · 2 comments
Open

Chapter 4 updating HUD on level changes #28

mrwnphs opened this issue Jul 15, 2024 · 2 comments

Comments

@mrwnphs
Copy link

mrwnphs commented Jul 15, 2024

I got the code to allow the player to get to new levels. But, the score and life count resets when I go to each new level. I have spent hours trying to resolve the problem. I did find a workaround, but it feels like a bad practice. I ended up adding global variables to the GameState script and I update the HUD through the HUD scene's _process function. Maintaining the score and life count via global variables seems like a reasonable thing to do. But updating the HUD via the _process function feels wasteful- why update the HUD score repeatedly when it hasn't changed?

I don't think that the code in the tutorial in Chapter 4 addresses the score and life count during level changes, but I am open to any thoughts or advice.

@mrwnphs
Copy link
Author

mrwnphs commented Jul 16, 2024

In order to support HUD updates during level changes, I switched from the signal based method to the Singleton or global variable method.

In the game_state.gd script, I added these two global variables:
var score = 0
var life_counter = 3

Then I changed the level base script (level_base.gd) to update the score:

# var score = 0: set = set_score

#func set_score(value):
	#score = value
	#score_changed.emit(score)

func _on_item_picked_up():
	#score += 1
	#score_changed.emit(score)
	GameState.score += 1
	$CanvasLayer/HUD.update_score(GameState.score)
	print(GameState.score)

Then updated the player.gd script:

	HURT:
		$AnimationPlayer.play("hurt")
		velocity.y = -200
		velocity.x = -100 * sign(velocity.x)
		# life -= 1
		GameState.life_counter -= 1
		get_parent().get_node("CanvasLayer/HUD").update_life(GameState.life_counter)
		await get_tree().create_timer(0.5).timeout
		if GameState.life_counter <= 0:
			change_state(DEAD)
		else:
			change_state(IDLE)

and commented out this stuff in the player.gd script:

#var life = 3: set = set_life
#func set_life(value):
	## I believe this function gets called when the value of life gets changed
	##     in the change_state function
	#life = value
	#life_changed.emit(life)
	#if life <= 0:
		#change_state(DEAD)

And, finally, I updated main.gd with this:

func _ready():
   var level_num = str(GameState.current_level).pad_zeros(2)
   var path = "res://level_%s.tscn" % level_num
   var level = load(path).instantiate()
   add_child(level)
   hud = level.get_node("CanvasLayer/HUD")
   if hud:
   	hud.update_score(GameState.score)
   	hud.update_life(GameState.life_counter)
   else:
   	print("HUD not found")

@packt-simrana
Copy link
Collaborator

Hi mrwnphs,

Thank you for bringing this issue to our attention. We appreciate your detailed feedback.

@cbscribe Could you please look into this issue and provide assistance? Your expertise will be invaluable in resolving the reader's concern.

Thank you for your help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants